user2783132
user2783132

Reputation: 225

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

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 

Upvotes: 0

Views: 6430

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382170

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

Upvotes: 2

Related Questions