我的页面上的小元素的边框半径设置为50%,因此它们显示为小点:
CSS:
.star {
width: 5px;
height: 5px;
background-color: rgb(236, 236, 236);
position: absolute;
border-radius: 50%;
transform: scale(1);
display: block;
transition: 0.25s ease-in background-color, 0.25s ease-in opacity, 0.25s ease-out transform;
cursor: pointer;
}
Run Code Online (Sandbox Code Playgroud)
每一个都有一个悬停动作,会弹出一个弹出窗口。但是,现在存在一个问题,即悬停(至少在我测试过的浏览器中)是一个寻找像素的游戏。
是否有一个“技巧”在点上添加一个不可见的边框,使它们在不寻找像素的情况下更具选择性?
设置border为2px solid transparent仅能使测试中的圆圈变大,而CSS outline不会产生:hover状态或mouseenter事件。
尝试这个:
.star {
width: 10px;
height: 10px;
padding:10px;
background:#000;
border-radius:50%;
background-clip:content-box; /* <- key point*/
}
.star:hover { background-color:#f00; }
Run Code Online (Sandbox Code Playgroud)
<div class="star"></div>Run Code Online (Sandbox Code Playgroud)
增加填充将为您带来更大的命中利润。
使用伪元素增加“命中区域”
body {
background: #000;
}
.star {
width: 5px;
height: 5px;
background-color: rgb(236, 236, 236);
position: absolute;
left: 50%;
top: 50%;
border-radius: 50%;
transform: scale(1);
display: block;
transition: 0.25s ease-in background-color, 0.25s ease-in opacity, 0.25s ease-out transform;
cursor: pointer;
}
.star::before {
content: '';
position: absolute;
border-radius: 50%;
width: 500%;
height: 500%;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
z-index:-1;
border:1px solid green; /* for demo purposes */
}
.star:hover {
background: #f00;
}Run Code Online (Sandbox Code Playgroud)
<div class="star"></div>Run Code Online (Sandbox Code Playgroud)