滚动时更改background-position-x

时间:2012-04-23 17:29:06

标签: jquery html css animation

我正在使用Ian Lunn的Parallax教程http://www.ianlunn.co.uk/demos/recreate-nikebetterworld-parallax/。我希望在此基础上不仅可以改变背景的y位置,还可以改变滚动的x位置。这基本上会增加水平移动和垂直移动,以便一些物体在两个方向上移动。

举例说明我想做的事情http://www.nikebetterworld.com/product。汽车进来的第三个场景是我想要实现的运动。

他的原始代码是:

//function that is called for every pixel the user scrolls. Determines the position of the background
/*arguments: 
    x = horizontal position of background
    y = vertical position of the background
    windowHeight = height of the viewport
    pos = position of the scrollbar
    adjuster = adjust the position of the background
    inertia = how fast the background moves in relation to scrolling
    backgroundPosition-y = original background position vertical
*/
function newPos(x, windowHeight, pos, adjuster, inertia){
    return x + "% " + (-((windowHeight + pos) - adjuster) * inertia)  + "px";
}

//function to be called whenever the window is scrolled or resized
function Move(){ 
    var pos = $window.scrollTop(); //position of the scrollbar

    //if the first section is in view...
    if($firstBG.hasClass("inview")){
        //call the newPos function and change the background position
        $firstBG.css({'backgroundPosition': newPos(20, windowHeight, pos, 300, 0.1)}); 
        sun_1.css({'backgroundPosition': newPos(10, windowHeight, pos, 4000, .1)});
        deer.css({'backgroundPosition': newPos(40, windowHeight, pos, 500, .1)});
    }

我确定这是非常非常错误的,但这是我尝试过的。

sun_1.css({'backgroundPosition': newPos(10, windowHeight, pos, 4000, .1)}, {'background-position-x': '0% ' + parseInt(-xx/ 10) + 'px'});

有没有办法将其添加到" newPos"功能?

1 个答案:

答案 0 :(得分:0)

实际上,我发现了一些有用的东西。

sun_1.css({'backgroundPosition': newPos((3+pos/50), windowHeight, pos, 4000, .2)});

也许不是最好的解决方案,但似乎工作正常。第一个数字定义起始位置(在这种情况下为3%),第二个数字抓住窗口相对于滚动的位置,第三个数字确定移动速度。

要更改沿x的移动方向,请将“50”更改为负数。

相关问题