-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloopScroll.js
executable file
·126 lines (110 loc) · 3.78 KB
/
loopScroll.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
(function($){
var loopScroll = function(content, options){
var $u = $('ul',content),
$first = $u.find('li:first').clone(),
$last = $u.find('li:last').clone(),
$second = $u.find('li:eq(1)').clone();
$u.append($first)
$u.prepend($last)
$u.append($second)
var $l = $('li',$u).css('float','left');
var params = {
'$u' : $u,
'$l' : $l,
'len' : $l.length,
'loaded' : true,
'i' : 1,
'$i' : $(options.index, content)
}
this.options = $.extend({}, params, options);
this.init()
}
loopScroll.prototype = {
init : function(){
var options = this.options, me = this;
options.$l.find('img').load(function(){
if(options.loaded){
var w = options.w = options.$l.width();
options.$u.width( w * options.len ).css( 'left', -w );
options.$l.css('opacity',0.5).eq(1).css('opacity',1);
me.run();
options.loaded = false;
}
})
this.bind()
},
run : function(){
var options = this.options, me = this;
options.cle && clearInterval(options.cle);
options.cle = setInterval(function(){
options.i++
if( options.i >= options.len-1){
options.i = 1
options.$u.css('left',-options.w);
arguments.callee();
}else{
scrollImg.call( options );
}
},options.speed);
},
bind : function(){
var options = this.options, me = this;
if( options.mouseWheel ){
options.$u.mouseover( function(){ clearInterval(options.cle) } )
.mouseout( function(){ me.run() } )
}
$(options.btnNext).click($.proxy( nextHandle, options ))
$(options.btnPrev).click($.proxy( prevHandle, options ))
//$(options.btnNext).mouseout( function(){ me.run() } )
//$(options.btnPrev).mouseout( function(){ me.run() } )
}
}
var nextHandle = function(){
clearInterval(this.cle)
this.$u.stop(true, false)
if(this.i == (this.len-2)){
this.i = 1;
this.$u.css('left',-this.w);
}
//console.log(new Date().getSeconds())
this.i++;
scrollImg.call( this )
return false;
}
var prevHandle = function(){
clearInterval(this.cle)
this.$u.stop(true, false)
if( this.i == 1 ){
this.i = this.len-2;
this.$u.css('left',-this.w*(this.i))
}
this.i--;
scrollImg.call( this )
return false;
}
var scrollImg = function(){
this.$l.eq(this.i-1).animate({'opacity':0.5},500)
this.$l.eq(this.i).animate({'opacity':1},500);
this.$u.stop(true, true).animate( { 'left': -this.w*this.i }, 800,function(){
});
this.$i.find('a').eq(this.i-1).addClass( this.index_class )
.siblings().removeClass( this.index_class );
//this.i == this.len-1 && $('a:eq(0)',this.$i).addClass( this.index_class ).siblings().removeClass( this.index_class );
}
$.fn.loopScroll = function(option){
var defaults = {
btnNext : ".next_btn",
btnPrev : ".pre_btn",
index : ".img_count",
index_class : 'active',
mouseWheel : true,
speed : 4000
}
return this.each(function(){
var $this = $(this)
,data = $this.data('loopScroll')
,options = $.extend({}, defaults, $this.data(), typeof option == 'object' && option);
if (!data) $this.data('loopScroll', (data = new loopScroll(this, options)))
})
}
})(jQuery)