Created
July 25, 2015 08:54
-
-
Save bepatrickdavid/0a4d01c733e1c45c0c27 to your computer and use it in GitHub Desktop.
CSS: Moving background on mouse over muovere in base al movimento del mouse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="top-image"></div> | |
<style> | |
#top-image { | |
background:url('https://d3ui957tjb5bqd.cloudfront.net/images/screenshots/products/0/8/8905/red-rocks-park-o.jpg') -25px -50px; | |
position:fixed ; | |
top:0; | |
width:100%; | |
z-index:0; | |
height:100%; | |
background-size: calc(100% + 50px); | |
} | |
</style> | |
<script> | |
$(document).ready(function() { | |
var movementStrength = 25; | |
var height = movementStrength / $(window).height(); | |
var width = movementStrength / $(window).width(); | |
$("#top-image").mousemove(function(e){ | |
var pageX = e.pageX - ($(window).width() / 2); | |
var pageY = e.pageY - ($(window).height() / 2); | |
var newvalueX = width * pageX * -1 - 25; | |
var newvalueY = height * pageY * -1 - 50; | |
$('#top-image').css("background-position", newvalueX+"px "+newvalueY+"px"); | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment