-
Notifications
You must be signed in to change notification settings - Fork 28
/
jquery.scrollParallax.js
157 lines (138 loc) · 5.48 KB
/
jquery.scrollParallax.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
149
150
151
152
153
154
155
156
157
/*!
* Copyright (c) 2010 Brandon Aaron (http://brandonaaron.net)
* backgroundPosition cssHook for jquery. Necessary to combat different css property names between browsers
* https://github.com/brandonaaron/jquery-cssHooks
* Licensed under the MIT License (LICENSE.txt).
*/
(function($) {
// backgroundPosition[X,Y] get hooks
var $div = $('<div style="background-position: 3px 5px">');
$.support.backgroundPosition = $div.css('backgroundPosition') === "3px 5px" ? true : false;
$.support.backgroundPositionXY = $div.css('backgroundPositionX') === "3px" ? true : false;
$div = null;
var xy = ["X","Y"];
// helper function to parse out the X and Y values from backgroundPosition
function parseBgPos(bgPos) {
var parts = bgPos.split(/\s/),
values = {
"X": parts[0],
"Y": parts[1]
};
return values;
}
if (!$.support.backgroundPosition && $.support.backgroundPositionXY) {
$.cssHooks.backgroundPosition = {
get: function( elem, computed, extra ) {
return $.map(xy, function( l, i ) {
return $.css(elem, "backgroundPosition" + l);
}).join(" ");
},
set: function( elem, value ) {
$.each(xy, function( i, l ) {
var values = parseBgPos(value);
elem.style[ "backgroundPosition" + l ] = values[ l ];
});
}
};
}
if ($.support.backgroundPosition && !$.support.backgroundPositionXY) {
$.each(xy, function( i, l ) {
$.cssHooks[ "backgroundPosition" + l ] = {
get: function( elem, computed, extra ) {
var values = parseBgPos( $.css(elem, "backgroundPosition") );
return values[ l ];
},
set: function( elem, value ) {
var values = parseBgPos( $.css(elem, "backgroundPosition") ),
isX = l === "X";
elem.style.backgroundPosition = (isX ? value : values[ "X" ]) + " " +
(isX ? values[ "Y" ] : value);
}
};
$.fx.step[ "backgroundPosition" + l ] = function( fx ) {
$.cssHooks[ "backgroundPosition" + l ].set( fx.elem, fx.now + fx.unit );
};
});
}
})(jQuery);
/*!
* Scroll-based parallax plugin for jQuery
* Copyright (c) 2011 Dave Cranwell (http://davecranwell.com)
* Licensed under the MIT License.
* 2011-05-18
* version 1.0
*/
(function($){
$.fn.scrollParallax = function(options) {
var settings = {
'speed': 0.2,
'axis': 'x,y',
'debug': false
}
function debug(msg){
if(settings.debug && 'console' in window && 'log' in window.console){
console.log(msg);
}
}
return this.each(function() {
//defined accessible $this var in standard way for use within functions
var $this = $(this),
//timestamp,last position for the interval & FPS ~= 30
ts,FPS = 34,lastPos = {x:0,y:0},intervalActive,
//find current position so parallax can be relative to it
currentPosArray=$this.css("backgroundPosition").split(" "),
currentXPos=parseInt(currentPosArray[0].replace(/[^0-9\-]/g, "")),
currentYPos=parseInt(currentPosArray[1].replace(/[^0-9\-]/g, ""));
//extend options in standard way
if (options) {
$.extend(settings, options);
}
$this.bind('inview', function (event, visible) {
if (visible == true) {
$this.addClass("inview");
debug("in view");
}else{
$this.removeClass("inview");
debug("out of view");
}
});
function updateElemPos(){
var newXPos,newYPos,
offset = $this.offset();
//calculate new position
newXPos = (settings.axis.match(/x/))?
parseInt((-(offset.left - $(window).scrollLeft()) * settings.speed) + currentXPos) + "px":
currentPosArray[0];
newYPos = (settings.axis.match(/y/))?
parseInt((-(offset.top - $(window).scrollTop()) * settings.speed) + currentYPos) + "px":
currentPosArray[1];
if(typeof intervalActive == 'undefined'){
lastPos.x = newXPos;
lastPos.y = newYPos;
}
intervalActive = true;
debug("new X position: "+ newXPos);
debug("new Y position: "+ newYPos);
if((newXPos !== lastPos.x) || (newYPos !== lastPos.y)){
$this.css({'backgroundPosition': newXPos + " " + newYPos});
lastPos.x = newXPos;
lastPos.y = newYPos;
}else{
clearInterval(ts);
intervalActive = false;
$(window).bind('scroll.parallax',onScroll);
}
}
function onScroll(){
if($this.hasClass("inview")){
if(!intervalActive){
ts = setInterval(updateElemPos,FPS);
$(window).unbind('scroll.parallax');
}
}
}
//recalculate position on scroll
$(window).bind('scroll.parallax',onScroll);
});
};
})(jQuery);