slider.js
6.38 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* Created by 韩麒麟 on 2015/9/25.
*/
/*
*参数说明
*
*
* width: 500,-- 宽度
* height: 300,-- 高度
* autoPlay: true,--定时播放
* isShowNextAndPrev: true,--显示上一个、下一个按钮
* isShowTitle: true,--显示标题
* isShowControls: true,--显示控制
* data: [{
"imgUrl": "img/1.png",
"link": "链接",
"title": "标题"
}],--数据
* speed: 600--动画时间/毫秒
*
*
*/
/* 创建一个闭包 */
;
(function ($) {
//对象
$.Slider = function (element, options) {
var self = this;
this.$element = element;
this.defaults = {
width: 500,
height: 300,
autoPlay: true,
isShowNextAndPrev: true,
isShowTitle: true,
isShowControls: true,
data: [],
speed: 600
};
this.init = function () {
this.opts = $.extend({}, this.defaults, options || {});
if (!this.opts.data.length) {
alert("数据获取异常!");
return;
}
this.renderHtml();
}
//初始化 渲染Dom
this.init();
this.prev = this.$element.find(".prev");
this.next = this.$element.find(".next");
this.ul = this.$element.find("ul");
this.index = 0;
this.isAnimate = false;
this.arrLi = this.ul.find("li");
this.arrImg = this.ul.find("img");
this.arrTitle = this.$element.find("p");
this.size = this.arrImg.size();
this.timer = null;
this.controls = this.$element.find(".controls");
this.controlsA = this.controls.find('a');
this.setValues();
this.firstLi = this.arrLi.first();
this.lastILi = this.arrLi.last();
this.posWidth = this.arrLi.width();
//Show Title
if (!this.opts.isShowTitle) {
this.arrTitle.hide();
}
//Show Next Prev
if (!this.opts.isShowNextAndPrev) {
this.prev.hide();
this.next.hide();
}
if (!this.opts.isShowControls) {
this.controls.hide();
}
this.next.click(function () {
self.npClick("next");
});
this.prev.click(function () {
self.npClick("prev");
});
this.controlsA.click(function () {
self.controlsClick($(this).index());
});
//autoPlay
if (this.opts.autoPlay) {
this.autoPlay();
this.$element.hover(function () {
clearInterval(self.timer);
}, function () {
self.autoPlay();
});
}
}
//原型
$.Slider.prototype = {
autoPlay: function () {
var self = this;
clearInterval(this.timer);
this.timer = setInterval(function () {
self.npClick("next");
}, 2000);
},
controlsClick: function (index) {
this.index = index;
this.move();
},
npClick: function (direction) {
if (!this.isAnimate) {
this.isAnimate = !this.isAnimate;
direction == "prev" ? this.index-- : this.index++;
this.move();
} else {
return;
}
},
showA: function () {
var index = this.index;
if (index < 0) {
index = this.size - 1;
}
if (index >= this.size) {
index = 0;
}
this.controlsA.eq(index).addClass("active").siblings().removeClass("active");
if (this.opts.isShowTitle) {
this.arrTitle.html(this.opts.data[index].title);
}
},
move: function () {
var self = this;
if (this.index < 0) {
this.lastILi.css({position: "absolute", left: -this.posWidth});
}
if (this.index >= this.size) {
this.firstLi.css({position: "relative", left: this.index * this.posWidth});
}
$(this.ul).animate({left: -this.index * this.posWidth},
this.opts.speed,
function () {
if (self.index < 0) {
self.index = self.size - 1;
self.lastILi.css({position: "relative", left: ""});
self.ul.css({left: -self.index * self.posWidth});
}
if (self.index >= self.size) {
self.index = 0;
self.firstLi.css({position: "relative", left: ""});
self.ul.css({left: 0});
}
self.isAnimate = false;
});
this.showA();
},
renderHtml: function () {
var strHtml =
'<ul></ul>'
+ '<div class="controls">'
+ '</div>'
+ '<a href="javascript:;" class="arrow prev"><</a>'
+ '<a href="javascript:;" class="arrow next">></a>'
+ '<p></p>';
this.$element.append(strHtml);
var strImg = "",
strA = "";
for (var obj in this.opts.data) {
var object = this.opts.data[obj];
//strImg += "<li><a href='" + object.link + "'><img src='" + object.imgUrl + "'/><p>" + object.title + "</p></a></li>";
strImg += "<li><a href='" + object.link + "'><img src='" + object.imgUrl + "'/></a></li>";
strA += "<a href='javascript:;'></a>";
}
this.$element.find("ul").append(strImg).next().append(strA);
},
setValues: function () {
this.$element.css({width: this.opts.width, height: this.opts.height});
this.arrImg.css({width: this.opts.width, height: this.opts.height});
this.ul.css({width: this.size * this.opts.width});
this.controlsA.first().addClass("active");
this.arrTitle.html(this.opts.data[this.index].title);
}
}
//插件
$.fn.slider = function (options) {
return this.each(function () {
if (undefined == $(this).data('slidePlugin')) {
var slidePlugin = new $.Slider($(this), options);
$(this).data('slidePlugin', slidePlugin);
}
})
}
})(jQuery);