2010-11-19 33 views
1

當在可拖動元素上移動時,我希望光標變成一隻手,一旦鼠標向下移動,直到鼠標向上移動,我想變成一隻「抓」手。什麼是適當的,跨瀏覽器兼容的方式來做到這一點?CSS遊標。現代和普及的方式是什麼?

使用谷歌搜索只會帶來從2000年的網站,與教程在IE6。 BLA!

在那裏有沒有關於這個主題的任何好的現代教程?如果不是,有人需要寫一個。這將成爲一本非常棒的雜誌文章!

回答

4

使用jQuery框架,你可以做這樣的事情:

// define a hover event so that when you hover over and out of the dragable element 
// the cursor changes accordingly 
$('#element').hover(function(){ 
    $(this).css('cursor','move'); 
} , function(){ 
    $(this).css('cursor','default'); 
}); 

// this cursor property is only supported in mozilla, but here you can insert 
// an image as other posters have specified 
// this event changes the cursor when you click the dragable element 
$('#element').mousedown(function(){ 
    $(this).css('cursor','-moz-grabbing'); 
}); 

// this event changes the cursor back to the default type after you let go 
// of the dragable element 
$('#element').mouseup(function() { 
    $(this).css('cursor','default'); 
}); 

對於一個活生生的例子,看看這個:http://jsfiddle.net/EaEe3/讓我知道如果你需要更多的信息。我希望這有幫助。

+0

爲什麼使用JavaScript設置有效的css規則? – MatTheCat 2010-11-19 15:33:15

+0

海報提到了一個可拖動的元素,所以我想如果他使用jQuery拖動,這將補充它。但我同意你的看法......這可以通過CSS發佈。 – Hristo 2010-11-19 15:37:49

+0

你是對的我沒有想到^^' – MatTheCat 2010-11-19 15:43:23

2

的propper方法是使用cursor規則的默認值,如您案 '移動'。

如果你願意,你必須有一個IE瀏覽器的.cur文件,併爲他人PNG/GIF文件的自定義光標,所以你可以寫

cursor:url(notie.png),url(ie.cur),move; 
+0

+1包含IE屬性 – 2010-11-19 16:57:28

相關問題