Moving background-image on mousemove

Developer
Size
2,040 Kb
Views
28,336

How do I make an moving background-image on mousemove?

This was in response to a StackOverflow question: http://stackoverflow.com/questions/16743479/moving-background-with-my-cursor/16744057#16744057. What is a moving background-image on mousemove? How do you make a moving background-image on mousemove? This script and codes were developed by Chris Boon on 29 October 2022, Saturday.

Moving background-image on mousemove Previews

Moving background-image on mousemove - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>moving background-image on mousemove</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id="top-image"></div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Moving background-image on mousemove - Script Codes CSS Codes

#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);
}

Moving background-image on mousemove - Script Codes JS Codes

$(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");
});
});
Moving background-image on mousemove - Script Codes
Moving background-image on mousemove - Script Codes
Home Page Home
Developer Chris Boon
Username chrisboon27
Uploaded October 29, 2022
Rating 4.5
Size 2,040 Kb
Views 28,336
Do you need developer help for Moving background-image on mousemove?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Chris Boon (chrisboon27) Script Codes
Create amazing web content with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!