css 的pointer-events 属性

2023-03-05,,

1.css 有好多属性,可以让你感觉到不可思议,关键是可以解决一些难以实现的问题,今天遇到一个,就是 point-enevts属性

支持 pointer-events 属性 的浏览器版本

2. 1  point-events 属性的效果:

2.1.1 阻止点击事件的效果

2.1.2 阻止鼠标指针的显示

2.1.3 阻止css 里因为hover 和active 状态的改变而触发的事件

2.1.4 穿透上层元素,实现下层元素的选中效果

阻止点击事件的效果:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
a {
text-decoration: none;
}
span {
cursor: pointer;
}
</style>
</head>
<body>
<div>
<li><a href="https://www.baidu.com/">百度</a></li>
<li><a href="https://www.tmall.com/">天猫,这是一个可以点击的链接</a></li>
<p><span>鼠标手型效果</span></p>
</div>
</body>
</html>

运行效果:1. 鼠标移到百度,天猫,鼠标手型上    都会有一个手型效果

改变css 样式:

 <style>
a {
text-decoration: none;
}
a[href="https://www.tmall.com/"] {
pointer-events: none;
}
span {
cursor: pointer;
}
</style>

运行效果:a 标签的默认手型效果没有,点击默认跳转也没有了

2.2  使用point-events 属性,解除元素的hover效果:

改变css 样式:增加一个 hover 效果

    <style>
a {
text-decoration: none;
}
a[href="https://www.tmall.com/"] {
pointer-events: none;
}
span {
cursor: pointer;
}
p {
background: #ccc;
}
p:hover {
background: #f00;
}
</style>

使用 pointer-events 改变 hover 效果

  <style>
a {
text-decoration: none;
}
a[href="https://www.tmall.com/"] {
pointer-events: none;
}
span {
cursor: pointer;
}
p {
/* 这个是改变hover 的代码 */
pointer-events: none;
}
p {
background: #ccc;
}
p:hover {
background: #f00;
}
</style>

2.3 pointer-events属性改变点击事件的效果:(阻止点击事件)

初始状态:

 <style>
a {
text-decoration: none;
}
a[href="https://www.tmall.com/"] {
pointer-events: none;
}
span {
cursor: pointer;
}
.active {
background: blue;
}
</style>
<script>
window.onload = function() {
$('p>span').on('click', function() {
$('p').toggleClass("active");
});
}
</script>

运行效果:

加入 pointer-events :none  属性改变效果

     <style>
a {
text-decoration: none;
}
a[href="https://www.tmall.com/"] {
pointer-events: none;
}
p {
/* 阻止改变点击事件的效果,连同阻止子元素的点击事件的效果 */
pointer-events: none;
}
span {
cursor: pointer;
}
.active {
background: blue;
}
</style>

运行结果:

2.4 穿透上层元素,直接作用域下层元素:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
span {
color: red;
}
p {
top: 0;
left: 0;
position: absolute;
width: 100%;
height: 300px;
background: #000;
opacity: 0.5;
padding-top: 100px;
}
</style>
</head>
<body>
<div>
<li><a href="https://www.baidu.com/">百度</a></li>
<li><a href="https://www.tmall.com/">天猫,这是一个可以点击的链接</a></li>
<p><span>鼠标手型效果</span></p>
</div>
</body>
</html>

运行结果:鼠标移到a标签上方,没有效果,不会变成手型,点击也没有作用

加入pointer-events:none 透视 p 元素:

 <style>
* {
margin: 0;
padding: 0;
}
span {
color: red;
}
p {
/* 加入pointer-events属性透视p 元素 */
pointer-events: none;
}
p {
top: 0;
left: 0;
position: absolute;
width: 100%;
height: 300px;
background: #000;
opacity: 0.5;
padding-top: 100px;
}
</style>

运行结果:直接透视p元素,可以点击li 下面的a 标签,同时可以正常运行跳转:

鼠标移动a标签上方,鼠标会变成手型,点击后会实现a连接的跳转

css 的pointer-events 属性的相关教程结束。

《css 的pointer-events 属性.doc》

下载本文的Word格式文档,以方便收藏与打印。