2010-04-17 204 views
14

我正在玩HTML5和一些JavaScript來製作一個小畫板。每當我用chrome點擊畫布時,光標變成文本光標。我試圖把光標放在CSS中,但這似乎不起作用。這一定是一件容易的事情,但我看它並不能在任何地方找到它html5畫布手形光標問題

+4

那麼,首先它的「光標:指針」不是「光標:手」...... – animuson 2010-04-17 20:46:24

回答

7

使用pointer爲你的光標屬性,而不是像這樣:

canvas { cursor: pointer; } 

hand是IE /歌劇具體, you can see a full list of which cursors work in which browsers here

+0

特別是這個註釋在所提到的鏈接中很有幫助http://www.quirksmode.org/css/cursor.html#note – 2010-04-17 20:56:23

+0

我把它放進去,但仍然在做同樣的事情。檢查它在這裏:http://p-func.com/html5_test/test2.html – pfunc 2010-04-17 21:30:40

+0

@pfunc - 我無法直接調試您的頁面,它有一些無效的屬性,需要更正...'',他們不能像'

18

使用畫布上的禁用文本選擇。這像一個魅力。

var canvas = document.getElementById('canvas'); 
canvas.onselectstart = function() { return false; } // ie 
canvas.onmousedown = function() { return false; } // mozilla 

乾杯, 克里斯

+2

美麗!我一直在尋找一個解決方案,誰會夢見它如此簡單?對於互聯網的啓發:「返回錯誤」方法也可以防止光標在Chrome中更改爲i型光束。我知道阿特伍德不喜歡在他們的評論中口頭表達感激之情,但無論如何謝謝你! – Toji 2011-01-17 05:42:51

+2

對於webkit瀏覽器,您還可以添加'body {-webkit-user-select:none; ''到你的CSS。 – Jacksonkr 2011-09-01 17:07:23

+0

不錯!這節省了我不少時間! – BaronVonKaneHoffen 2012-02-26 16:38:17

12

而其他人是絕對的指你的怪異模式參考的一聲,這不會解決你所遇到的問題,而且基本上你需要實現的變化克里斯的回答。

在我自己的實現,我發現,防止鼠標按下事件默認行爲是被要求停止討厭的文本選擇光標一切:

function handleMouseDown(evt) { 
    evt.preventDefault(); 
    evt.stopPropagation(); 

    // you can change the cursor if you want 
    // just remember to handle the mouse up and put it back :) 
    evt.target.style.cursor = 'move'; 

    // rest of code goes here 
} 

document.addEventListener('mousedown', handleMouseDown, false); 

希望有所幫助。

乾杯, 達蒙。

+0

是的,這確實有幫助。謝謝! – shino 2012-08-04 02:27:26