我想要一個視頻播放器全屏,並且我想讓控件在mousemove
上顯示。由於沒有mousestop
我剛啓動了一個計時器,如果它完成,它將隱藏工具。jQuery mousemove自己觸發
問題 - 由於我懷疑回調的性質,效果不斷在顯示和隱藏之間來回切換。該類mouseMoving
在CSS中添加display: block !important
,當它被刪除,theControls
返回到它的原始的CSS這是display: none !important
$('#theDiv').mousemove(
function()
{
//When the mouse is moving, add the class which adds display: block !important
$('#theControls').addClass('mouseMoving');
// Clear the timer each time the mouse moves, so that if the mouse doesnt move for a full 2 seconds,
// hide the controls by removing the afforementioned class.
clearTimeout(timer);
var timer = setTimeout(
function()
{
$('#theControls').removeClass('mouseMoving');
},
2000);
}
);
事實上,如果你走在全屏幕和移動鼠標,該控件將出現,然後隱藏,然後開發工具將顯示類mouseMoving
正在不斷被追加和刪除,即使我不再移動鼠標,。這將無限期地繼續下去,或者直到鼠標再次移動,然後循環重複。
你爲什麼叫'clearTimer'上'timer'定義之前呢?你不應該在閉包之外定義'timer',所以你可以這樣做嗎? – ahitt6345
而且,你的意思是'clearTimer'或'clearTimeout'?因爲除非你定義了'clearTimer',否則不存在。 – ahitt6345
@ ahitt6345我的意思是'clearTimeout'抱歉,把它改了。 –