-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautoscroll.js
130 lines (119 loc) · 3.52 KB
/
autoscroll.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* Autoscroll v1
* By humyfred https://humyfred.github.io/Autoscroll
* Github: https://github.com/humyfred/Autoscroll
* MIT Licensed.
*/
;(function(){
/*
*/
function Autoscroll(options){
this.scrollObj = null;
options = options || {};
this.scrollId = options.scrollId;
this.scrollIdx = options.scrollIndex || 0;
this.time = options.speed || 40;
//this.fixHeight = options.fixHeight|| '100px';
this.stopable = options.stopable; //是否可停止滚动
this.scrollCache = null; //正在滚动的对象缓存
this.stopCache = null; //停止滚动的对象缓存
this.init();
}
Autoscroll.prototype = {
consturctor:Autoscroll,
init:function(){
this.scrollObj = document.getElementById(this.scrollId);
this.addScrollAction();
},
getFixHeight:function(){
var objHeight = this.scrollObj.clientHeight;
if(objHeight>380){
return 380;
}else{
return objHeight;
}
},
addScrollAction:function(){
var self = this;
var parent = this.scrollObj.parentNode;
parent.style.height = this.getFixHeight()+ 'px';
parent.style.overflow = 'hidden';
var clone = self.cloneAndAppendParent(this.scrollObj,parent);
if(self.stopable){ //注册鼠标事件
self.stopToView()
}
var timer = setTimeout(function(){
self.toScroll();
}, self.time);
self.scrollCache = {
obj : this.scrollObj,
clone: clone,
timer: timer
}
},
cloneAndAppendParent : function(obj,parent){
var clone = obj.cloneNode(true);
parent.appendChild(clone);
return clone;
},
scrollToOrback :function(val){
if(val.clone.offsetHeight-val.obj.parentNode.scrollTop<=0){
val.obj.parentNode.scrollTop = 0;
}else{
val.obj.parentNode.scrollTop++;
}
},
toScroll:function(){
var self = this;
var val = self.scrollCache;
clearTimeout(val.timer);
if(!this.watchStopCahche()){ //看看是否有鼠标事件,把当前对象注册到停止滚动数组里
this.scrollToOrback(val);
val.timer = setTimeout(function(){
self.toScroll();
}, self.time);
}
},
watchStopCahche:function(){
if(this.stopCache!==null){
return true;
}
return false; //没有,继续滚动
},
stopToView : function(){
var parent = this.scrollObj.parentNode;
this.bindFn(parent,'mouseover',function(e){
var target = e?e.currentTarget.children[this.scrollIdx]:event.srcElement.parentNode.parentNode;
if(this.stopCache===null){
this.stopCache = {obj:target};
}
});
this.bindFn(parent,'mouseout',function(e){
var target = e?e.currentTarget.children[this.scrollIdx]:event.srcElement.parentNode.parentNode;
var self = this;
if(this.stopCache.obj===target){
this.stopCache.timer = setTimeout(function(){
self.toScroll();
}, this.time);
this.stopCache.clone = this.nextElement(target);
this.scrollCache = this.stopCache;
this.stopCache = null;
}
})
},
nextElement:function(obj){
return obj.nextSibling.nextSibling;
},
bindFn:function(dom,type,fn){
if(window.addEventListener){
dom.addEventListener(type,fn.bind(this),false)
}else if(window.attachEvent){
dom.attachEvent('on'+type,fn.bind(this))
}
},
}
if (typeof module !== 'undefined' && typeof module.exports === 'object') {
module.exports = Autoscroll;
} else {
window.Autoscroll = Autoscroll;
}
})();