forked from Huxpro/jquery.HSlider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.HSlider.js
executable file
·229 lines (193 loc) · 6.12 KB
/
jquery.HSlider.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* ===========================================================
* jQuery-HSlider.js v2
* ===========================================================
* Copyright 2015 Hux.
*
* Create an photo-first, fullpage web slider
* All animation is powered in CSS3
* Only for modern broswer
*
* ========================================================== */
(function($){
var defaults = {
easing:"ease",
animationTime:1300,
pagination:true,
};
/*------------------------------------------------*/
/* Credit: Eike Send for the awesome swipe event */
/*------------------------------------------------*/
$.fn.swipeEvents = function() {
return this.each(function() {
var startX,
startY,
$this = $(this);
$this.bind('touchstart', touchstart);
function touchstart(event) {
var touches = event.originalEvent.touches;
if (touches && touches.length) {
startX = touches[0].pageX;
startY = touches[0].pageY;
$this.bind('touchmove', touchmove);
}
//event.preventDefault();
}
function touchmove(event) {
var touches = event.originalEvent.touches;
if (touches && touches.length) {
var deltaX = startX - touches[0].pageX;
var deltaY = startY - touches[0].pageY;
if (deltaX >= 50) {
$this.trigger("swipeLeft");
}
if (deltaX <= -50) {
$this.trigger("swipeRight");
}
if (deltaY >= 50) {
$this.trigger("swipeUp");
}
if (deltaY <= -50) {
$this.trigger("swipeDown");
}
if (Math.abs(deltaX) >= 50 || Math.abs(deltaY) >= 50) {
$this.unbind('touchmove', touchmove);
}
}
event.preventDefault();
}
});
};
/* swipe event End */
/*------------------------------------------------*/
$.fn.HSlider = function(options){
var settings = $.extend({}, defaults, options),
$slider = $(this),
sections = $("section"),
total = sections.length,
quiet = false,
paginationList = "";
$.fn.transformPage = function(settings,pos){
$(this).css({
"-webkit-transform": "translate3d(" + pos + "%, 0 ,0)",
"-webkit-transition": "all " + settings.animationTime + "ms " + settings.easing,
"-moz-transform": "translate3d(" + pos + "%, 0 ,0)",
"-moz-transition": "all " + settings.animationTime + "ms " + settings.easing,
"-ms-transform": "translate3d(" + pos + "%, 0 ,0)",
"-ms-transition": "all " + settings.animationTime + "ms " + settings.easing,
"transform": "translate3d(" + pos + "%, 0 ,0)",
"transition": "all " + settings.animationTime + "ms " + settings.easing
});
return $(this);
}
$.fn.slideLeft = function(){
var _index = $("section.active").data("index");
if (_index < total) location.hash = '#'+ (_index + 1);
return $(this);
}
$.fn.slideRight = function(){
var _index = $("section.active").data("index");
if (_index <= total && _index > 1) location.hash = '#'+ (_index - 1);
return $(this);
}
// seperate RENDER, support URL hash
$.fn._render = function(){
var _hash = Math.floor(Number(location.hash.split('#')[1])); // get hash, do type cast
_hash = _hash ? _hash : 1; // undefined, 0, NaN
if(_hash < 1) _hash = 1; // prevent pre overflow
if(_hash > total) _hash = total; // prevent post overflow
var _activeIndex = _hash;
// reset current
$("section.active").removeClass("active");
$(".pagination li a" + ".active").removeClass("active");
// activate new
$("section[data-index=" + _activeIndex + "]").addClass("active");
$(".pagination li a" + "[data-index=" + _activeIndex + "]").addClass("active");
// calculate pos and transform
var pos = ((_activeIndex - 1) * 100) * -1;
$slider.transformPage(settings, pos);
return $(this);
}
$.fn._renderPagination = function(){
if(!settings.pagination) return;
// Create pagination
$("<ul class='pagination'>" + paginationList + "</ul>").prependTo("body");
posTop = ($slider.find(".HSlider").height() / 2) * -1;
$slider.find(".HSlider").css("margin-top", posTop);
// bind click event
$(".pagination li a").click(function (){
var page_index = $(this).data("index");
location.hash = '#' + page_index;
});
}
$.fn._bindEvent = function(){
// HashChange
$(window).on('hashchange', $slider._render);
//Mousewheel MouseScroll
$(document).bind('mousewheel DOMMouseScroll', function(event) {
event.preventDefault();
var delta = event.originalEvent.wheelDelta || -event.originalEvent.detail;
$slider._handleMouseScroll(event, delta);
});
//Touch
$slider.swipeEvents().bind("swipeLeft",function(){
$slider.slideLeft();
}).bind("swipeRight",function(){
$slider.slideRight();
});
return $(this);
}
$.fn._handleMouseScroll = function(event, delta){
if (quiet == false) {
if (delta == 0) return; // early return
if (delta < 0) {
$slider.slideLeft()
} else {
$slider.slideRight()
};
quiet = true;
// deal with OSX inertia scroll
setTimeout(function(){
quiet = false;
}, Number(settings.animationTime) + 100); // make sure Number
}
}
$.fn._initStyle = function(){
$slider.addClass("HSlider").css({
"position":"relative",
width:"100%",
height:"100%",
});
$.each(sections,function(i){
$(this).css({
position:"absolute",
width:"100%",
height:"100%",
left:i*100 +"%"
}).addClass("section").attr("data-index", i+1);
$(this).find("img").css({
minWidth: "100%",
minHeight: "100%",
position:"absolute",
zIndex:1
})
$(this).find("article").css({
position:"absolute",
boxSizing:"border-box",
width:"100%",
bottom:0,
zIndex:4
})
if(settings.pagination == true) {
paginationList += "<li><a data-index='"+(i+1)+"' href='#" + (i+1) + "'></a></li>"
}
});
return $(this);
}
// create slider
$slider
._initStyle()
._bindEvent()
._render()
._renderPagination()
}
})(window.jQuery);