将鼠标悬停在所需区域上时,让工具提示跟随光标时遇到了问题。将会在句子中间。 HTML将是这样的
<html>
<div class="paragraph">
I really like <a data-tooltip="Chocolate</a>
</div>
</html>
和CSS
<style>
[data-tooltip] {
position: relative;
z-index: 2;
cursor: pointer;
}
[data-tooltip]:before,
[data-tooltip]:after {
visibility: hidden;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=0);
opacity: 0;
pointer-events: none;
}
[data-tooltip]:before {
position: absolute;
bottom: 150%;
left: 50%;
margin-bottom: 5px;
margin-left: -80px;
padding: 10px;
width: 250px;
border: 1px dotted #fa1e64;
background-color: #000;
background-color: rgba(255, 255, 255, 0.97);
color: #fa1e64;
content: attr(data-tooltip);
text-align: center;
font-size: 1.1rem;
line-height: 1.5;
transform: translate3d(0,-10px,0);
transition: all .15s ease-in-out;
}
[data-tooltip]:hover:before,
[data-tooltip]:hover:after {
opacity: 1;
transform: translate3d(0,0,0);
visibility: visible;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
filter: progid: DXImageTransform.Microsoft.Alpha(Opacity=100)
}
</style>
和Javascript
var tooltip = document.querySelectorAll('data-tooltip');
document.addEventListener('mousemove', fn, false);
function fn(e) {
for (var i=tooltip.length; i--;) {
tooltip[i].style.left = e.pageX + 'px';
tooltip[i].style.top = e.pageY + 'px';
}
}
是否有必要对其进行调整以使其跟随光标?