0

So I have a background image but no html with tags but I want to make the background image move on hove with some type of javascript. But when I add the img tag into the html it covers everything else. Here is a live example I don't know that much javascript so I'm stuck.

body { background-image: url('images/bg_2880.jpg'); }

I need the javascript to move the image on the mouse hover like this js fiddle

3
  • 2
    Whats wrong with the example you linked? Why not just plug your image url into that code? You need to create a Minimal, Complete, and Verifiable example for us to help you with your code. Commented Feb 14, 2019 at 22:43
  • 1
    @Ethical38 I think you just answered your own question Commented Feb 14, 2019 at 22:44
  • When I add the image tag it covers everything on this web page getmdl.io/templates/blog/index.html Commented Feb 14, 2019 at 22:45

1 Answer 1

5

This is called a parallax effect

Simple example:

addEventListener('mousemove', ev => {
  const force = 100;
  const dx = -ev.clientX / window.innerWidth * force;
  const dy = -ev.clientY / window.innerHeight * force;
  document.body.style.backgroundPositionX = dx + 'px';
  document.body.style.backgroundPositionY = dy + 'px';
});
body {
  background-image: url("http://placekitten.com/800/800");
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.