2014-03-25 37 views
0

我在某些元素上使用jquery mouseenter/mouseleave函數。如果在頁面加載時Jquery mouseenter事件掛起

在Mouseenter上有些內容通過ajax加載並向elemet添加菜單。在mouseleave上,菜單消失。

如果我在頁面渲染時懸停這些元素,有時候這些元素不會消失。他們保持可見。我無法用演示代碼重現此內容。

有什麼建議嗎?可能是.data()的問題?

$(document).on('mouseenter', '._hoverflow', channels.smallMenu); 
$(document).on('mouseleave', '._hoverflow', channels.smallMenu); 

toggleMenu: function(p,i){ 
    if(p.data('loaded') === true ){ 
     if(p.data('visible') === true){ 
      i.stop(true,true).fadeOut('fast'); 
      p.removeClass('fadet').data('visible',false); 
     } 
     else{ 
      i.stop(true,true).fadeIn('fast'); 
      p.addClass('fadet').data('visible',true); 
     } 
     return true; 
    } return false; 
}, 
smallMenu: function(a){ 
    var p = $(this), cid = parseFloat(p.data('id')), i = p.find('.channel-dropdown'); 
    if(channels.toggleMenu(p,i)){ return false; } 
    p.addClass('fadet').data('loaded',true).data('visible',true); 
    var s = $.post(channels.vars.details, { id: cid }); 
    s.done(function(data){ channels.menuTemplate(data, cid).appendTo(p).stop(true,true).fadeIn('fast'); }); 

}, 

回答

1

我認爲這個問題是你已經設置後p.data('loaded', true)你不能切換,因爲在你的toggleMenu檢查p.data('loaded') === true的可見狀態。

圍繞移動邏輯,這樣你可以隨時切換能見度但只有一次加載內容:

toggleMenu: function(p,i){ 
    if(p.data('visible') === true){ 
     i.stop(true,true).fadeOut('fast'); 
     p.removeClass('fadet').data('visible',false); 
    } 
    else{ 
     i.stop(true,true).fadeIn('fast'); 
     p.addClass('fadet').data('visible',true); 
    } 
}, 

smallMenu: function(a){ 
    var p = $(this), cid = parseFloat(p.data('id')), i = p.find('.channel-dropdown'); 

    // Always toggle the menu visibility. 
    channels.toggleMenu(p,i); 

    // But only load the content once. 
    if(p.data('loaded') === true){ return false; } 
    p.addClass('fadet').data('loaded',true).data('visible',true); 
    var s = $.post(channels.vars.details, { id: cid }); 
    s.done(function(data){ channels.menuTemplate(data, cid).appendTo(p).stop(true,true).fadeIn('fast'); }); 
} 
+0

內容加載只有一次,但你的代碼是乾淨。錯誤仍然發生。有些事情工作。 add/removeClass('fadet')起作用。 fadeIn(),fadeOut()的東西不。頁面加載後,運行很多JS,同時這樣做的一些元素徘徊保持菜單​​可見,有時懸停效果相反,隱藏懸停顯示離開... – user2429266

+0

好的。我想我明白了。東西被加載,這需要一些時間,加載後調用fadeIn。這與頁面加載沒有任何關係。它在加載後淡入菜單。無論是否徘徊.. – user2429266

+0

在這一行:s.done(function(data){channels.menuTemplate(data,cid).appendTo(p).stop(true,true).fadeIn('fast'); }); 我需要檢查容器是否具有淡化類。如果不是不會淡入。 – user2429266

相關問題