The logic is actually quite simple: what you want to do is to offset the image away from its original position based on the relative position of the cursor in the document/viewport. We will need to perform all this calculations in the mousemove
event on document.
$(document).on('mousemove', function(e) {...});
Also, this means that you will need some following information:
- determine the maximum offset you want the image to be moved from its original position
- the viewport width and height
- the mouse/cursor position relative to viewport height—that will give us a range of [0, 1]
- transform that range to [-1, 1], since negative values are used to move to the top/left and positive values used to move to the bottom/right
- use CSS3 transform to move the image
Step-by-step guide
1. Determine maximum offset
Let's say we want to restrict the movement to ±30px. We can define them as:
// Maximum offset for image
var maxDeltaX = 30,
maxDeltaY = 30;
2. Get viewport dimensions
Viewport dimensions can be accessed by document.documentElement.clientWidth/clientHeight
:
// Get viewport dimensions
var viewportWidth = document.documentElement.clientWidth,
viewportHeight = document.documentElement.clientHeight;
3 and 4. Get the relative position of the cursor to the viewport
The key here is to calculate the relative position of the cursor to the viewport. First, we get the fraction of the mouse/cursor coordinates to the viewport, which will give us a range of [0, 1]. However, we need to transform this into [-1, 1], so that we can calculate left/top movement (using negative values) and bottom/right movement (using positive values). The arithmetic transformation from [0, 1] to [-1, 1] is simply multiplying to 2 (so you get [0, 2]) and minus 1 (then you get [-1, 1]):
// Get relative mouse positions to viewport
var mouseX = e.pageX / viewportWidth * 2 - 1,
mouseY = e.pageY / viewportHeight * 2 - 1;
5. Use CSS3 transform
to position your image
This is the most straight forward part. The amount to translate is simply mouseX
× maxDeltaX
and the same along the y-axis. We pass these values into transform: translate(<x>px, <y>px)
:
// Calculate how much to transform the image
var translateX = mouseX * maxDeltaX,
translateY = mouseY * maxDeltaY;
$('.animated').css('transform', 'translate('+translateX+'px, '+translateY+'px)');
Working example
See proof-of-concept below:
// Settings
// Maximum offset for image
var maxDeltaX = 30,
maxDeltaY = 30;
// Bind mousemove event to document
$(document).on('mousemove', function(e) {
// Get viewport dimensions
var viewportWidth = document.documentElement.clientWidth,
viewportHeight = document.documentElement.clientHeight;
// Get relative mouse positions to viewport
// Original range: [0, 1]
// Should be in the range of -1 to 1, since we want to move left/right
// Transform by multipling by 2 and minus 1
// Output range: [-1, 1]
var mouseX = e.pageX / viewportWidth * 2 - 1,
mouseY = e.pageY / viewportHeight * 2 - 1;
// Calculate how much to transform the image
var translateX = mouseX * maxDeltaX,
translateY = mouseY * maxDeltaY;
$('.animated').css('transform', 'translate('+translateX+'px, '+translateY+'px)');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<img class="animated" src="http://via.placeholder.com/350x150"/>