Skip to content

Commit 8798d19

Browse files
committed
add button
1 parent 056fb29 commit 8798d19

File tree

3 files changed

+157
-24
lines changed

3 files changed

+157
-24
lines changed

slider/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<li class="slider-item"><img src="images/pic23.gif" alt="3"></li>
1616
<li class="slider-item"><img src="images/pic24.gif" alt="4"></li>
1717
</ul>
18+
<div class="slider-button prev">&lt;</div>
19+
<div class="slider-button next">&gt;</div>
1820
</div>
1921
<script>
2022
Slider('#slider')

slider/slider.css

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ ul,li {
1919
width: 100%;
2020
height: 220px;
2121
overflow: hidden;
22+
-webkit-user-select: none;
23+
-ms-user-select: none;
24+
user-select: none;
2225
}
2326

2427
.slider-items {
@@ -29,13 +32,11 @@ ul,li {
2932
.slider-item {
3033
float: left;
3134
text-align: center;
35+
cursor: pointer;
3236
}
3337

3438
.slider-item > img {
3539
width: 100%;
36-
-webkit-user-select: none;
37-
-ms-user-select: none;
38-
user-select: none;
3940
pointer-events: none;
4041
}
4142

@@ -65,4 +66,27 @@ ul,li {
6566
.slider-bullet-active {
6667
opacity: 1;
6768
background-color: #007aff;
69+
}
70+
71+
.slider-button {
72+
position: absolute;
73+
top: 50%;
74+
width: 50px;
75+
height: 50px;
76+
text-align: center;
77+
line-height: 50px;
78+
margin-top: -25px;
79+
z-index: 10;
80+
font-size: 4rem;
81+
color: gray;
82+
-webkit-user-select:none;
83+
user-select:none;
84+
}
85+
86+
.next {
87+
right: 0px;
88+
}
89+
90+
.prev {
91+
left: 0px;
6892
}

slider/slider.js

Lines changed: 128 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
delay:5000,
2626
direction: 'horizontal',
2727
autoplay: true,
28-
bounceRatio:0.3,
28+
bounceRatio:0.1,
2929
pagination:true,
3030
paginationClass:'slider-pagination',
3131
bulletClass:'slider-bullet',
@@ -48,6 +48,30 @@
4848
}
4949
return source;
5050
}
51+
var Device = (function () {
52+
var ua = navigator.userAgent;
53+
var android = ua.match(/(Android);?[\s\/]+([\d.]+)?/);
54+
var ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
55+
var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
56+
var iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/);
57+
return {
58+
ios: ipad || iphone || ipod,
59+
android: android,
60+
desktop:!(ipad || iphone || ipod || android)
61+
};
62+
})();
63+
64+
function translate3d(element,x,y) {
65+
x = x === undefined ? 0 : x;
66+
y = y === undefined ? 0 : x;
67+
element.style['-webkit-transform'] = 'translate3d(-'+x+'px,'+y+'px,0px)';
68+
element.style['transform'] = 'translate3d(-'+x+'px,'+y+'px,0px)';
69+
}
70+
71+
function transition(element,time){
72+
element.style['-webkit-transition-duration'] = time+'ms';
73+
element.style['transition-duration'] = time+'ms';
74+
}
5175

5276
function Slider( selector, options ) {
5377
options = extend(defaults,options);
@@ -71,7 +95,10 @@
7195
this.sliderCount = sliderCount;
7296
this.lastIndex = sliderCount - 1;
7397
this.firstIndex = 0;
98+
this.isAnimating = false;
99+
this.axis = {x:0,y:0};
74100
this.initSlides();
101+
this.bindEvents();
75102
}
76103

77104
var fn = Slider.init.prototype;
@@ -80,6 +107,7 @@
80107
var width = this.wrap.clientWidth;
81108
this.slideWidth = width;
82109
this.wrap.style.width = width * this.sliderCount + 'px';
110+
this.bounceWidth = this.params.bounceRatio * width;
83111
this.activeIndex = 0;
84112
this.slideStack = [];
85113
for(var i = 0;i<this.sliderCount;i++){
@@ -118,23 +146,25 @@
118146
this.lastIndex += 2;
119147
}
120148

149+
fn.fixedLoop = function(activeIndex){
150+
if(!this.params.loop) return;
151+
translate3d(this.wrap,this.slideWidth*activeIndex);
152+
transition(this.wrap,0);
153+
this.activeIndex = activeIndex;
154+
this.isAnimating = false;
155+
}
156+
121157
fn.fixedNextLoop = function(){
122158
var that = this;
123-
var x = this.slideWidth;
124159
setTimeout(function(){
125-
translate3d(that.wrap,x);
126-
transition(that.wrap,0);
127-
that.activeIndex = 1;
160+
that.fixedLoop(1)
128161
},that.params.speed);
129162
}
130163

