[英]jquery incomplete animation on hover
我正在為我的網站開發導航系統。 想法是在懸停父母時顯示內容。
但是,當內容快速懸停時,動畫是不完整的,並且在兩者之間停止。
完整的工作和代碼在下面的演示鏈接中。 關於更改代碼的任何建議將很有幫助。
當同時懸停“兩個”箭頭(從一個箭頭快速移動到另一個箭頭)時,它們停止並且不完整
我使用的代碼是
$('.anpn-wrap').mouseenter(function(){
$wrap = $(this);
$txt = $(this).find('.anpn-text');
$cnt = $(this).find('.anpn-cnt');
$txt.hide();
$cnt.css({ 'opacity': 0, 'margin-top': '-50px', 'width': '200px' });
$wrap.stop().animate({ 'width': $cnt.outerWidth() });
$cnt.show().animate({ 'opacity': 1, 'margin': 0});
});
$('.anpn-wrap').mouseleave(function(){
$wrap = $(this);
$txt = $(this).find('.anpn-text');
$cnt = $(this).find('.anpn-cnt');
$cnt.show().stop().animate({ 'opacity': 0, 'margin-top': '-50px' }, function(){
$cnt.hide();
$wrap.stop().animate({ 'width': $txt.outerWidth() });
$txt.fadeIn();
});
});
通過不本地化$wrap
, $txt
和$cnt
,它們將是全局的,因此,如果在更早的mouseleave動畫完成之前發生mousenter事件,則這些var將被覆蓋,並且第一個動畫中的回調將解決另一個按鈕的元素。
解決方案是在兩個處理程序中都用var
聲明$wrap
, $txt
和$cnt
。
嘗試這個:
$('.anpn-wrap').mouseenter(function() {
var $wrap = $(this).stop();
var $txt = $wrap.find('.anpn-text').hide();
var $cnt = $wrap.find('.anpn-cnt').css({
'opacity': 0,
'margin-top': '-50px',
'width': '200px'
}).show().animate({
'opacity': 1,
'margin': 0
});
$wrap.animate({
'width': $cnt.outerWidth()
});
}).mouseleave(function() {
var $wrap = $(this);
var $txt = $wrap.find('.anpn-text');
var $cnt = $wrap.find('.anpn-cnt').show().stop().animate({
'opacity': 0,
'margin-top': '-50px'
}, function() {
$cnt.hide();
$wrap.stop().animate({
'width': $txt.outerWidth()
});
$txt.fadeIn();
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.