2011-10-12 73 views
8

我有一個拖動UI程序,其中可拖動元素上的鼠標光標變爲點擊抓取手。更改整個頁面的鼠標光標?

的問題是,我讓拖動到屏幕上的任何地方發生,並在錨標記,等光標變爲...

我試過$('*').addClass('grabbing'),但它真的很貴。

有沒有簡單易用的代碼處理方法?

+0

參見:http://stackoverflow.com/questions/192900/wait-cursor-over-entire-html-page –

回答

5

做它的CSS級別:

* { 
    cursor: pointer; 
} 

做jQuery的力量它遍歷DOM中的每一個節點,並重寫它的CSS類。在一份長文件中,這是浪費時間。

+0

但是,這將始終是積極的。光標只需在鼠標關閉時更改。它需要以某種方式在Javascript中發生。 – Wesley

+0

您可以通過javascript動態添加/刪除css規則。只需從onmousedown/onmouseup中完成,它應該比從頁面上的每個dom節點添加/刪除類都快。 –

+0

@Mark B酷。你能用一個例子提供一個新的答案嗎?我會給它一個鏡頭,如果它的工作正確,我會標記它。 – Wesley

1

試試這個

$('body').addClass('grabbing'); 

OR

$(document).addClass('grabbing'); 

// EDITED ANSWER

$('body').mousedown(function(e){ 
    $(this).css({'cursor':'pointer'}); 
}).mouseup(function(e){ 
    $(this).css({'cursor':'auto'}); 
}); 

如果Firebug是上,您可以看到在機身風格標籤的變化。但有些如何不起作用。你可以把它作爲參考,並嘗試類似的方法獲得解決方案。

+0

不會僱用zIndex的元素重寫? –

+0

@MikeChristensen正確。這不會做到這一點。 – Wesley

0

我能想到做到這一點的唯一方法是相當黑客。

創建一個D-Z,其索引高於該頁面上所有內容的Z值,並且具有所需的光標。只要鼠標光標關閉,就啓用該div。示例代碼:

<html><body style="width: 100%; height: 100%;"> 
<DIV id="cover" style="position: absolute; width: 100%; height: 100%; zindex: 5000; top: 0px; left: 0px; cursor: pointer; background: transparent; display: none;">&nbsp;</div> 

<ul><li>One</li><li>Two</li><li>Three</li></ul> 

<script> 
    window.document.body.onmousedown = function() 
    { 
     document.getElementById('cover').style.display = ''; 
    }; 

    window.document.body.onmouseup = function() 
    { 
     document.getElementById('cover').style.display = 'none'; 
    }; 
</script> 

</body></html>