未捕获的类型错误:无法使用"in"运算符在未定义中搜索"marginLeft"



这实际上是我之前问题的延续,但建议作为一个单独的问题重新打开。

我有一个精灵图像,里面

有三个图像,我想在鼠标悬停图像时每秒更改图像位置

我尝试过:
http://jsfiddle.net/377Ja/4/

错误:

Uncaught TypeError: Cannot use 'in' operator to search for 'marginLeft' 
                    in undefined 
那是

因为this不是你的元素,而是在你的回调中window

这是一种修复方法:

var myInterval
$(".miniPosterImg").hover(function() {
    var $this = $(this);
    myInterval= setInterval(function(){
        thismarginLeft = $this.css("margin-left").replace(/[^0-9]/g,'');
        if(thismarginLeft < 360){
                thismarginLeft = thismarginLeft-120;
        //}else{
        //      thismarginLeft = 0;
        }
        $this.css("margin-left", thismarginLeft + "px");
    },1000);
}, function(){
    clearInterval(myInterval) ;
});

示范

最新更新