1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
if (typeof Object.create !== "function") {
Object.create = function(obj) {
function F() {
}
F.prototype = obj;
return new F();
};
}
(function($, window, document) {
var Newstape = {
$elem: null,
$content: null,
options: {},
height: 0,
contentHeight: 0,
dragstartPos: 0,
dragDeltaY: 0,
dragDeltaYReduce: 0,
timer: null,
pos: 0,
init: function(options, el) {
var base = this;
base.$elem = $(el);
base.$content = $('.newstape-content', base.$elem);
base.options = $.extend({}, $.fn.newstape.options, base.$elem.data(), options);
var heightRefresh = function() {
base.height = base.$elem.outerHeight();
base.contentHeight = base.$content.outerHeight();
};
if (base.options.heightSpy) {
setInterval(heightRefresh, 1000);
}
heightRefresh();
var play = function() {
base.timer = setInterval(function() {
base.move();
}, base.options.period);
};
base.$elem.bind('mouseover.newstape', function() {
clearInterval(base.timer);
});
base.$elem.bind('mouseout.newstape', function() {
play();
});
if (base.options.mousewheel) {
base.$elem.bind('mousewheel.newstape', function(e) {
e.preventDefault();
base.pos = (e.deltaY > 0) ? base.pos + base.options.mousewheelRate : base.pos - base.options.mousewheelRate;
base.move();
});
}
$('a', base.$elem).focus(function(e) {
base.$elem.scrollTop(0);
base.pos = base.height - $(this).position().top - $(this).outerHeight();
base.move();
});
if (base.options.dragable) {
base.$elem.bind('dragstart.newstape', function(e, dd) {
base.dragDeltaY = 0;
base.dragDeltaYReduce = 0;
base.dragstartPos = base.pos;
base.$elem.addClass('newstape-drag');
}).bind('drag.newstape', function(e, dd) {
base.dragDeltaY = dd.deltaY;
base.pos = base.dragstartPos + (dd.deltaY - base.dragDeltaYReduce);
base.move();
}).bind('dragend.newstape', function(e, dd) {
base.$elem.removeClass('newstape-drag');
});
}
play();
},
move: function() {
var base = this;
var dragUpdate = function() {
base.dragstartPos = base.pos;
base.dragDeltaYReduce = base.dragDeltaY;
base.dragDeltaY = 0;
};
if (base.pos <= base.contentHeight * -1) {
base.pos = base.height;
dragUpdate();
}
if (base.pos >= base.height + base.options.offset) {
base.pos = base.contentHeight * -1;
dragUpdate();
}
if (!base.$elem.hasClass('newstape-drag')) {
base.pos = base.pos - base.options.offset;
}
base.$content.css('top', parseInt(base.pos) + 'px');
}
};
$.fn.newstape = function(options) {
return this.each(function() {
if ($(this).data("newstape-init") === true) {
return false;
}
$(this).data("newstape-init", true);
var newstape = Object.create(Newstape);
newstape.init(options, this);
$.data(this, "newstape", newstape);
});
};
$.fn.newstape.options = {
period: 30,
offset: 1,
mousewheel: true,
dragable: true,
mousewheelRate: 30,
heightSpy: true
};
}(jQuery, window, document));
|