我正在尝试禁用图片的高亮效果,当我将鼠标拖动到图片上时,它会被高亮显示。
看一下:
非常感谢!
![enter image description here](https://istack.dev59.com/ph2m8.webp)
使用 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
将避免用户意外(或非意外)拖动时出现幽灵图像。 - Herickimg::-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编程方式禁用它。
img {
-khtml-user-select: none;
-o-user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
user-select: none;
}
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"));
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 */
}
img {
-webkit-tap-highlight-color: transparent;
}
body {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
如果这里有人对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;
}