问题
把“按钮”的点击区域放大一些
答案
使用 ::before
或者 ::after`` 伪元素,利用 relative + absolute
定位,伪元素设为负数,给一个更大的区域
理解
通过添加伪元素可以伪造更大的点击区域原因:
- 利用
relative + absolute
定位不会改变又来的文档流和布局 - 伪元素继承了元素的 cursor 属性
- 伪元素是原来点击节点的子节点,点击伪元素能触发事件并冒泡给父元素
代码
<!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>
.area {
width: 100px;
text-align: center;
border: 1px solid #ccc;
cursor: pointer;
position: relative;
}
.area::before{
content: "";
position: absolute;
border: 1px solid #fa0000;
top: -10px;
left: -10px;
bottom: -10px;
right: -10px;
}
</style>
</head>
<body style="margin: 30px">
<div class="area">原点击区域</div>
</body>
</html>
网友评论