user1400312
user1400312

Reputation:

JavaScript Image Flip

I have some code that is running a JavaScript image flip, I'm new to JS and I need the image to flip when hovered and and hovered off, if I put it to onhover it flips everytime the mouse is moved, not when the mouse is off.

Here is the code:

$(document).ready(function () {
    setInterval(function () {
        $('.sponsorFlip').load('script.js');
        $('.sponsorFlip').load('jquery.flip.min.js');
    }, 30000);
    $('.sponsorFlip').one("mouseenter mouseleave", function () {
        var elem = $(this);
        if (elem.data('flipped')) {
            elem.revertFlip();
            elem.data('flipped', false)
        }
        else {
            elem.flip({
                direction: 'rl',
                speed: 250,
                onBefore: function () {
                    elem.html(elem.siblings('.sponsorData').html());
                }
            });
            elem.data('flipped', true);
        }
    });
    $('.sponsorFlip').bind("click", function () {
        var elem = $(this);
        if (elem.data('flipped')) {
            elem.revertFlip();
            elem.data('flipped', false)
        }
        else {
            elem.flip({
                direction: 'rl',
                speed: 250,
                onBefore: function () {
                    elem.html(elem.siblings('.sponsorData').html());
                }
            });
            elem.data('flipped', true);
        }
    });
});

Upvotes: 0

Views: 794

Answers (1)

Jānis
Jānis

Reputation: 328

Make use of classes. ;) Preview - http://jsfiddle.net/TJZmM/3/

$('div').bind('mouseover mouseout', function(){
    var self = $(this);

    if(self.toggleClass('flipped').hasClass('flipped')) {
        self.html('rl');
    }
    else {
        self.html('lr');
    }
});​

Upvotes: 1

Related Questions