jQuery/Javascript NaN 在对数字进行加法时



我正在尝试创建一个简单的链接滚动器,但在确定当前位置时遇到了一些麻烦。

基本上,在我的代码中,我已经在函数外部初始化了当前 Pos 变量。然后尝试向其添加 1,但我的行为有些不稳定。有时得到NaN。

这是在本地主机 xampp 安装上,相同的代码在 jsfiddle 中工作正常,所以我无法理解它。

JSFIDDLE: http://jsfiddle.net/w654X/

我的代码如下,任何帮助将不胜感激。

var currentPos = 1;
$('#test').click(function() {
    // exit if animation is already playing
    if ($(':animated').length) {
        return false;
    }
    height = $('#inner').height();
    noOfLinks = height / 53;
    lastPos = noOfLinks - 4;
    alert(currentPos);
    if (currentPos != lastPos) {
        $('#inner').animate({
            marginTop: "-=106px"
        });
    } 
    else {
        $('.arrow-up').hide();
    }
    currentPos += 1;
});

请尝试以下代码

$(document).ready(function(){
var currentPos = 1;
$('#test').click(function() {
// exit if animation is already playing
    if ($(':animated').length) {
        return false;
    }
height = $('#inner').height();
noOfLinks = height / 53;
lastPos = noOfLinks - 4;
alert(currentPos);
if (currentPos != lastPos) {
    $('#inner').animate({
        marginTop: "-=106px"
    });
} else {
    $('.arrow-up').hide();
}
currentPos += 1;
});
});

让我知道您在加载或文档准备就绪时在哪里使用了脚本。

谢谢

好吧,这让我发疯太久了,但事实证明我已经在另一个脚本中使用了变量名称"direction"。使用了不同的名字,一切都很好。

最新更新