2015-04-15 20 views
0

我有一個指示元素,當我將其懸停時應該消失,替換應該出現在同一個地方。檢測到多個事件,其中應該只有一個

現在,當我離開這個替換時,它現在應該消失,並且舊的指示器應該再次放置它。

這裏是指標或觸發一個HTML:

<div class="help-trigger for-desktop"> 
    <div class="bg"> 
     <h4>HELP ME<i class="icon-double-angle-right"></i><i class="icon-double-angle-left"></i></h4> 
    </div> 
</div> 
<div class="help-trigger for-mobile"> 
    <div class="bg"> 
    </div> 
</div> 

這裏是jQuery代碼:

jQuery('.help-trigger.for-mobile').hover(function(){ 
    jQuery(this).stop(true, true).hide(); 
    jQuery('.help-trigger.for-desktop').stop(true, true).show(); 
    jQuery('.help-trigger.for-desktop').mouseout(function(){ 
     jQuery(this).stop(true, true).hide(); 
     jQuery('.help-trigger.for-mobile').stop(true, true).show(); 
    }); 
}); 

這在一般的作品,但它有點閃爍,它是不穩定的。事件被檢測到超過他們應該(更好的話),所以我得到太多的變化,即使我沒有離開.for-desktop元素

+0

您在每個'。幫助 - 特里格結合mouseout事件er.for-mobile' mouseout/mouseleave。 –

回答

2

您正在添加一個.mouseout()事件處理程序每​​次(!)你徘徊你的移動您的.help-trigger.for-mobile元素。因此,在每個鼠標移動中,您都會撥打幾個處理程序......是您的目的嗎?

編輯:此外,使用.hover()僅一次回調會添加一個處理兩者的mouseenter 鼠標離開,所以你.mouseout每個的mouseenter添加額外的處理程序和鼠標離開

+1

**作爲一個方面說明:**不僅在懸停,但當鼠標離開,因爲OP使用'懸停'方法'輸入/輸出處理程序 –

+0

謝謝。這解決了它 –

0

嘿,我想你努力才達到這樣的事情:

$('.help-trigger.for-mobile').hover(function() { 
     $(this).hide();  
    }, function() { 
     $(this).show(); 
    }); 

你把.mouseout鼠標懸停內,不能工作方式:https://api.jquery.com/hover/

相關問題