I want to have a function executed once on mouseenter then on mouseleave I would like to have the element to go back to its state.
My code produces an infinite loop. The mouse enters and the element flips infinitely until my mouse leaves.
$(document).ready(function() {
var flipfunc = function() {
var elem = $(this);
if (!elem.data('flipped')) {
elem.flip({
direction: 'lr',
speed: 250,
onBefore: function() {
elem.html(elem.siblings('.sponsorData').html());
}
});
elem.data('flipped', true);
elem.addClass('sponsorFlipped');
//elem.unbind('mouseenter', flipfunc);
}
else {
elem.revertFlip();
// Unsetting the flag:
elem.data('flipped', false)
//elem.unbind('mouseleave', rflipfunc);
}
}
$('.sponsorFlip').bind('mouseenter', flipfunc);
//$('.sponsorFlipped').bind('mouseleave', rflipfunc);
});
I have tried many solution, but I do not see where the issue is…
mouseenterevent is triggered when it flips, so you have to detect whether mouse has moved out or not. This is one method to do it.see a demo here ( tested only in FF ) : http://jsfiddle.net/tgg33/7/
Now the div gets flipped only when mouse enters. If you want to flip it back when mouse leaves you have to add
mouseleavehandler.