I have two seperate animations that are occurring, one fires on mouseenter
the other on click
. The problem is that when they are both activated it creates a jumpy animation.
You can see what I mean here: JSFiddle
Is it possible to prevent the mouseenter
event from occurring if the click
event is activated?
Javascript
$('header h1').mouseenter(function() {
$('header:not(.open)').delay(500).animate({
'padding-bottom': '40px'
}, 150, function() {
//function complete
});
});
$('header h1').mouseleave(function() {
$('header:not(.open)').animate({
'padding-bottom': '20px'
}, 150, function() {
//function complete
});
});
$('header h1').click(function() {
if ($('header').hasClass('open')) {
$('header p').fadeOut(100);
$('header').removeClass('open').animate({
'padding-bottom': '20px'
}, 300, function() {
//animation complete
});
}
else {
$('header').addClass('open').animate({
'padding-bottom': '150px'
}, 300, function() {
$('header p').fadeIn();
});
}
});
Seems easier to just do this :
$('header').on({
mouseenter: function(e) {
if (!$(this).is('.open')) {
$(this).stop(true, true).delay(500).animate({'padding-bottom': '40px' }, 150, function() {
//function complete
});
}
},
mouseleave: function(e) {
if (!$(this).is('.open')) {
$(this).stop(true, true).animate({'padding-bottom': '20px'}, 150, function() {
//function complete
});
}
},
click: function() {
if ($(this).is('.open')) {
$(this).find('p').fadeOut(100).end().removeClass('open').stop(true, true).animate({'padding-bottom': '20px'}, 300, function() {
//animation complete
});
}else{
$(this).addClass('open').stop(true, true).animate({'padding-bottom': '150px'}, 300, function() {
$('p', this).fadeIn();
});
}
}
});
No, you can't. Especially because those events are based on each other: To click on an element, you need to enter it with the mouse: You can't deactivate the enter
event which already happened when you click. And you obviously shouldn't deactivate future click
events when entering.
The solution to your problem with the jumping animations should be the stop()
method, which ends running animations on the selected elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With