2013-07-11 61 views
0

基本上我的代码是让图像向上滑动,当我想让它向右滑动时。它出什么问题了?jQuery图像向上滑动而不是向右滑动

HTML

<div id="wrapper"> 
    <div id="header"> 
    </div> 
</div> 

JS

var scrollSpeed = 70;  // Speed in milliseconds 
var step = 1;    // How many pixels to move per step 
var current = 0;   // The current pixel row 
var imageHeight = 4300;  // Background image height 
var headerHeight = 300;  // How tall the header is. 

//The pixel row where to start a new loop 
var restartPosition = -(imageHeight - headerHeight); 

function scrollBg(){ 

//Go to next pixel row. 
current -= step; 

//If at the end of the image, then go to the top. 
if (current == restartPosition){ 
    current = 0; 
} 

//Set the CSS of the header. 
$('#header').css("background-position","0 "+current+"px"); 


} 

//Calls the scrolling function repeatedly 
var init = setInterval("scrollBg()", scrollSpeed); 

下面是代码:http://pastebin.com/9FpvSBxm

回答

1

你需要增加值

function scrollBg(){ 

    //Go to next pixel row. 
    current += step; //increment instead of decrement 

    //If at the end of the image, then go to the top. 
    if (current == restartPosition){ 
     current = 0; 
    } 

    //Set the CSS of the header. 
    $('#header').css("background-position",current +"px 0"); // parameters are right top not top right 



} 

演示:Fiddle

+0

这只是让它上升。 – user2552392

+0

@ user2552392检查更新 –

0

更新:我想通了。

$('#header').css("background-position","0 "+current+"px"); 

我将0更改为1,它修复了它。现在这完全是运气,但为什么这样做能解决它?