【发布时间】:2011-10-12 13:33:36
【问题描述】:
我正在尝试禁用图像上的突出显示,当我用鼠标在图像上移动并拖动时, 看一看 :
非常感谢!
【问题讨论】:
-
这是 google 上某些搜索的最高结果。我认为this 是你们许多人正在寻找的!
我正在尝试禁用图像上的突出显示,当我用鼠标在图像上移动并拖动时, 看一看 :
非常感谢!
【问题讨论】:
尝试将其作为css背景而不是img元素。
【讨论】:
img {
-khtml-user-select: none;
-o-user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
user-select: none;
}
【讨论】:
你可以试试这个(这不适用于所有浏览器):
img::-moz-selection {
background-color: transparent;
color: #000;
}
img::selection {
background-color: transparent;
color: #000;
}
或者您可以使用设置了适当宽度和高度的<div>,并在其上使用 CSS 背景图像。例如,我在我的网站上使用它:
<div id="header"></div>
#header {
height: 79px;
width: 401px;
background: url(http://nclabs.org/images/header.png) no-repeat;
}
最后,您可以使用 Javascript 以编程方式禁用它。
【讨论】:
这禁用了 DOM 元素上的突出显示:
function disableSelection(target){
if (typeof target.onselectstart!="undefined") // if IE
target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") // if Firefox
target.style.MozUserSelect="none";
else // others
target.onmousedown=function(){return false;}
target.style.cursor = "default";
}
像这样使用它:
disableSelection(document.getElementById("my_image"));
【讨论】:
使用user-select 属性:
img{
-khtml-user-select: none;
-o-user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
user-select: none;
}
【讨论】:
draggable 属性。在 img 标签中将其设置为 false 将避免用户意外(或不)拖动它时出现重影。
img{
-ms-user-select: none; /* IE 10+ */
-moz-user-select: none; /* Firefox all */
-webkit-user-select: none; /* Chrome all / Safari all */
user-select: none; /* Likely future */
}
【讨论】:
如果这里有人对 sass mixin 感兴趣:
// Prevent users to select an element
@mixin no-select {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
【讨论】:
要从整个网站中删除选择的文本和图像,请使用正文选择器
body {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
【讨论】:
如果您在单击图像时遇到问题,这里是解决方案。
img {
-webkit-tap-highlight-color: transparent;
}
如果不起作用,请在包含图像整个宽度的图像的父级中尝试。
【讨论】: