-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallax.js
45 lines (45 loc) · 1.41 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
(function (jQuery) {
var jQuerywindow = jQuery(window);
var windowHeight = jQuerywindow.height();
jQuerywindow.resize(function () {
windowHeight = jQuerywindow.height();
});
jQuery.fn.parallax = function (xpos, speedFactor, outerHeight) {
var jQuerythis = jQuery(this);
var getHeight;
var firstTop;
var paddingTop = 0;
jQuerythis.each(function () {
firstTop = jQuerythis.offset().top;
});
if (outerHeight) {
getHeight = function (jqo) {
return jqo.outerHeight(true);
};
} else {
getHeight = function (jqo) {
return jqo.height();
};
}
if (arguments.length < 1 || xpos === null) xpos = "50%";
if (arguments.length < 2 || speedFactor === null) speedFactor = 0.1;
if (arguments.length < 3 || outerHeight === null) outerHeight = true;
function update() {
var pos = jQuerywindow.scrollTop();
jQuerythis.each(function () {
var jQueryelement = jQuery(this);
var top = jQueryelement.offset().top;
var height = getHeight(jQueryelement);
if (top + height < pos || top > pos + windowHeight) {
return;
}
jQuerythis.css(
"backgroundPosition",
xpos + " " + Math.round((top - pos) * speedFactor) + "px"
);
});
}
jQuerywindow.bind("scroll", update).resize(update);
update();
};
})(jQuery);