-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.fullpage.js
107 lines (95 loc) · 2.53 KB
/
jquery.fullpage.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
/*
* created by zhangkai on 2016/10/10
*/
;(function(global,$){
'use strict';
var Fullpage = (function(){
function Fullpage (el,options) {
this.$el = el;
this.currIndex = 0;
this.animating = false;
this.init();
}
var utils = {
throttle:function(callback,delayTime,maxTime){
var timer = null;
var prevTime = 0;
return function(){
var context = this;
var argument = arguments;
var currTime = Date.now();
if(maxTime && currTime - prevTime >= maxTime){
prevTime = currTime;
callback.apply(context,argument);
}else{
if(timer) clearTimeout(timer);
timer = setTimeout(function(){
callback.apply(context,argument);
},delayTime);
}
}
}
};
Fullpage.prototype = {
constructor:Fullpage,
init:function(){
this.initHTML();
this.bindEvent();
},
initHTML:function(){
this.$el.children().css({
'height':'100vh',
'transition':'all 0.7s',
'-webkit-transition':'all 0.7s'
});
},
bindEvent:function(){
var that = this;
var targetIndex,x0,y0,xDiff,yDiff,delta;
$(window).on('wheel DOMMouseScroll',utils.throttle(function(){
var e = arguments[0].originalEvent;
delta = e.wheelDelta?e.wheelDelta:-e.detail;
targetIndex = that.currIndex + (delta>0?-1:1);
that.gotoTarget(targetIndex);
},100));
this.$el.on('touchstart',function(e){
x0 = e.touches[0].clientX;
y0 = e.touches[0].clientY;
});
this.$el.on('touchmove',utils.throttle(function(){
console.log('move')
var e = arguments[0];
if (!x0 || !y0) return;
xDiff = e.touches[0].clientX - x0;
yDiff = e.touches[0].clientY - y0;
targetIndex = that.currIndex + (yDiff>0?-1:1);
that.gotoTarget(targetIndex);
},16));
},
gotoTarget:function(targetIndex){
var children = this.$el.children();
var that = this;
var translateY;
if(this.animating || targetIndex<0 || targetIndex>this.$el.children().length-1) return;
translateY= 'translateY(-'+targetIndex*100+'%)';
this.animating = true;
$(children[0]).on('transitionend', function callback() {
this.removeEventListener('transitionend', callback);
that.animating = false;
});
children.css({
'transform':translateY,
'-webkit-transform':translateY
});
this.currIndex = targetIndex;
}
};
return Fullpage;
})();
$.fn.fullpage = function(options){
this.each(function(){
new Fullpage($(this),options);
});
return this;
};
})(this,this.jQuery);