-
Notifications
You must be signed in to change notification settings - Fork 1
/
parallax.js
executable file
·69 lines (59 loc) · 1.58 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
// parallax.js
//
// O2 WEB
// o2web.ca
// Tous droits réservés
// All rights reserved
// 2015
//
// DEFINE MODULE
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// Define AMD module
define(['jquery', 'raf', 'scrollEvents'], factory);
} else {
// JQUERY INIT
factory(jQuery);
}
}
//
//
// MAIN CODE
(this, function($){
$win = $(window);
// global variable for parallax.js
window.parallax = {};
parallax.items = [];
parallax.options = {
perspective: 200,
transform: Modernizr.prefixed('transform')
}
// jquery function
$.fn.parallax = function(args, options){
$selection = $(this);
// Pass methods directly to scrollevents
var type = typeof(args);
if(type == 'string'){ return $(this).scrollEvents(args, options) }
// set scrollEvents
$selection.each(function(){
var $el = $(this);
var el = $el[0];
el.z = parseFloat($el.attr('z'));
$el.scrollEvents({
flag: 'parallax',
round: 1000,
offset: el.z ? (parallax.options.perspective * Math.abs(el.z)) : 0,
offsetBottom: el.z ? (parallax.options.perspective * Math.abs(el.z)) : 0,
travel: function(e){
var el = e.data.selection[0];
var z = el.z;
var delta = z>0 ? 1 - e.data.delta() : e.data.delta();
var travel = (parallax.options.perspective* Math.abs(z) )
var y = Math.round((delta*travel) - (travel/2));
el.style[parallax.options.transform] = 'translate3d(0,'+y+'px, 0)';
}
});
return $selection;
})
}
}));