-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathjquery.enllax.js
148 lines (131 loc) · 5.04 KB
/
jquery.enllax.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
/**
* jQuery.enllax.js v1.1.0
* https://github.com/mmkjony/enllax.js
* demo: http://mmkjony.github.io/enllax.js/
*
* Copyright 2015, MMK Jony
* This content is released under the MIT license
**/
(function($){
'use strict';
$.fn.enllax = function(opt){
var elem = $('[data-enllax-ratio]');
elem.each(function(){
var $this = $(this);
$this.css({
'position': 'absolute',
'left': '0',
'right': '0'
});
});
var winHeight = $(window).height();
var options = $.extend({
ratio: 0,
type: 'background', //foreground
direction: 'vertical', //horizontal
offset: 0
}, opt);
elem.each(function(){
var ratio;
var type;
var dir;
var offset;
var $this = $(this);
var docHeight = $(document).height();
var offsettop = $this.offset().top;
var height = $this.outerHeight();
var dataRat = $this.data('enllax-ratio');
var dataType = $this.data('enllax-type');
var dataDir = $this.data('enllax-direction');
var dataOffset = $this.data('enllax-offset');
if(dataRat) {
ratio = dataRat;
}
else { ratio = options.ratio; }
if(dataType) {
type = dataType;
}
else { type = options.type; }
if(dataDir) {
dir = dataDir;
}
else { dir = options.direction; }
if(dataOffset) {
offset = dataOffset;
}
else { offset = options.offset; }
var bgY = Math.round(offsettop * ratio);
var transform = Math.round( ( offsettop - ( winHeight / 2 ) ) * ratio - offset );
if(type == 'background') {
if(dir == 'vertical') {
$this.css({
'background-position': 'center ' + -bgY + 'px'
});
}
else if(dir == 'horizontal') {
$this.css({
'background-position': -bgY + 'px' + ' center'
});
}
}
else if(type == 'foreground') {
if(dir == 'vertical') {
$this.css({
'-webkit-transform': 'translateY(' + transform + 'px)',
'-moz-transform': 'translateY(' + transform + 'px)',
'transform': 'translateY(' + transform + 'px)',
'position': 'absolute',
'left': '0',
'right': '0'
});
}
else if(dir == 'horizontal') {
$this.css({
'-webkit-transform': 'translateX(' + transform + 'px)',
'-moz-transform': 'translateX(' + transform + 'px)',
'transform': 'translateX(' + transform + 'px)'
});
}
}
$(window).on('scroll', function(){
var docHeight = $(document).height();
var offsettop = $this.offset().top;
var height = $this.outerHeight();
var scrolling = $(this).scrollTop();
bgY = Math.round((offsettop - scrolling) * ratio);
transform = Math.round( ( ( offsettop - ( winHeight / 2 ) ) - scrolling ) * ratio - offset );
if(type == 'background') {
if(dir == 'vertical') {
$this.css({
'background-position': 'center ' + -bgY + 'px'
});
}
else if(dir == 'horizontal') {
$this.css({
'background-position': -bgY + 'px' + ' center'
});
}
}
else if((type == 'foreground') && (scrolling < docHeight)) {
if(dir == 'vertical') {
$this.css({
'-webkit-transform': 'translateY(' + transform + 'px)',
'-moz-transform': 'translateY(' + transform + 'px)',
'transform': 'translateY(' + transform + 'px)',
'position': 'absolute',
'left': '0',
'right': '0'
});
}
else if(dir == 'horizontal') {
$this.css({
'-webkit-transform': 'translateX(' + transform + 'px)',
'-moz-transform': 'translateX(' + transform + 'px)',
'transform': 'translateX(' + transform + 'px)'
});
}
}
});
});
};
})(jQuery);