Site logo

Archived topic

How to make moving background header cover image - mouse moving effect

9 replies · Started by Nikola on January 22, 2018

Viewing posts 1–10 of 10

Hello Tom,

Can you help me with moving effect header background cover image - mouse moving effect?

Something like on this website https://www.rainfall.ro/

Thank in advance.

BR
Nikola

Tom,

I tried a couple of them earlier but i did not have the results with them.

I have targeted .generate-content-header, and i wrote JS in the wp_footer hook.

Maybe i am doing something wrong?

Thanks

Nikola

I'm not seeing any JS for it on the page - can you add it back so I can take a look?

Ok, take a look now.

Where did you add it? I'm having trouble spotting it.

In the wp_footer hook.

<?php if ( is_page( 'post-2' ) ) : ?>
    <script>
        var lFollowX = 0,
    lFollowY = 0,
    x = 0,
    y = 0,
    friction = 1 / 30;

function moveBackground() {
  x += (lFollowX - x) * friction;
  y += (lFollowY - y) * friction;
  
  translate = 'translate(' + x + 'px, ' + y + 'px) scale(1.1)';

  $('.generate-content-header').css({
    '-webit-transform': translate,
    '-moz-transform': translate,
    'transform': translate
  });

  window.requestAnimationFrame(moveBackground);
}

$(window).on('mousemove click', function(e) {

  var lMouseX = Math.max(-100, Math.min(100, $(window).width() / 2 - e.clientX));
  var lMouseY = Math.max(-100, Math.min(100, $(window).height() / 2 - e.clientY));
  lFollowX = (20 * lMouseX) / 100; // 100 : 12 = lMouxeX : lFollow
  lFollowY = (10 * lMouseY) / 100;

});
moveBackground();
    </script>
<?php endif; ?>

That code is targeting yourwebsite.com/post-2, which doesn't exist.

Yea, stupid mistake. :)

But, still not working. :/

In your code, try replacing $ characters with jQuery.

This archived topic is closed to new replies.