-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
177 lines (177 loc) · 7.41 KB
/
index.html
File metadata and controls
177 lines (177 loc) · 7.41 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js左右箭头按钮控制图片轮播滚动</title>
<style>
*{margin:0;padding:0;list-style-type:none;}
a,img{border:0;}
body{font:12px/180% Arial, Helvetica, sans-serif, "新宋体";}
/* case */
.case{height:627px;overflow:hidden;width:891px;margin:0 auto;}
.case_box{position:relative;margin:140px auto 0px;width:860px;height:470px;overflow:hidden}
.case_box p{z-index:2;position:absolute;text-indent:-9999px;width:28px;height:51px;top:40%;cursor:pointer}
.case_box .prev{text-indent:-9999px;background:url(images/previous.png) no-repeat;left:0px}
.case_box .next{background:url(images/next.png) no-repeat;top:40%;right:0px}
.case_box ul{position:absolute;height:470px;overflow:hidden;top:0px;left:0px}
.case_box ul li{width:860px;float:left;height:470px}
.case_box ol{position:absolute;bottom:0;left:50%;margin:0 0 0 -65px;height:12px;z-index:99;}
.case_box ol li{float:left;margin:0 4px;display:inline;width:12px;height:12px;line-height:999em;background:url(images/pagination.png) no-repeat;overflow:hidden;cursor:pointer;}
.case_box ol li.active{background-position:0 -12px;}
</style>
</head>
<body>
<div class="case">
<div id="slider" class="case_box">
<ul>
<li class="case_1"><a href="#"><img src="images/case_1.png"/></a></li>
<li class="case_2"><a href="#"><img src="images/case_2.png"/></a></li>
<li class="case_3"><a href="#"><img src="images/case_3.png"/></a></li>
<li class="case_4"><a href="#"><img src="images/case_4.png"/></a></li>
<li class="case_5"><a href="#"><img src="images/case_5.png"/></a></li>
<li class="case_6"><a href="#"><img src="images/case_6.png"/></a></li>
</ul>
</div>
</div>
</body>
</html>
<script>
var Effect = (function() {
var Slider = function(o) {
this.setting = typeof o === 'object' ? o : {};
this.target = this.setting.target || 'slider';
this.showMarkers = this.setting.showMarkers || false;
this.showControls = this.setting.showControls || false;
this.timer = null;
this.currentTime = null;
this.ms = 35;
this.autoMs = 3000;
this.iTarget = 0;
this.nextTarget = 0;
this.speed = 0;
this.init();
this.handleEvent();
};
Slider.prototype = {
init: function() {
this.obj = document.getElementById(this.target);
this.oUl = this.obj.getElementsByTagName('ul')[0];
this.aUlLis = this.oUl.getElementsByTagName('li');
this.width = this.aUlLis[0].offsetWidth;
this.number = this.aUlLis.length;
this.oUl.style.width = this.width * this.number + 'px';
if(this.showMarkers) {
var oDiv = document.createElement('div');
var aLis = [];
for(var i = 0; i < this.number; i++) {
aLis.push('<li>'+ (i+1) +'<\/li>');
}
oDiv.innerHTML = '<ol>'+ aLis.join('') +'<\/ol>';
this.obj.appendChild(oDiv.firstChild);
this.aLis = this.obj.getElementsByTagName('ol')[0].getElementsByTagName('li');
this.aLis[0].className = 'active';
oDiv = null;
}
if(this.showControls) {
this.oPrev = document.createElement('p');
this.oNext = document.createElement('p');
this.oPrev.className = 'prev';
this.oPrev.innerHTML = '«';
this.oNext.className = 'next';
this.oNext.innerHTML = '»';
this.obj.appendChild(this.oPrev);
this.obj.appendChild(this.oNext);
}
},
handleEvent: function() {
var that = this;
this.currentTime = setInterval(function() {
that.autoPlay();
}, this.autoMs);
this.addEvent(this.obj, 'mouseover', function() {
clearInterval(that.currentTime);
});
this.addEvent(this.obj, 'mouseout', function() {
that.currentTime = setInterval(function() {
that.autoPlay();
}, that.autoMs);
});
if(this.showMarkers) {
for(var i = 0; i < this.number; i++) {
var el = this.aLis[i];
(function(index) {
that.addEvent(el, 'mouseover', function() {
that.goTime(index);
});
})(i);
}
}
if(this.showControls) {
this.addEvent(this.oPrev, 'click', function() {
that.fnPrev();
});
this.addEvent(this.oNext, 'click', function() {
that.autoPlay();
});
}
},
addEvent: function(el, type, fn) {
if(window.addEventListener) {
el.addEventListener(type, fn, false);
}
else if(window.attachEvent) {
el.attachEvent('on' + type, fn);
}
},
fnPrev: function() {
this.nextTarget--;
if(this.nextTarget < 0) {
this.nextTarget = this.number - 1;
}
this.goTime(this.nextTarget);
},
autoPlay: function() {
this.nextTarget++;
if(this.nextTarget >= this.number) {
this.nextTarget = 0;
}
this.goTime(this.nextTarget);
},
goTime: function(index) {
var that = this;
if(this.showMarkers) {
for(var i = 0; i < this.number; i++) {
i == index ? this.aLis[i].className = 'active' : this.aLis[i].className = '';
}
}
this.iTarget = -index * this.width;
if(this.timer) {
clearInterval(this.timer);
}
this.timer = setInterval(function() {
that.doMove(that.iTarget);
}, this.ms);
},
doMove: function(target) {
this.oUl.style.left = this.speed + 'px';
this.speed += (target - this.oUl.offsetLeft) / 3;
if(Math.abs(target - this.oUl.offsetLeft) === 0) {
this.oUl.style.left = target + 'px';
clearInterval(this.timer);
this.timer = null;
}
}
};
return {
slider: function(o) {
var tt = new Slider(o);
}
};
})();
// 调用语句
Effect.slider({
'targetElement': 'slider',
'showMarkers': true,
'showControls': true
});
</script>