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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
| /*
*图片文字滚动扩展包
*版本:v1.2
*编码:utf-8版本
*作者:cici
*email:chengds@ifeng.com
*日期:2009-4-15
*/
/*
container:滚动的容器ID
btnPrevious:上一步按钮的ID
btnNext:下一步按钮的ID
*/
function ifeng_Scroll(container,btnPrevious,btnNext)
{
////////////////对外接口/////////////////////////
this.IsAutoScroll = true; //是否自动滚动
this.IsSmoothScroll= true;//是否平滑连续滚动 平滑滚动:true 间隔滚动:false
this.PauseTime = 1000;//间隔滚动时每次滚动间隔的时间。单位:毫秒。建议值:100--3000 适用于间隔滚动。
this.Direction = "N"; //滚动方向.向东:E,向北:N
this.ControllerType = "click";//上一个和下一个按钮事件的触发方式:click为点击触发滚动 否则就是 鼠标按住按钮触发滚动。支持click 和mousedown两种模式
this.BackCall = null;//回调函数 滚动到末尾时执行
this.Step = 1;//步长 可以理解为速度1--10
////////////////对外接口/////////////////////////
this.Speed = 10;
this.container = container;
this.NextButton = this.$(btnNext);
this.PreviousButton = this.$(btnPrevious);
this.ScrollElement = this.$(container);
this.UlElement = this.$(container).getElementsByTagName('ul')[0];//ul元素
this.UlElement.innerHTML+=this.UlElement.innerHTML;
this.UlSpace ;//ul的实际宽度
this.LiSpace;
}
ifeng_Scroll.prototype = {
lastpos:0,
curPos:0,
curTimeoutId:null,
curIntervalScrollTimeoutId:null,
ScrollElementPos:0,
$:function(element)
{
return document.getElementById(element);
},
Init:function()
{
this.UlSpace = this.Direction=="E"?this.UlElement.offsetWidth:this.UlElement.offsetHeight;//ul的实际宽度
this.LiSpace = parseInt(this.UlSpace/this.UlElement.getElementsByTagName('li').length);
this.UlSpace = this.LiSpace*this.UlElement.getElementsByTagName('li').length;
this.Direction=="E"?this.$(this.container).style.width=this.UlSpace+"px":this.$(this.container).style.height=this.UlSpace+"px";
//设置基础样式
this.ScrollElement.style.overflow="visible";
this.ScrollElement.parentNode.style.overflow="hidden";
this.Direction=="E"?this.ScrollElement.style.width="10000px":this.ScrollElement.style.height="10000px";
this.UlElement.style.float="left";
this.ScrollType=this.Direction=="E"?"left":"top";
this.Bind(this,this.PreviousButton,this.ControllerType,"Pre");
this.Bind(this,this.NextButton,this.ControllerType,"Next");
this.ScrollElement.onmouseover = this.GetFunction(this,"MouseOver");
this.ScrollElement.onmouseout = this.GetFunction(this,"MouseOut");
},
Reset:function()
{
this.Pause();
this.ScrollElement.style[this.ScrollType] = '0px';
},
Bind:function(_this,el,type,param)
{
if(el)
{
if(type=="click"){
el.onclick = this.GetFunction(this,param);
}
else
{
el.onmousedown = this.GetFunction(this,"MouseDown",param);
el.onmouseup = this.GetFunction(this,"MouseUp");
}
el.onmouseover = this.GetFunction(this,"MouseOver");
el.onmouseout = this.GetFunction(this,"MouseOut");
}
},
Start:function()
{
if(!this.IsAutoScroll) return;
if(this.IsSmoothScroll)
{
this.SmoothScroll();
}
else
{
this.IntervalScroll();
}
},
Pause:function()
{
clearTimeout(this.curTimeoutId);
clearTimeout(this.curIntervalScrollTimeoutId);
},
MouseOver:function()
{
clearTimeout(this.mouseoutTimeoutId);
this.mouseoverTimeoutId = setTimeout(this.GetFunction(this,"Pause"),10);
},
MouseOut:function()
{
clearTimeout(this.mouseoverTimeoutId);
this.mouseoutTimeoutId = setTimeout(this.GetFunction(this,"Start"),10);
},
MouseDown:function(direction)
{
var _step;
var _to;
if(direction=="Pre")
{
_step = this.Step*2;
curPos = parseInt(this.ScrollElement.style[this.ScrollType]);
if(!curPos) curPos=0;
if(curPos==0)
{
this.ScrollElement.style[this.ScrollType] = -this.UlSpace/2 + "px";
this.curPos=-this.UlSpace/2;
}
_to = 0;
}
else
{
_step = -this.Step*2;
_to = -this.UlSpace/2;
}
moveParams = {from:this.curPos, to:_to, step: _step,controller:"MouseDown:" + direction,callback:this.GetFunction(this,"ScrollFinish")};
this.RunScroll(moveParams);
},
MouseUp:function()
{
clearTimeout(this.curTimeoutId);
},
Pre:function()
{
var curPos = parseInt(this.ScrollElement.style[this.ScrollType]);
if(!curPos) curPos=0;
var _to ;
if(curPos==0)
{
this.ScrollElement.style[this.ScrollType] = -this.UlSpace/2 + "px";
this.curPos=-this.UlSpace/2;
_to = -this.UlSpace/2 + this.LiSpace;
}
else
{
_to = this.curPos%this.LiSpace==0?this.curPos + this.LiSpace:parseInt(this.curPos/this.LiSpace)*this.LiSpace;
}
moveParams = {from:this.curPos, to:_to, step: this.Step*2,controller:"Previous",callback:this.GetFunction(this,"ScrollFinish")};
this.RunScroll(moveParams);
},
Next:function()
{
_to = this.curPos%this.LiSpace==0?this.curPos - this.LiSpace:(parseInt(this.curPos/this.LiSpace)-1)*this.LiSpace;
moveParams = {from:this.curPos, to:_to, step: -this.Step*2,controller:"Next",callback:this.GetFunction(this,"ScrollFinish")};
this.RunScroll(moveParams);
},
IntervalScroll:function()
{
var _to = parseInt(this.curPos/this.LiSpace)*this.LiSpace-this.LiSpace;
var moveParams = {from:this.curPos, to:_to, step: -this.Step,controller:"IntervalScroll",callback:this.GetFunction(this,"ScrollFinish")};
this.RunScroll(moveParams);
},
SmoothScroll:function()
{
var _to = -this.UlSpace/2;
var moveParams = {from:this.curPos, to:_to, step: -this.Step,controller:"SmoothScroll",callback:this.GetFunction(this,"ScrollFinish")};
this.RunScroll(moveParams);
},
RunScroll:function(params)
{
this.Scroll(params);
},
Scroll:function(param)
{
var step = Math.abs(param.to - this.curPos)<Math.abs(param.step)?param.to - this.curPos:param.step;
this.ScrollElement.style[this.ScrollType] =(param.from+step)+"px";
this.curPos = parseInt(this.ScrollElement.style[this.ScrollType]);
clearTimeout(this.curTimeoutId);
if(this.curPos!=param.to)
{
var moveParams = {from:this.curPos, to:param.to, step: param.step,controller:param.controller,callback:param.callback};
this.curTimeoutId = setTimeout(this.GetFunction(this,"Scroll",moveParams),this.Speed);
}
else
{
if(param.callback) param.callback();
if(param.controller=="SmoothScroll")
{ this.SmoothScroll();}
else if (param.controller=="IntervalScroll")
{
if(this.curIntervalScrollTimeoutId) clearTimeout(this.curIntervalScrollTimeoutId);
this.curIntervalScrollTimeoutId = setTimeout(this.GetFunction(this,"IntervalScroll"),this.PauseTime);
}
else if(param.controller.indexOf("MouseDown")!=-1)
{
derection = param.controller.split(':')[1];
this.MouseDown(derection);
}
}
},
ScrollFinish:function()
{
if(this.curPos<=-this.UlSpace/2)
{
this.ScrollElement.style[this.ScrollType] = "0px";
}
else if(this.curPos>=0)
{
this.ScrollElement.style[this.ScrollType] = -this.UlSpace/2 + "px";
}
this.curPos = parseInt(this.ScrollElement.style[this.ScrollType]);
if(this.BackCall)this.BackCall();
},
GetFunction:function(variable,method,param)
{
return function()
{
variable[method](param);
}
}
} |