-
Notifications
You must be signed in to change notification settings - Fork 1
/
slide-jquery.js
136 lines (111 loc) · 3.29 KB
/
slide-jquery.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
127
128
129
130
131
132
133
134
135
136
;(function($) {
'use strict';
var Slide = function(ele, options) {
this.$element = ele;
this.interval = null;
this.current = 0;
this.defaults = {
speed: 100,
second: 3000,
showMarker: false,
marker: false,
showController: false,
setClass: {
marker: '',
active: '',
unactive: '',
controllerPrev: '',
controllerNext: ''
}
};
this.config = $.extend({}, this.defaults, options);
// showMarker
this.showMarker = this.config.showMarker;
this.mark = this.config.marker;
this.marker = this.config.setClass.marker;
this.active = this.config.setClass.active;
this.unactive = this.config.setClass.unactive;
// show left and right controller
this.showController = this.config.showController;
this.controllerPrev = this.config.setClass.controllerPrev;
this.controllerNext = this.config.setClass.controllerNext;
// initialize
this.init();
this.handleEvent();
};
Slide.prototype = {
init: function() {
this.$oUl = $('ul', this.$element);
this.$oULi = $('li', this.$oUl[0]);
this.width = this.$oULi.outerWidth(true);
this.number = this.$oULi.length;
this.$oUl.css('width', this.number * this.width);
// marker
if (this.showMarker) {
var oOlis = [],html;
for (var i = 1; i <= this.number; i++) {
if (i === 1) {
this.mark ? oOlis.push('<li class="' + this.active + '">' + i + '<\/li>') : oOlis.push('<li class="' + this.active + '">' + "" + "<\/li>");
} else {
this.mark ? oOlis.push('<li class="' + this.unactive + '">' + i + '<\/li>') : oOlis.push('<li class="' + this.unactive + '">' + "" + "<\/li>");
}
}
html = '<ol class="' + this.marker + '">' + oOlis.join('') + '</ol>';
this.$element.append(html);
this.$oOl = $('ol', this.$element);
}
// controller
if (this.showController) {
html = '<span class="' + this.controllerPrev + '"></span>';
html += '<span class="' + this.controllerNext + '"></span>';
this.$element.append(html);
}
},
handleEvent: function() {
var that = this;
this.start();
this.$element.hover(this.stop.bind(this), this.start.bind(this));
if (this.showMarker) {
this.$oOl.find('li').hover(function() {
that.move($(this).index());
});
}
if (this.showController) {
this.$element.find('span').eq(0).click(function() {
that.prev();
});
this.$element.find('span').eq(1).click(function() {
that.next();
});
}
},
start: function() {
this.interval = setInterval(this.next.bind(this),this.config.second);
},
stop: function() {
clearInterval(this.interval);
},
prev: function() {
this.move(this.current - 1);
},
next: function() {
this.move(this.current + 1);
},
move: function(index) {
if (index < 0) index = (this.number - 1);
if (index >= this.number) index = 0;
var left = -index * this.width;
this.current = index;
if (this.showMarker) {
this.$oOl.find('li:eq(' + index + ')').removeClass().addClass(this.active).siblings().removeClass().addClass(this.unactive);
}
this.$oUl.stop().animate({left: left}, this.config.speed);
}
};
$.fn.slide = function(opt) {
return this.each(function() {
var $this = $(this);
new Slide($this, opt);
});
};
})(window.jQuery);