-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallax.js
46 lines (29 loc) · 1.03 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
(function ($) {
$.fn.parallax = function () {
var varWidthWindow = $(window).width();
if (varWidthWindow < 768)
{
$(this).css('background-position', "");
return;
}
$(this).each(function () {
var $obj = $(this);
console.log(this);
$(window).scroll(function () {
var varTopScroll = $(window).scrollTop();
var varTopElement = $obj.offset().top;
var varHeightWindow = $(window).height();
var varElementVisibilityStartPoint = varTopElement - varHeightWindow;
varElementVisibilityStartPoint = (varElementVisibilityStartPoint < 0) ? 0 : varElementVisibilityStartPoint;
if (varTopElement + varHeightWindow < varTopScroll || varTopElement > varTopScroll + varHeightWindow) {
/* console.log("Out of view"); */
return;
}
var yPos = -((varTopScroll - varElementVisibilityStartPoint) * $obj.data('speed'));
var bgpos = '50% ' + yPos + 'px';
$obj.css('background-position', bgpos);
});
});
return this;
};
}(jQuery));