小编cee*_*eed的帖子

如何检查jquery中是否存在变量

有没有办法在jquery/javascript中创建一个变量,如果它不存在?

我试过:

    if (moving.length<0) {
        moving=false;
    }

    if (moving=='undefined') {
        moving=false;
    }
Run Code Online (Sandbox Code Playgroud)

但逻辑上这一切都很糟糕,因为对变量的初始检查已经未定义

希望你们能帮忙:)

第一个答案工作正常,但我的问题仍在继续.

$.fn.animation = function() {
    if (typeof moving == 'undefined') {var moving = false;};

    if(moving===false){
        var moving=true;
        console.log('test');
    };      
};


$(window).scroll(function(){
    if($(window).scrollTop() < scrollpos){
        elm.animation();
    }
    scrollpos=$(window).scrollTop();
});
Run Code Online (Sandbox Code Playgroud)

在这个设置'test'中,即使我记录移动设置为true,仍然会被忽略.

所以我要归档的是每次我滚动元素得到检查,如果它已经动画.如果是这样,不要做动画.

基本上是一个队列

variables jquery exists

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

exists ×1

jquery ×1

variables ×1