jQuery图像向上滑动而不是向右滑动

时间:2013-07-11 04:06:51

标签: jquery html

基本上我的代码是在我希望它向右滑动时使这个图像向上滑动。怎么了?

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

更新:我明白了。

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

我将0更改为1并修复了它。现在这纯粹是靠运气,但为什么要解决这个问题呢?