Help with a mousemove parallax effect script

Hi,

I have a parallax script that should get the images from the document and move them according to mouse movement.
It doesn’t work.

Can you know what is the problem?

<script>
    window.onload = function() {
      var c1left = document.getElementById('l2').offsetLeft,
        c1top = document.getElementById('l2').offsetTop,
        c2left = document.getElementById('l3').offsetLeft,
        c2top = document.getElementById('l3').offsetTop,

        parallaxBox.onmousemove = function(event) {
          event = event || window.event;
          var x = event.clientX - parallaxBox.offsetLeft,
            y = event.clientY - parallaxBox.offsetTop;

          mouseParallax('l2', c1left, c1top, x, y, 50);
          mouseParallax('l3', c2left, c2top, x, y, 20);
        }

    }

    function mouseParallax(id, left, top, mouseX, mouseY, speed) {
      var obj = document.getElementById(id);
      var parentObj = obj.parentNode,
        containerWidth = parseInt(parentObj.offsetWidth),
        containerHeight = parseInt(parentObj.offsetHeight);
      obj.style.left = left - (((mouseX - (parseInt(obj.offsetWidth) / 2 + left)) / containerWidth) * speed) + 'px';
      obj.style.top = top - (((mouseY - (parseInt(obj.offsetHeight) / 2 + top)) / containerHeight) * speed) + 'px';
    }
  </script>

Thanks!

I’ve tried running this code in jsfiddle, but there are some syntax errors in your code.
For example, the var statement has a trailing comma that doesn’t lead to any other variables being defined.

Do you have a working example of the code from somewhere else? Or failing that, can you supply the HTML code that’s supposed to be used with this script?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.