[英]jQuery: On click, prevent mouseenter event
我有一個正在發生的兩個不同的動畫,一個火災mouseenter
其他的click
。 問題是,當它們都被激活時,它會創建一個跳躍動畫。
你可以在這里看到我的意思: JSFiddle
如果click
事件被激活,是否可以防止發生mouseenter
事件?
使用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();
});
}
});
似乎更容易做到這一點:
$('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();
});
}
}
});
如何在延遲后檢查open
課。
改變
$('header h1').mouseenter(function() {
$('header:not(.open)').delay(500).animate({
'padding-bottom': '40px'
}, 150, function() {
//function complete
});
});
至
$('header h1').mouseenter(function() {
$.delay(500);
$('header:not(.open)').animate({
'padding-bottom': '40px'
}, 150, function() {
//function complete
});
});
顯然你可以根據自己的喜好微調延遲,但這個想法可能是這樣的:
如果它們在x
毫秒之前沒有單擊,則增加填充。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.