我有一个列表,并且有一个用于其项目的点击处理程序:
<ul>
<li>foo</li>
<li>goo</li>
</ul>
如何将鼠标指针更改为手形指针(如将鼠标悬停在按钮上)?现在,当我将鼠标悬停在列表项上时,该指针将变为文本选择指针。
cursor: grab
我有一个列表,并且有一个用于其项目的点击处理程序:
<ul>
<li>foo</li>
<li>goo</li>
</ul>
如何将鼠标指针更改为手形指针(如将鼠标悬停在按钮上)?现在,当我将鼠标悬停在列表项上时,该指针将变为文本选择指针。
cursor: grab
Answers:
正如人们所说,鉴于时间的流逝,您现在可以放心使用:
li { cursor: pointer; }
cursor: pointer
对于IE 5.5以上的版本,仅做就足够了:quirksmode.org/css/cursor.html
pointer
这个问题已经有5年了。
cursor:pointer
仍然可以使用。因此,如果有使用的借口cursor:hand
,那就不再存在。
用于li
:
li:hover {
cursor: pointer;
}
运行snippet选项后,查看带有示例的更多光标属性:
.auto { cursor: auto; }
.default { cursor: default; }
.none { cursor: none; }
.context-menu { cursor: context-menu; }
.help { cursor: help; }
.pointer { cursor: pointer; }
.progress { cursor: progress; }
.wait { cursor: wait; }
.cell { cursor: cell; }
.crosshair { cursor: crosshair; }
.text { cursor: text; }
.vertical-text { cursor: vertical-text; }
.alias { cursor: alias; }
.copy { cursor: copy; }
.move { cursor: move; }
.no-drop { cursor: no-drop; }
.not-allowed { cursor: not-allowed; }
.all-scroll { cursor: all-scroll; }
.col-resize { cursor: col-resize; }
.row-resize { cursor: row-resize; }
.n-resize { cursor: n-resize; }
.e-resize { cursor: e-resize; }
.s-resize { cursor: s-resize; }
.w-resize { cursor: w-resize; }
.ns-resize { cursor: ns-resize; }
.ew-resize { cursor: ew-resize; }
.ne-resize { cursor: ne-resize; }
.nw-resize { cursor: nw-resize; }
.se-resize { cursor: se-resize; }
.sw-resize { cursor: sw-resize; }
.nesw-resize { cursor: nesw-resize; }
.nwse-resize { cursor: nwse-resize; }
.cursors > div {
float: left;
box-sizing: border-box;
background: #f2f2f2;
border:1px solid #ccc;
width: 20%;
padding: 10px 2px;
text-align: center;
white-space: nowrap;
&:nth-child(even) {
background: #eee;
}
&:hover {
opacity: 0.25
}
}
<h1>Example of cursor</h1>
<div class="cursors">
<div class="auto">auto</div>
<div class="default">default</div>
<div class="none">none</div>
<div class="context-menu">context-menu</div>
<div class="help">help</div>
<div class="pointer">pointer</div>
<div class="progress">progress</div>
<div class="wait">wait</div>
<div class="cell">cell</div>
<div class="crosshair">crosshair</div>
<div class="text">text</div>
<div class="vertical-text">vertical-text</div>
<div class="alias">alias</div>
<div class="copy">copy</div>
<div class="move">move</div>
<div class="no-drop">no-drop</div>
<div class="not-allowed">not-allowed</div>
<div class="all-scroll">all-scroll</div>
<div class="col-resize">col-resize</div>
<div class="row-resize">row-resize</div>
<div class="n-resize">n-resize</div>
<div class="s-resize">s-resize</div>
<div class="e-resize">e-resize</div>
<div class="w-resize">w-resize</div>
<div class="ns-resize">ns-resize</div>
<div class="ew-resize">ew-resize</div>
<div class="ne-resize">ne-resize</div>
<div class="nw-resize">nw-resize</div>
<div class="se-resize">se-resize</div>
<div class="sw-resize">sw-resize</div>
<div class="nesw-resize">nesw-resize</div>
<div class="nwse-resize">nwse-resize</div>
</div>
您不需要jQuery,只需使用以下CSS内容:
li {cursor: pointer}
和瞧!便利。
CSS:
.auto { cursor: auto; }
.default { cursor: default; }
.none { cursor: none; }
.context-menu { cursor: context-menu; }
.help { cursor: help; }
.pointer { cursor: pointer; }
.progress { cursor: progress; }
.wait { cursor: wait; }
.cell { cursor: cell; }
.crosshair { cursor: crosshair; }
.text { cursor: text; }
.vertical-text { cursor: vertical-text; }
.alias { cursor: alias; }
.copy { cursor: copy; }
.move { cursor: move; }
.no-drop { cursor: no-drop; }
.not-allowed { cursor: not-allowed; }
.all-scroll { cursor: all-scroll; }
.col-resize { cursor: col-resize; }
.row-resize { cursor: row-resize; }
.n-resize { cursor: n-resize; }
.e-resize { cursor: e-resize; }
.s-resize { cursor: s-resize; }
.w-resize { cursor: w-resize; }
.ns-resize { cursor: ns-resize; }
.ew-resize { cursor: ew-resize; }
.ne-resize { cursor: ne-resize; }
.nw-resize { cursor: nw-resize; }
.se-resize { cursor: se-resize; }
.sw-resize { cursor: sw-resize; }
.nesw-resize { cursor: nesw-resize; }
.nwse-resize { cursor: nwse-resize; }
您还可以使光标成为图像:
.img-cur {
cursor: url(images/cursor.png), auto;
}
我认为仅在JavaScript可用时才显示手/指针光标会很聪明。因此人们不会感到可以单击不可点击的内容。
为此,您可以使用JavaScript库jQuery将CSS添加到元素中,如下所示
$("li").css({"cursor":"pointer"});
或直接将其链接到点击处理程序。
或者,当将Modernizer与结合<html class="no-js">
使用时,CSS如下所示:
.js li { cursor: pointer; }
对于完整的跨浏览器,请使用:
cursor: pointer;
cursor: hand;
只需执行以下操作即可:
li {
cursor: pointer;
}
我将其应用于您的代码以查看其工作原理:
注意:同样不要忘记您可以使用任何带有自定义光标的手形光标,例如,可以创建像这样的收藏夹手形图标:
div {
display: block;
width: 400px;
height: 400px;
background: red;
cursor: url(http://findicons.com/files/icons/1840/free_style/128/hand.png) 4 12, auto;
}
<div>
</div>
为了使任何东西都能获得“ mousechange”待遇,您可以添加一个CSS类:
我不会说要使用cursor:hand
它,因为它仅对Internet Explorer 5.5及更低版本有效,并且Internet Explorer 6随Windows XP(2002)一起提供。人们只有在浏览器停止为他们工作时才会得到升级的提示。此外,在Visual Studio中,它将在该条目下加上红色下划线。它告诉我:
验证(CSS 3.0):“手”不是“光标”属性的有效值
所有其他响应都建议使用标准CSS指针,但是,有两种方法:
将CSS属性cursor:pointer;
应用于元素。(当光标悬停在按钮上时,这是默认样式。)
cursor:url(pointer.png);
使用自定义图形为指针应用CSS属性。如果要确保所有平台上的用户体验都相同(而不是让浏览器/ OS决定指针光标的外观),则可能更希望这样做。请注意,如果找不到图片,则可以添加后备选项,包括辅助网址或其他任何选项,例如cursor:url(pointer.png,fallback.png,pointer);
当然,这些可以以这种方式li{cursor:pointer;}
,作为类.class{cursor:pointer;}
或作为每个元素的style属性的值应用于列表项style="cursor:pointer;"
。
对于基本的手形符号:
尝试
cursor: pointer
如果您想要一个手形符号,例如拖放一些项目,请尝试:
cursor: grab
您还可以使用以下样式:
li {
cursor: grabbing;
}
注意:不建议这样做,因为这被认为是不好的做法
将内容包装在包含href
属性的锚标记中将可以工作,而无需显式地应用cursor: pointer;
具有锚属性(由CSS修改)的副作用的属性:
<a href="#" style="text-decoration: initial; color: initial;"><div>This is bad practice, but it works.</div></a>
检查以下内容。我从W3Schools获得。
<!DOCTYPE html>
<html>
<head>
<style>
.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: -webkit-grab; cursor: grab;}
.grabbing {cursor: -webkit-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;}
</style>
</head>
<body>
<h1>The cursor property</h1>
<p>Mouse over the words to change the mouse cursor.</p>
<p class="alias">alias</p>
<p class="all-scroll">all-scroll</p>
<p class="auto">auto</p>
<p class="cell">cell</p>
<p class="context-menu">context-menu</p>
<p class="col-resize">col-resize</p>
<p class="copy">copy</p>
<p class="crosshair">crosshair</p>
<p class="default">default</p>
<p class="e-resize">e-resize</p>
<p class="ew-resize">ew-resize</p>
<p class="grab">grab</p>
<p class="grabbing">grabbing</p>
<p class="help">help</p>
<p class="move">move</p>
<p class="n-resize">n-resize</p>
<p class="ne-resize">ne-resize</p>
<p class="nesw-resize">nesw-resize</p>
<p class="ns-resize">ns-resize</p>
<p class="nw-resize">nw-resize</p>
<p class="nwse-resize">nwse-resize</p>
<p class="no-drop">no-drop</p>
<p class="none">none</p>
<p class="not-allowed">not-allowed</p>
<p class="pointer">pointer</p>
<p class="progress">progress</p>
<p class="row-resize">row-resize</p>
<p class="s-resize">s-resize</p>
<p class="se-resize">se-resize</p>
<p class="sw-resize">sw-resize</p>
<p class="text">text</p>
<p class="url">url</p>
<p class="w-resize">w-resize</p>
<p class="wait">wait</p>
<p class="zoom-in">zoom-in</p>
<p class="zoom-out">zoom-out</p>
</body>
</html>
https://developer.mozilla.org/zh-CN/docs/Web/CSS/cursor
/* Keyword value */
cursor: pointer;
cursor: auto;
/* URL, with a keyword fallback */
cursor: url(hand.cur), pointer;
/* URL and coordinates, with a keyword fallback */
cursor: url(cursor1.png) 4 12, auto;
cursor: url(cursor2.png) 2 2, pointer;
/* Global values */
cursor: inherit;
cursor: initial;
cursor: unset;
演示
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
手图像
li:hover{
cursor: url("../icons/hand.cur"), pointer;
}