K
K
kimqar2020-04-23 14:49:58
JavaScript
kimqar, 2020-04-23 14:49:58

How can I make the image move from the cursor in different directions when hovering over it?

How can I make the image move away from the cursor when hovering over it? And is it possible to move it if the picture is through the before pseudo-element?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Istomin, 2020-04-23
@kimqar

Try the vanilla-tilt library
https://micku7zu.github.io/vanilla-tilt.js/

D
Dubolom Unicellular, 2020-04-23
@duboloms

Use the anime.js library, it's an engine for website animations and doesn't weigh much.

img.addEventListener("mouseover", function() {
   anime({
    targets: '.lion', // селектор твоего элемента
    translateX: function() {
      return anime.random(0, 50); // аnime.js сам сгенерит случайное число от 0 до 50
    },
    translateY: function() {
      return anime.random(0, 50); // аnime.js сам сгенерит случайное число от 0 до 50
    }
});

Or use the parallax.js library

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question