2012-04-23 65 views
1

我正在使用Ian Lunn的视差教程http://www.ianlunn.co.uk/demos/recreate-nikebetterworld-parallax/。我想在此基础上不仅改变背景的y位置,而且还改变滚动的x位置。这基本上会增加水平移动以及垂直移动,以便一些物体在两个方向上移动。在滚动上更改背景位置-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”功能?

回答

0

其实,我想出了一些有效的东西。

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

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

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