-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallax.js~
28 lines (24 loc) · 1.06 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
//Registers a new .parallax element. Must be called on the Jquery object immediately after the element is inserted.
function registerObject(e) {
e.data('anchor', {x_0: parseInt(e.css('left')),
y_0: parseInt(e.css('top')),
pos: e.attr('data-pos') - $('#parallax-parameters').attr('data-origin')});
};
$(function() {
//Register every .parallax element when the document is loaded
$('.parallax').each(function(i) {
registerObject($(this));
});
//Generate parallax effect for each .parallax element on mousemove.
$(document).mousemove(function(e) {
$('.parallax').each(function(i) {
var x_rel = (1 - 2 * e.pageX / $(window).width()) * $('#parallax-parameters').attr('data-x_ratio');
var y_rel = (1 - 2 * e.pageY / $(window).height()) * $('#parallax-parameters').attr('data-y_ratio');;
var x_0 = $(this).data('anchor').x_0;
var y_0 = $(this).data('anchor').y_0;
var pos = $(this).data('anchor').pos - $('#parallax-parameters').attr('data-origin');
$(this).offset({left: x_0 + x_rel*pos,
top: y_0 + y_rel*pos});
})
});
});