-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmouse.parallax.js
More file actions
50 lines (39 loc) · 1.4 KB
/
Copy pathmouse.parallax.js
File metadata and controls
50 lines (39 loc) · 1.4 KB
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
/*
Mouse Parallax
==============
A simple jQuery plugin to allow given elements to be used as backgrounds that respond to mouse movement. Could easily be further extended or modified.
--------------
Author: "Pip Beard Design," Benjamin Alan Robinson
LICENSE: The MIT License (MIT)
*/
(function ( $ ) {
$.fn.extend({
mouseParallax: function(options) {
var defaults = { moveFactor: 5, zIndexValue: "-1", targetContainer: 'body' };
var options = $.extend(defaults, options);
return this.each(function() {
var o = options;
var background = $(this);
$(o.targetContainer).on('mousemove', function(e){
mouseX = e.pageX;
mouseY = e.pageY;
windowWidth = $(window).width();
windowHeight = $(window).height();
percentX = ((mouseX/windowWidth)*o.moveFactor) - (o.moveFactor/2);
percentY = ((mouseY/windowHeight)*o.moveFactor) - (o.moveFactor/2);
leftString = (0-percentX-o.moveFactor)+"%";
rightString = (0-percentX-o.moveFactor)+"%";
topString = (0-percentY-o.moveFactor)+"%";
bottomString = (0-percentY-o.moveFactor)+"%";
background[0].style.left = leftString;
background[0].style.right = rightString;
background[0].style.top = topString;
background[0].style.bottom = bottomString;
if(o.zIndexValue) {
background[0].style.zIndex = o.zIndexValue;
}
});
});
}
});
} (jQuery) );