一般網頁上滑鼠的游標都會有預設的樣子,像是本來的箭頭,或是移到超連結上的手形,那如果有要修改在不同元素上的滑鼠游標的樣子,就可以使用 CSS 的 cursor 屬性。
所有可以設定的屬性有這些:
.alias {cursor: alias;}
.all-scroll {cursor: all-scroll;}
.auto {cursor: auto;}
.cell {cursor: cell;}
.context-menu {cursor: context-menu;}
.col-resize {cursor: col-resize;}
.copy {cursor: copy;}
.crosshair {cursor: crosshair;}
.default {cursor: default;}
.e-resize {cursor: e-resize;}
.ew-resize {cursor: ew-resize;}
.grab {cursor: grab;}
.grabbing {cursor: grabbing;}
.help {cursor: help;}
.move {cursor: move;}
.n-resize {cursor: n-resize;}
.ne-resize {cursor: ne-resize;}
.nesw-resize {cursor: nesw-resize;}
.ns-resize {cursor: ns-resize;}
.nw-resize {cursor: nw-resize;}
.nwse-resize {cursor: nwse-resize;}
.no-drop {cursor: no-drop;}
.none {cursor: none;}
.not-allowed {cursor: not-allowed;}
.pointer {cursor: pointer;}
.progress {cursor: progress;}
.row-resize {cursor: row-resize;}
.s-resize {cursor: s-resize;}
.se-resize {cursor: se-resize;}
.sw-resize {cursor: sw-resize;}
.text {cursor: text;}
.url {cursor: url(myBall.cur),auto;}
.w-resize {cursor: w-resize;}
.wait {cursor: wait;}
.zoom-in {cursor: zoom-in;}
.zoom-out {cursor: zoom-out;}
可以一一去調整看看,其中我比較常會改動的就是把 a 的標籤改成 pointer,因為預設來說 a 的標籤要搭配 href 的屬性才會變成手形,如果我們用了 a 的標籤但他卻不是超連結(一般常見的就是當按鈕),這時把滑鼠游標改成手形就有助於使用者知道這個是可以互動的按鈕。
又如果想要自訂自己想要的樣子,可以設定為 url(),裡面則是指向圖片的網址,現在圖片格式也支援 png 或 svg 了很方便,而第二個值可以設定為 auto,或是你的圖片與點擊的相對位置有要變動,像是:
.elem {
cursor: url('image.svg') 10 5, move;
}
這樣對應的位置會往右 10px 往下 5px 這樣,有些時候調整位置可以讓使用者點擊的時候不會困惑。