0
如何更改div div懸停時的全局var值。Javascript更改div div全局變量Hover
當我懸停在.mehover
或.mehoverAnother
類 'hideplease' 將被添加到.mehide
或.mehideAnother
類。當在hoverOut刪除類.mehide
或.mehideAnother
而是由2S並且如果每個我懸停在.mehover
或.mehoverAnother
的傳輸TimeToLive變量值改變爲0。
見下面我的代碼的時間延遲的去除一類:
的Javascript
var timetolive = 2000;
$(document).ready(function() {
$('.meHover').hover(function() {
//code here to change the timetolive var value
//the .mehide or .mehideAnother should hide immediately by changing the timetolive var value to 0
$('.mehide').addClass('hideplease');
}, function() {
setTimeout(function(){ $('.mehide').removeClass('hideplease'); }, timetolive); //delay removal of class
});
$('.meHoverAnother').hover(function() {
//code here to change the timetolive var value
//the .mehide or .mehideAnother should hide immediately by changing the timetolive var value to 0
$('.mehideAnother').addClass('hideplease');
}, function() {
setTimeout(function(){ $('.mehideAnother').removeClass('hideplease'); }, timetolive); //delay removal of class
});
});
HTML
<div class="container">
<div class="meHover">hoverme</div>
<div class="meHoverAnother">other hoverme</div>
<div class="mehide"></div>
<div class="mehideAnother"></div>
</div>
的jsfiddle這裏https://jsfiddle.net/pqn01e5h/9/
不知道你想要什麼來實現的,但如果你想重新開始計數到「活」,'setTimeout'返回計時器對象,它可以傳遞給'clearTimeout'功能「取消「它。 – ahwayakchih
我只需要清除timetolive var當我懸停在.mehover或.mehoverAnother –
你的意思是'timetolive = 0;'? – ahwayakchih