簡體   English   中英

僅在懸停時用圓圈制作光標

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM