首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义div作为游标会导致链接上的悬停问题。

自定义div作为游标会导致链接上的悬停问题。
EN

Stack Overflow用户
提问于 2021-04-17 21:45:24
回答 2查看 211关注 0票数 1

使用绝对div创建的自定义游标有问题,在测试中我意识到自定义div直接定位在默认游标下,如果我悬停一个链接,就无法处理JS“鼠标中心”,因为默认游标总是只悬停在自定义游标上.有办法解决吗?

代码语言:javascript
复制
<div class="custom-cursor"></div>

Scss:

代码语言:javascript
复制
.custom-cursor {
   width: 20px;
   height: 20px;
   border: 2px solid orange;
   position: absolute;
   z-index: 1080;
   border-radius: 50%;
   transition-duration: 100ms;
   transition-timing-function: ease-out;
   box-shadow: 0 0 0 2px black;
   &.hover {
      width: 40px;
      height: 40px;
      background: rgba(#FFCC00,.5);
   }
}

香草JS:

代码语言:javascript
复制
const cursor = document.querySelector('.custom-cursor');
    
// Custom cursor follow the default cursor
document.addEventListener('mousemove', e => {
    cursor.setAttribute('style', 'top: '+(e.pageY - 10)+'px; left: ' +(e.pageX - 10)+'px;')
});

const links = document.querySelectorAll('a');

// Custom cursor change style on hover links
for(let x of links) {

    x.addEventListener('mousenter', () => {
     cursor.classList.add('hover');
    });

    x.addEventListener('mouseleave', () => {
     cursor.classList.remove('hover');
    });
        
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-04-17 22:30:19

您可以对游标-div使用pointer-events: none;,这样就可以通过悬停事件。(你还忘了“鼠标中心”中的e

工作示例:

代码语言:javascript
复制
const cursor = document.querySelector('.custom-cursor');
    
// Custom cursor follow the default cursor
document.addEventListener('mousemove', e => {
    cursor.setAttribute('style', 'top: '+(e.pageY - 10)+'px; left: ' +(e.pageX - 10)+'px;')
});

const links = document.querySelectorAll('a');

// Custom cursor change style on hover links
for(let x of links) {

    x.addEventListener('mouseenter', () => {
     cursor.classList.add('hover');
    });

    x.addEventListener('mouseleave', () => {
     cursor.classList.remove('hover');
    });
        
}
代码语言:javascript
复制
.custom-cursor {
   width: 20px;
   height: 20px;
   border: 2px solid orange;
   position: absolute;
   border-radius: 50%;
   transition-duration: 100ms;
   transition-timing-function: ease-out;
   box-shadow: 0 0 0 2px black;
   pointer-events: none;
}

.custom-cursor.hover {
      width: 40px;
      height: 40px;
      background: rgba(#FFCC00,.5);
}
代码语言:javascript
复制
<div class="custom-cursor"></div>
<a href="#">troet</a>
<a href="#">quak</a>
<a href="#">miau</a>

票数 1
EN

Stack Overflow用户

发布于 2021-04-17 22:39:51

You can enable clicking through elements by adding pointer-events: none; to your CSS

代码语言:javascript
复制
.custom-cursor {
   pointer-events: none; /*don't interact with this div*/
   width: 20px;
   height: 20px;
   border: 2px solid orange;
   position: absolute;
   z-index: 1080;
   border-radius: 50%;
   transition-duration: 100ms;
   transition-timing-function: ease-out;
   box-shadow: 0 0 0 2px black;
   &.hover {
      width: 40px;
      height: 40px;
      background: rgba(#FFCC00,.5);
   }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67143235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档