-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslideJQuery2.js
More file actions
166 lines (156 loc) · 3.71 KB
/
slideJQuery2.js
File metadata and controls
166 lines (156 loc) · 3.71 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
function slideFn( slideIds ){
for(var i in slideIds){
this[i] = slideIds[i];
}
this.temp = 0;
this.liLength = 0;
this.liWidth = 0;
this.timer = null;
this.init();
}
slideFn.prototype = {
init:function(){
var _this = this;
_this.ulDom(_this.uls);
_this.rightBtnFn();
_this.pointerBtnFn();
_this.slideInterval();
_this.slideMove();
_this.leftBtnFn();
},
ulDom:function( _ulId ){
//创建ulDOM结构
var _this = this;
var chi = ' ';
_this.createDom( _ulId );//创建ul里面的li
chi = _ulId.children();//li
_this.liLength = chi.length;//li个数
_this.liWidth = chi.eq(0).width();//li单个宽度
_ulId.width(_this.liLength * _this.liWidth);//ul的宽度
_this.createIconBtn( _this.liLength );//调用生成小圆点的方法
_this.pointerWrap( _this.liLength );//调用设置小圆点父容器位置的方法
_this.pointerBtnFn( );//小圆点添加事件
},
createDom:function( _ulId ){
//生成li
var _this = this;
var imgs = _this.getDate();
for (var i = 0; i < imgs.length; i++) {
$('<li/>')
.html('<img src = '+ imgs[i] +' />')
.appendTo( _ulId );
};
},
getDate:function(){
//获取图片
var _this = this;
var _urls = slideImgUrl.urls;
return _urls;
},
leftBtnFn:function(){
//左按钮
var _this = this;
_this.leftMove.on('click',function(){
// if(_this.temp < _this.liLength - 1){
// _this.temp ++;
// }else{
// _this.temp = 0;
// }
// slideAnimate(_this.uls,_this.temp,_this.liWidth);//调用动画的公共方法
// _this.iconStyle();//小圆点样式
_this.slideMove();
_this.slideInterval();
});
},
rightBtnFn:function(){
//右按钮
var _this = this;
_this.rightMove.on('click',function(){
if(_this.temp > 0 ){
_this.temp --;
}else{
_this.temp = _this.liLength - 1;
}
slideAnimate(_this.uls,_this.temp,_this.liWidth);//调用动画的公共方法
_this.iconStyle();//小圆点样式
_this.slideInterval();
});
},
createIconBtn:function( _liNum ){
//生成小圆点
var _this = this;
for (var i = 0; i < _liNum; i++) {
$('<p></p>',{}).appendTo( _this.slidePoint);
};
$('p:first-child').addClass('redP');
},
pointerWrap:function( _liNum ){
//小圆点容器
var _this = this;
var _iconWrapWidth = _liNum * 30;
_this.slidePoint.width( _iconWrapWidth );
_this.slidePoint.css('margin-left',-_iconWrapWidth / 2);
},
pointerBtnFn:function(){
//小圆点添加事件
var _this = this;
var _icons = _this.slidePoint.find('p');
_icons.on('click',function(){
_this.temp = $(this).index();
slideAnimate(_this.uls,_this.temp,_this.liWidth);
_this.iconStyle();
});
},
iconStyle:function(){
//给小圆点添加样式
var _this = this;
var _icons = _this.slidePoint.find('p');
_icons.eq(_this.temp)
.addClass('redP')
.siblings()
.removeClass('redP');
},
slideMove:function(){
//自动移动
var _this = this;
if(_this.temp < _this.liLength - 1){
_this.temp ++;
}else{
_this.temp = 0;
}
slideAnimate(_this.uls,_this.temp,_this.liWidth);//调用动画的公共方法
_this.iconStyle();//小圆点样式
_this.slideInterval();
},
slideInterval:function(){
//定时器
var _this = this;
clearInterval(_this.timer);
_this.timer = setInterval(function(){
_this.slideMove();
},1500);
}
}
var slideImgUrl = {
urls:[
'temp2.jpg',
'temp3.jpg',
'temp1.jpg',
'temp1.jpg',
'temp2.jpg',
'temp3.jpg'
]
}
var slideIds = {
uls : $('#uls'),//ul容器
slidePoint : $('#slidePoint'),//小圆点父容器
leftMove : $('#leftMove'),//左
rightMove : $('#rightMove')//右
}
//动画向左移动公共方法
function slideAnimate(ele,i,n,time){
ele.stop().animate({
left: - i * n
},500);
}
new slideFn( slideIds );