Mousemove parallax effect moves position of div

后端 未结 1 1541
面向向阳花
面向向阳花 2021-02-08 09:56

I\'m trying to create a slight parallax effect (I\'m not sure if it really constitutes parallax but it\'s a similar idea). Where there are four layers which all move around at a

相关标签:
1条回答
  • 2021-02-08 10:35

    How about using jQuery.offSet() instead? You might want to adjust the math/values, but I think it should set you in the right direction.

    $(document).ready(function () {
        $('#layer-one').mousemove(function (e) {
            parallax(e, this, 1);
            parallax(e, document.getElementById('layer-two'), 2);
            parallax(e, document.getElementById('layer-three'), 3);
        });
    });
    
    function parallax(e, target, layer) {
        var layer_coeff = 10 / layer;
        var x = ($(window).width() - target.offsetWidth) / 2 - (e.pageX - ($(window).width() / 2)) / layer_coeff;
        var y = ($(window).height() - target.offsetHeight) / 2 - (e.pageY - ($(window).height() / 2)) / layer_coeff;
        $(target).offset({ top: y ,left : x });
    };
    

    JSFiddle: http://jsfiddle.net/X7UwG/854/

    0 讨论(0)
提交回复
热议问题