131164
fn.fixedPrevLoop = function(){
132165
var that = this;
133-
var x = this.slideWidth*(this.lastIndex-1);
134166
setTimeout(function(){
135-
translate3d(that.wrap,x);
136-
transition(that.wrap,0);
137-
that.activeIndex = that.lastIndex-1;
167+
that.fixedLoop(that.lastIndex-1)
138168
},that.params.speed);
139169
}
140170

@@ -186,11 +216,12 @@
186216
this.params.autoplay && clearTimeout(this.timeId);
187217
return;
188218
}
219+
this.isAnimating = true;
189220
translate3d(this.wrap,activeIndex*this.slideWidth);
190221
transition(this.wrap,this.params.speed);
191222
this.setActivePagination();
192223
if(activeIndex === this.lastIndex){
193-
this.params.loop && this.fixedNextLoop();
224+
this.fixedNextLoop();
194225
}
195226
}
196227

@@ -200,25 +231,101 @@
200231
this.params.autoplay && clearTimeout(this.timeId);
201232
return;
202233
}
234+
this.isAnimating = true;
203235
translate3d(this.wrap,activeIndex*this.slideWidth);
204236
transition(this.wrap,this.params.speed);
205237
this.setActivePagination();
206238
if(activeIndex === this.firstIndex){
207-
this.params.loop && this.fixedPrevLoop();
239+
this.fixedPrevLoop();
208240
}
209241
}
210242

211-
function translate3d(element,x,y) {
212-
x = x === undefined ? 0 : x;
213-
y = y === undefined ? 0 : x;
214-
element.style['-webkit-transform'] = 'translate3d(-'+x+'px,'+y+'px,0px)';
215-
element.style['transform'] = 'translate3d(-'+x+'px,'+y+'px,0px)';
243+
fn.bindEvents = function(){
244+
if(Device.desktop){
245+
this.wrap.addEventListener('mousedown',this,false);
246+
this.wrap.addEventListener('mousemove',this,false);
247+
this.wrap.addEventListener('mouseup',this,false);
248+
}else{
249+
this.wrap.addEventListener('touchstart',this,false);
250+
this.wrap.addEventListener('touchmove',this,false);
251+
this.wrap.addEventListener('touchend',this,false);
252+
}
253+
this.wrap.addEventListener('transitionend',this,false);
254+
this.container.addEventListener('click',this,false);
255+
}
256+
257+
fn.transitionend = function(){
258+
this.isAnimating = false;
259+
}
260+
261+
fn.start = function(pageX){
262+
this.axis.x = pageX;
263+
if(this.params.autoplay){
264+
this.params.autoplay = false;
265+
this.timeId && clearTimeout(this.timeId);
266+
}
267+
}
268+
269+
fn.move = function(pageX){
270+
if(this.isAnimating) return;
271+
var distance = this.axis.x - pageX;
272+
translate3d(this.wrap,distance + this.slideWidth*this.activeIndex);
273+
transition(this.wrap,0);
274+
if(distance > 0){
275+
if(distance > this.bounceWidth){
276+
this.next();
277+
}
278+
}else{
279+
if(-distance > this.bounceWidth){
280+
this.prev();
281+
}
282+
}
283+
}
284+
285+
fn.stop = function(){
286+
if(this.isAnimating)return;
287+
translate3d(this.wrap,this.slideWidth*this.activeIndex);
288+
transition(this.wrap,this.params.speed);
289+
}
290+
291+
fn.handleEvent = function(e){
292+
var type = e.type;
293+
switch(type){
294+
case 'mousedown':
295+
this.start(e.pageX);
296+
break;
297+
case 'mousemove':
298+
if(this.axis.x === 0) return;
299+
this.move(e.pageX);
300+
break;
301+
case 'mouseup':
302+
this.axis.x = 0;
303+
this.stop();
304+
break;
305+
case 'touchstart':
306+
this.start(e.targetTouches[0].pageX);
307+
break;
308+
case 'touchmove':
309+
this.move(e.targetTouches[0].pageX);
310+
break;
311+
case 'touchend':
312+
this.stop();
313+
break;
314+
case 'transitionend':
315+
case 'WebkitTransitionend':
316+
this.transitionend();
317+
break;
318+
case 'click':
319+
e.stopPropagation();
320+
var evt = e.target.className.split(' ')[1];
321+
if(evt==='prev'||evt==='next'){
322+
this[evt]();
323+
}
324+
break;
325+
default:
326+
break;
327+
}
216328
}
217-
218-
function transition(element,time){
219-
element.style['-webkit-transition-duration'] = time+'ms';
220-
element.style['transition-duration'] = time+'ms';
221-
}
222329

223330
if(!noGlobal){
224331
window.Slider = Slider;

0 commit comments

Comments
 (0)