2012-05-16 177 views
0

我試圖創建兩個「按鈕」,使用懸停事件,隱藏一些divs,然後顯示一些其他人在他們的位置。然後我試圖延遲交換回默認div。jQuery swap div懸停延遲

所有的作品都很好,除非你從一個按鈕到另一個按鈕,在這個時候你會同時顯示很多div,直到延遲過去。如果我們不使用延遲,它可以很好地工作。

的JavaScript:

jQuery(function ($) { 

$('#servers-btn').hover(
    function() { 
    $('#servers, #servers-heading, #servers-arrow').show(0); 
    $('#default, #default-heading').hide(0); 
    }, 
    function() { 
    setTimeout(function() { 
     $('#servers, #servers-heading, #servers-arrow').hide(0); 
     $('#default, #default-heading').show(0); 
     },1000) 
    } 
); 

$('#hosting-btn').hover(
    function() { 
    $('#hosting, #hosting-heading, #hosting-arrow').show(0); 
    $('#default, #default-heading').hide(0); 
    }, 
    function() { 
    setTimeout(function() { 
     $('#hosting, #hosting-heading, #hosting-arrow').hide(0); 
     $('#default, #default-heading').show(0); 
     },1000) 
    } 
); 

}); 

我想我需要做出一個懸停功能知道對方的存在,因此可以取消超時,我只是不知道如何做到這一點。

編輯 - 只是整理代碼,將所有的div放入一個隱藏/顯示。

此外,我應該可能提到#default,#servers和#hosting div顯示在完全相同的位置。所以需要在同一時間立即切換(以上所述)。

編輯 - 最新嘗試在這裏使用clearTimeout http://jsfiddle.net/KH4tt/1/ - 但不能使它正常工作。

+0

請使用這個例子,像這樣\ jQuery('#hosting')。stop(true,true).show(0); – ShibinRagh

+0

@ShibinRagh我必須使用delay()才能正常工作嗎? – Deltabee

+0

如果我使用clearTimeout函數怎麼辦?只是不確定如何實現它。 – Deltabee

回答

0

好下面是使用clearTimeout()函數取消在創建延遲的mouseleave(hover handlerOut)上使用的setTimeout()的最後一件事:

jQuery(function($) { 

var timeoutserver; 

function canceltimeout() { 
if (timeoutserver) { 
    window.clearTimeout(timeoutserver); 
    timeoutserver = null; 
} 
} 

function closeServertime() { 
    timeoutserver = window.setTimeout(function() { 
     $('#servers, #servers-heading, #servers-arrow').hide(0); 
     $('#default, #default-heading').show(0); 
    }, 1000);   
} 

function closeHostingtime() { 
    timeoutserver = window.setTimeout(function() { 
     $('#hosting, #hosting-heading, #hosting-arrow').hide(0); 
     $('#default, #default-heading').show(0); 
    }, 1000);   
} 


$('#servers-btn').hover(

function() { 
    canceltimeout(); 
    $('#servers, #servers-heading, #servers-arrow, ').show(0); 
    $('#default, #default-heading, #hosting, #hosting-heading, #hosting-arrow').hide(0); 
}, function() { 
    closeServertime(); 
}); 

$('#hosting-btn').hover(

function() { 
    canceltimeout(); 
    $('#hosting, #hosting-heading, #hosting-arrow').show(0); 
    $('#default, #default-heading, #servers, #servers-heading, #servers-arrow').hide(0); 
}, function() { 
    closeHostingtime(); 
}); 

}); 
0

嘗試是這樣的

$('#servers-btn').hover(
    function() { 
    $('#servers, #servers-heading, #servers-arrow').show(0); 
    $('#default, #default-heading').hide(1000, function() { 
     $('#servers, #servers-heading, #servers-arrow').hide(0); 
     $('#default, #default-heading').show(0); 
    }); 
    } 
); 
1

您可以使用jQuery中的.delay()函數(我使用jQuery 1.7.1版本),如下面的例子:

$(selector).delay(1000).hide("slow"); 
+0

嗨,我試過使用延遲而不是setTimeout,但它會導致內容稍微跳躍,因爲兩個鼠標移出時的事件似乎不會在同一時間執行。如果我可以將它們都包裝在一個延遲()中,但可能會起作用,但我不知道如何?此外,它仍然不能解決關鍵問題,如果您在delay()期間將鼠標懸停在另一個按鈕上,則會在新的mouseenter之後生成舊的鼠標符號。 – Deltabee