简体   繁体   English

仅在悬停时用圆圈制作光标

[英]Make cursor with circle only on hover

Hi to all I just wonder if there is possibilities to do same effect as on this link enter link description here but to show circle beside mouse pointer only when hove something.大家好,我只是想知道是否有可能做与此链接相同的效果在此处输入链接描述,但仅在鼠标指针旁边显示圆圈时才显示。

like this:像这样: 在此处输入图像描述

'''https://codepen.io/gmrchk/pen/pQobKL''' '''https://codepen.io/gmrchk/pen/pQobKL'''

It is something more like this below but need to be applied to any link.下面的内容更像这样,但需要应用于任何链接。

 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>

Just use code I found closely to what I need here: Circle follow cursor after hover on button只需使用我在此处找到的与我需要的代码非常接近的代码: 在按钮上悬停后圈出光标

You need a bit of JavaScript which will change the position of a circle when the mouse moves over the text, and makes the circle invisible when the mouse is not over the text.你需要一点 JavaScript 来在鼠标移到文本上时改变圆的位置,并在鼠标不在文本上时使圆不可见。

Here's a simple snippet.这是一个简单的片段。 Obviously you will want to alter the various dimensions to suit your particular requirements.显然,您将希望更改各种尺寸以满足您的特定要求。

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM