-
Notifications
You must be signed in to change notification settings - Fork 1
/
parallax.js
71 lines (60 loc) · 1.93 KB
/
parallax.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
/**
new Parallax({
element : '.parallax', //元素
direction : 'forward' //方向 forward:正向, reverse:反向
})
*/
//参数
var defaults ={
element : '.parallax', //元素
direction : 'reverse' //方向 forward:正向, reverse:反向
}
function Parallax(){
this.init.apply(this, arguments)
}
Parallax.prototype={
init : function(options){
this.options=$.extend(true,{},defaults,options);
this._bindEvent();
},
_direction : function(e){
var obj ={};
var w = $(window).width();
var h = $(window).height();
if(this.options.direction == 'reverse'){
obj.offsetX = 0.5 - e.pageX / w;
obj.offsetY = 0.5 - e.pageY / h;
}else if( this.options.direction == 'forward'){
obj.offsetX = 0.5 + e.pageX / w;
obj.offsetY = 0.5 + e.pageY / h;
}
return obj;
},
_bindEvent : function(){
var element = this.options.element;
var _this = this;
$(window).on('mousemove._parallax', function(e) {
var dirObj = _this._direction(e);
var offsetX = dirObj.offsetX;
var offsetY = dirObj.offsetY;
$(element).each(function(i, el) {
var offset = parseInt($(el).data('offset'));
var translate = "translate3d(" + Math.round(offsetX * offset) + "px," + Math.round(offsetY * offset) + "px, 0px)";
$(el).css({
'-webkit-transform': translate,
'transform': translate,
'moz-transform': translate
});
});
});
$(window).on('mousemove', function(e) {
console.log(e);
})
},
destory : function(){
$(window).off('._parallax');
}
}
if ( typeof dwfis !== "undefined" && typeof dwfis.define === "function") {
module.exports = Parallax;
}