0

This is actually a continue from my previous question, but it was suggest to reopen as a seperate question.

I have a sprite image with three images init, I would like to change image position every second while the mouse is hovering the image

What I've tried:
http://jsfiddle.net/377Ja/4/

Error:

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

1 回答 1

2

That's because this isn't your element but window in your callback.

Here's a way to fix it :

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) ;
});

Demonstration

于 2013-09-16T16:39:42.627 回答