-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
159 lines (156 loc) · 4.81 KB
/
index.html
File metadata and controls
159 lines (156 loc) · 4.81 KB
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
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
body {
margin: 0;
}
/*#scroll-box{
width: 500px;
height: 300px;
background: #ccc;
margin:0 auto;
overflow: hidden;
}*/
.scroll-item {
width: 100%;
height: 45px;
border-bottom: 1px solid #f00;
text-align: center;
line-height: 45px;
}
.scroll-item:nth-child(odd) {
/*background: #f6f6f6*/
}
/*.animate{-webkit-transition:all 1s ease-out;}*/
</style>
<body>
<div
style="
width: 500px;
height: 183px;
border: 1px solid #f00;
overflow: hidden;
"
>
<div id="scroll-box">
<div class="scroll-item">11</div>
<div class="scroll-item">22</div>
<div class="scroll-item">33</div>
<div class="scroll-item">44</div>
<div class="scroll-item">55</div>
<div class="scroll-item">66</div>
<div class="scroll-item">77</div>
<div class="scroll-item">88</div>
<div class="scroll-item">99</div>
<div class="scroll-item">00</div>
</div>
</div>
<script type="text/javascript">
/*
* 列表定时滚动效果(javascript)
* @listId :滚动列表ID
* @listTagName :滚动元素
* @scrollNum :设置滚动元素数量
* @speed : 滚动速度
*/
function fnScrollList() {
this.init.apply(this, arguments);
}
fnScrollList.prototype = {
init: function (listId, TagName, scrollNum, speed) {
var _this = this;
this.oList = this.$$(listId);
this.scrollTimer = null;
this.speed = speed || 1000;
this.scrollNum = scrollNum || 1;
this.TagName = TagName;
this.distance =
this.oList.getElementsByTagName(TagName)[0].offsetHeight *
this.scrollNum; //列表移动距离
this.oList.style.marginTop = 0 + "px";
this.oList.onmouseover = function () {
_this.pause();
};
this.oList.onmouseout = function () {
_this.scrollTimer = setTimeout(function () {
_this.play();
}, _this.speed);
};
this.play();
},
play: function () {
var _this = this;
var options = { marginTop: "-" + _this.distance };
_this.anim(_this.oList, options, function () {
//回调函数,移动列表元素
for (var i = 0, j = 0; i < _this.scrollNum; i++) {
var node = _this.oList.getElementsByTagName(_this.TagName)[j];
if (_this.TagName == "tr") {
_this.oList.getElementsByTagName("tbody")[0].appendChild(node);
} else {
_this.oList.appendChild(node);
}
}
_this.oList.style.marginTop = "0px";
});
_this.scrollTimer = setTimeout(function () {
_this.play(_this.distance);
}, _this.speed);
},
pause: function () {
clearTimeout(this.scrollTimer);
},
//动画函数
anim: function (oElement, oAttr, fnCallback) {
var _this = this;
clearInterval(oElement.timer);
oElement.timer = setInterval(function () {
var bStop = true;
for (var property in oAttr) {
var iCur = parseFloat(_this.css(oElement, property)); //获取当前位置属性值
var iSpeed = (oAttr[property] - iCur) / 5; //移动进度
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if (iCur != oAttr[property]) {
//如果当前数值不等于目标数值,则数值递增
bStop = false;
_this.css(oElement, property, iCur + iSpeed);
}
}
if (bStop) {
clearInterval(oElement.timer);
fnCallback && fnCallback.apply(_this, arguments);
}
}, 50);
},
//处理样式函数
css: function (oElement, attr, value) {
if (arguments.length == 2) {
return oElement.currentStyle
? oElement.currentStyle[attr]
: getComputedStyle(oElement, null)[attr];
} else if (arguments.length == 3) {
switch (attr) {
case "top":
case "left":
case "marginTop":
oElement.style[attr] = value + "px";
break;
default:
oElement.style[attr] = value;
break;
}
}
},
$$: function (o) {
if (o) {
return document.getElementById(o);
}
},
};
var list = new fnScrollList("scroll-box", "div", 1, 2000);
</script>
</body>
</html>