Move the background image on mouse over (Single Parallax)

前端 未结 3 536
囚心锁ツ
囚心锁ツ 2020-12-14 11:37

I would like to make the background image move slightly on the X and Y axis when the mouse is in the \"landing-content\" DIV, it should move with the movement of the mouse.

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 12:09

    You could use the mousemove event, as shown in this fiddle. http://jsfiddle.net/X7UwG/

    $('#landing-content').mousemove(function(e){
        var amountMovedX = (e.pageX * -1 / 6);
        var amountMovedY = (e.pageY * -1 / 6);
        $(this).css('background-position', amountMovedX + 'px ' + amountMovedY + 'px');
    });
    

    It's just a quick example though, you'll have to play with the numbers yourself. ;)

提交回复
热议问题