Created
May 17, 2015 20:35
-
-
Save nimaparsi/d262eafc97495a7ad56e to your computer and use it in GitHub Desktop.
Rotate Element with Cursor Position
This file contains 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
var img = $('img'); | |
if(img.length > 0){ | |
var offset = img.offset(); | |
function mouse(event){ | |
var center_x = (offset.left) + (img.width()/2); | |
var center_y = (offset.top) + (img.height()/2); | |
var mouse_x = event.pageX; var mouse_y = event.pageY; | |
var radians = Math.atan2(mouse_x - center_x, mouse_y - center_y); | |
var degree = (radians * (180 / Math.PI) * -1) + 90; | |
img.css('-moz-transform', 'rotate('+degree+'deg)'); | |
img.css('-webkit-transform', 'rotate('+degree+'deg)'); | |
img.css('-o-transform', 'rotate('+degree+'deg)'); | |
img.css('-ms-transform', 'rotate('+degree+'deg)'); | |
} | |
$(document).mousemove(mouse); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment