Skip to content

Instantly share code, notes, and snippets.

@khripunovpp
Created March 15, 2019 14:13
Show Gist options
  • Save khripunovpp/13d4c1552ad5e183316102b12afbbac8 to your computer and use it in GitHub Desktop.
Save khripunovpp/13d4c1552ad5e183316102b12afbbac8 to your computer and use it in GitHub Desktop.
parallaxMouse
var parallaxMouse = function(el, coeff, direction) {
$(window).bind('mousemove', function(e) {
parallaxScroll(e);
});
function parallaxScroll(e) {
$(el).each(function() {
var container = $('body'),
containerWidth = container.width(),
containerHeight = container.height(),
mouseXCord = event.pageX,
mouseYCord = event.pageY,
shiftX = containerWidth/2 - mouseXCord,
shiftY = containerHeight/2 - mouseYCord,
transformString;
if(direction === "x") {
transformString = 'transform: translateX(' + shiftX / (coeff*10) + '%)';
}
if(direction === "y") {
transformString = 'transform: translateY(' + shiftY / (coeff*10) + '%)';
}
if(direction === "xy") {
transformString = 'transform: translate(' + shiftX / (coeff*10) + '%, ' + shiftY / (coeff*10) + '%)';
}
this.style = transformString;
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment