2013-06-28 50 views
0

所以我想重構一些使用hoverIntent的jQuery鼠標。寄託都工作時,我呼籲個別對hoverIntent每個導航項目,但是當我嘗試使用下面的代碼,什麼也沒有發生第一鼠標懸停,那麼它的工作原理:hoverIntent不工作在第一個懸停

$('.navItem1').on('hover', function(){ 
    navHoverIntent(this); 
}); 

$('.navItem2').on('hover', function(){ 
    navHoverIntent(this); 
}); 

$('.navItem3').on('hover', function(){ 
    navHoverIntent(this); 
}); 

$('.navItem4').on('hover', function(){ 
    navHoverIntent(this); 

}); 

function navHoverIntent(className) { 
//set some vars for use in hoverIntent 
var currentDiv = $(className); 
var cubeImg = currentDiv.find('img'); 
var childDivs = currentDiv.find('div'); 
var navTitle = childDivs[0]; 
var subNav = childDivs[1]; 
//now calling hoverIntent 

currentDiv.hoverIntent({ 
    over: function(){ 
     if(cubeImg.css("display")=="block"){ 
     navTitle.css("color","#262261"); 
     } 
     currentDiv.css("backgroundColor","#d7d7d7"); 
     $(subNav).show(); 
     }, 
    out: function(){ 
     if(cubeImg.css("display")=="block"){ 
     navTitle.css("color","#8CC640"); 
     currentDiv.css("backgroundColor","#262261"); 
     }else{ 
     currentDiv.css("backgroundColor","transparent"); 
     $("#subnav1").hide(); 
     $("#subnav2").hide(); 
     $("#subnav3").hide(); 
     $("#subnav4").hide(); 
     } 
     }, 

    }); 
} 

任何幫助,將不勝感激!

回答

0

如果你仍然需要這個答案,我可以看到你的問題是在第一個懸停你添加事件監聽器(但它不會被觸發)。

此外,您正在每個懸停上添加一個新的事件偵聽器,而無需刪除舊的偵聽器。

如果這不是預期的行爲,您可以自己調用每個「navItem」的函數,並將其從懸停狀態中移除。

希望它有幫助