-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjquery.backgroundparallax.js
56 lines (45 loc) · 1.39 KB
/
jquery.backgroundparallax.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
(function($)
{
// This script was written by Steve Fenton
// http://www.stevefenton.co.uk/Content/Jquery-Background-Parallax/
// Feel free to use this jQuery Plugin
// Version: 3.0.3
// Contributions by:
$.fn.backgroundparallax = function (settings) {
var config = {
intensity: 0.5
};
if (settings) {
$.extend(config, settings);
}
function PerformScroll($Scroll, $Element) {
// Calculate the movement based on scroll and intensity, round to 2dp
var leftDistance = Math.round((($Scroll.scrollLeft() * config.intensity) * -1) * 100) / 100;
var topDistance = Math.round((($Scroll.scrollTop() * config.intensity) * -1) * 100) / 100;
// Scroll the background
$Element.css({ backgroundPosition: leftDistance + "px " + topDistance + "px" });
}
return this.each(function () {
// Make sure we are dealing with a flot
config.intensity = parseFloat(config.intensity);
// Ensure intensity remains sensible
if (config.intensity < 0.1) {
config.intensity = 0.1;
} else if (config.intensity > 3) {
config.intensity = 3;
}
var nodeName = this.nodeName.toLowerCase();
var $This = $(this);
if (nodeName == "body") {
$This = $(window);
}
$This.scroll(function (e) {
if (nodeName == "body") {
PerformScroll($("html"), $("body"));
} else {
PerformScroll($This, $This);
}
});
});
};
})(jQuery);