试图将条件语句添加到jquery动画

时间:2013-06-30 14:45:28

标签: jquery

这是小提琴:http://jsfiddle.net/webguy262/e5ana/18/

使用默认城镇场景加载页面。悬停地图图钉会改变场景。

如果鼠标停止十秒钟,则默认场景返回。

尝试仅在隐藏默认场景时才这样做。那是因为如果默认场景是可见的,代码就会运行,它会将该默认场景设置为动画并再次备份。

这是我正在尝试的......

if ($('#dt').css('bottom') == '0px') {
var t;
var del = 10000; //Ms delay 
    $(document).mousemove(function(){
    clearTimeout(t);
    t = setTimeout(function(){
        //If the mouse is not moved
        $(".town").animate({
            bottom: '0px'
        }, 500);
        $("#dt").animate({
            bottom: '126px'
        }, 500);
    }, del);
});
}

但它不起作用。不会抛出脚本错误,也不会发生任何事情。

语法问题?没有正确检测#dt的位置?

1 个答案:

答案 0 :(得分:0)

我在想这是因为你已经在你的函数内部,并在你调用mousemove时通过if语句。也许尝试这样的事情:

var t
,   del = 10000; //Ms delay 

$(document).mousemove(function(){
    if ($('#dt').css('bottom') === '0px') {
        clearTimeout(t);
        t = setTimeout(function(){
            //If the mouse is not moved
            $(".town").animate({
                bottom: '0px'
            }, 500);
            $("#dt").animate({
                bottom: '126px'
            }, 500);
        }, del);
    }
});