-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.kd-scroll.js
116 lines (97 loc) · 3.38 KB
/
jquery.kd-scroll.js
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
/**今点连续滚动插件 2013-7-12 by Will
*/
//创建一个闭包
(function ($) {
//插件定义
$.fn.scroll = function (options) {
var opts = $.extend({}, $.fn.scroll.defaults, options);
return this.each(function () {
var $this = $(this);
_init($this, opts);
});
// debug(this);
};
//初始化插件
function _init($this, opts) {
//要滚动的元素
var scroller = $this.find(opts.scroller);
var repeatHtml = $("<p>").append(scroller.clone()).html();
if (scroller.length == 0) {
debug('未找到要滚动的元素:' + opts.scroller);
return;
}
//重复添加滚动元素,直至填满容器
var ratio;
if (opts.vertical) {
if (scroller.height() == 0) {
debug('要垂直滚动的元素:' + opts.scroller + '的高度不能为0');
return;
}
ratio = $this.height() / scroller.height() + 1;
var repeatScroller = '';
for (var i = 1; i < ratio; i++) {
repeatScroller += repeatHtml;
}
$this.append(repeatScroller);
}
//水平滚动,添加容器
else {
if (scroller.width() == 0) {
debug('要水平滚动的元素:' + opts.scroller + '的宽度不能为0');
return;
}
ratio = $this.width() / scroller.width() + 1;
$this.wrapInner('<div class="kd-scroller-horizontal-container"></div>');
var repeatScroller = '';
for (var i = 1; i < ratio; i++) {
repeatScroller += repeatHtml;
}
$this.find('.kd-scroller-horizontal-container').append(repeatScroller);
}
//循环滚动
var scrolling = setInterval(function () {
scroll($this, scroller, opts);
}, opts.speed);
$this.hover(function () {
clearInterval(scrolling);
}, function () {
//离开继续调用
scrolling = setInterval(function () {
scroll($this, scroller, opts);
}, opts.speed);
});
}
//滚动
function scroll($this, scroller, opts) {
//垂直滚动
if (opts.vertical) {
if ($this.scrollTop() >= scroller.height())
$this.scrollTop(0);
else {
$this.scrollTop($this.scrollTop() + opts.step);
}
}
//水平滚动
else {
if ($this.scrollLeft() >= scroller.width())
$this.scrollLeft(0);
else {
$this.scrollLeft($this.scrollLeft() + opts.step);
}
}
};
// 私有函数:debugging
function debug(msg) {
if (window.console && window.console.log) {
var now = new Date();
window.console.log(now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds() + ' ' + msg);
}
};
//默认参数
$.fn.scroll.defaults = {
speed: 40, //滚动速度
scroller: '.kd-scroller',//滚动元素钩子
vertical: false,//是否垂直滚动
step: 1//每次滚动像素
};
})(jQuery);