-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyscript.js
64 lines (41 loc) · 1.68 KB
/
myscript.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
$( function() {
var element = $( 'body' );
var layers = element.css( 'background-position' ).split( ',' );
// add rate of postion change to x and y for each layer
var lay0xpos = 1;
var lay0ypos = 0;
var lay1xpos = 2;
var lay1ypos = 0;
var lay2xpos = 3;
var lay2ypos = 0;
// if background-position is set in your stylesheet make this the starting position for each layer
lay0split = layers[ 0 ].split( ' ' );
lay0xadd = parseFloat( lay0split[ 0 ] );
lay0yadd = parseFloat( lay0split[ 1 ] );
lay1split = layers[ 1 ].split( ' ' );
lay1xadd = parseFloat( lay1split[ 1 ] );
lay1yadd = parseFloat( lay1split[ 2 ] );
lay2split = layers[ 2 ].split( ' ' );
lay2xadd = parseFloat( lay2split[ 1 ] );
lay2yadd = parseFloat( lay2split[ 2 ] );
$(window).bind('scroll',function(e){
parallaxScroll();
});
function changelayer( layer0, layer1, layer2 ) {
layers[ 0 ] = layer0;
layers[ 1 ] = layer1;
layers[ 2 ] = layer2;
element.css( 'background-position', layers.join() );
}
function parallaxScroll(){
var scrolled = $( window ).scrollTop();
// calculate rate of position change per layer then add starting position if set by background-position in css
layer0x = ( 0- ( scrolled * lay0xpos ) ) + lay0xadd;
layer0y = ( 0- ( scrolled * lay0ypos ) ) + lay0yadd;
layer1x = ( 0- ( scrolled * lay1xpos ) ) + lay1xadd;
layer1y = ( 0- ( scrolled * lay1ypos ) ) + lay1yadd;
layer2x = ( 0- ( scrolled * lay2xpos ) ) + lay2xadd;
layer2y = ( 0- ( scrolled * lay2ypos ) ) + lay2yadd;
changelayer( layer0x + 'px ' + layer0y + 'px ', layer1x + 'px ' + layer1y + 'px ', layer2x + 'px ' + layer2y + 'px ' );
}
} );