![](/img/trans.png)
[英]:hover { cursor: pointer } on circle created via borderradius will make it a pointer even outside the circle
[英]Make cursor with circle only on hover
大家好,我只是想知道是否有可能做与此链接相同的效果在此处输入链接描述,但仅在鼠标指针旁边显示圆圈时才显示。
'''https://codepen.io/gmrchk/pen/pQobKL'''
下面的内容更像这样,但需要应用于任何链接。
const btn = document.querySelector(".button") const circle = document.querySelector(".circle") btn.onmouseenter = function() { circle.classList.add("in") } btn.onmousemove = function(e) { const { top, left, width, height } = btn.getBoundingClientRect() const { clientY, clientX } = e if (clientX < left || clientY < top || clientX > left + width || clientY > top + height) { circle.classList.remove("in") } circle.style.top = `${clientY - top}px` circle.style.left = `${clientX - left}px` };
body { margin: 20px; padding: 20px; } .button { padding: 40px 80px; border: 1px solid grey; color: blue; display: inline-block; position: relative; } .circle { position: absolute; display: none; width: 30px; height: 30px; border-radius: 50%; top: 0; left: 0; transform: translate(-50%, -50%); border: 2px solid red; } .circle.in { display: block; }
<a class="button"> Button <span class="circle"></span> </a>
只需使用我在此处找到的与我需要的代码非常接近的代码: 在按钮上悬停后圈出光标
你需要一点 JavaScript 来在鼠标移到文本上时改变圆的位置,并在鼠标不在文本上时使圆不可见。
这是一个简单的片段。 显然,您将希望更改各种尺寸以满足您的特定要求。
const circle = document.querySelector('.circle'); const hoverDiv = document.querySelector('.hoverDiv'); hoverDiv.addEventListener('mousemove', e => { circle.style.display = 'block'; circle.style.left = e.clientX - 48 + 'px'; circle.style.top = e.clientY - 48 + 'px'; }); hoverDiv.addEventListener('mouseout', e => { circle.style.display = 'none'; });
* { margin: 0; } .container { width: 100vw; height: 100vh; display: flex; align-items: center; justify-content: center; } .hoverDiv { display: : inline-block; cursor: default; width: fit-content; font-size: 64px; } .circle { width: 64px; height: 64px; background-color: cornflowerblue; border-radius: 50%; position: absolute; display: none; z-index: -1; }
<div class="container"> <div class="circle"></div> <div class="hoverDiv">Here is some text to hover</div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.