javascript - 自定义光标的交错移动和另一个问题
问题描述
我为我的网站实现了一个自定义光标,但我想为更大的光标添加延迟。因此我为其添加了一个transition
属性,但它看起来非常交错。
另外,我有一个悬停动画,似乎无法正常工作。当我刷新我的页面并将鼠标悬停在链接上时,第一次它什么也不做,但第二次我将鼠标悬停在它上面时,动画会起作用。
这是CSS代码:
.cursor{
width: 1rem;
height: 1rem;
border-radius: 100%;
background: rgba(255, 255, 255, 0.5);
position: absolute;
transform: translate(-50%, -50%);
z-index: 1000;
pointer-events: none;
transition: all 0.5s ease;
transition-property: width, height;
transform-origin: 100% 100%;
}
.cursor2{
width: 4rem;
height: 4rem;
border-radius: 100%;
border: 2px solid rgba(255, 255, 255, 0.7);
position: absolute;
transform: translate(-50%, -50%);
z-index: 1000;
pointer-events: none;
transition: all 1ms linear;
transition-property: width, height;
transform-origin: 100% 100%;
}
这是JS:
let hover = document.querySelectorAll(".project-link");
let mouseCursor = document.querySelector(".cursor");
let mouseCursor2 = document.querySelector(".cursor2");
hover.forEach(el => {
$(el).hover(function() {
el.addEventListener("mouseover", function(){
mouseCursor.classList.add("hover"),
mouseCursor2.classList.add("hide");
});
el.addEventListener("mouseleave", function(){
mouseCursor.classList.remove("hover"),
mouseCursor2.classList.remove("hide");
});
});
})
window.addEventListener("mousemove", cursor);
function cursor(e){
mouseCursor.style.top = e.pageY + "px",
mouseCursor.style.left = e.pageX + "px";
mouseCursor2.style.top = e.pageY + "px",
mouseCursor2.style.left = e.pageX + "px";
}
这是我必须制作自定义光标的 CSS 和 JS 代码。这就是我所拥有的,但似乎 Stack Overflow 让我写得更多,所以让我进一步解释一下。
因此,当我在刷新页面后将鼠标悬停在链接上时,光标不会动画。基本上,它不会添加所需的类,但如果我第二次将鼠标悬停在它上面而不是在同一时间刷新动画作品,它会将所需的类添加到光标 div。
解决方案
推荐阅读
- python - 如何将张量转换为numpy数组matmul的结果
- r - R - 使用 ~ 将 ggplot2 指定为数据的函数
- swagger - 如何保持 access_token 参数不登录以进行下一个帖子请求
- python - 在 Django 项目中包含 ffmpeg
- elasticsearch - 从 NEST 中的索引名称推断弹性搜索类型
- mysql - 为什么数据库要在 Linux 上创建操作系统用户?
- reactjs - 在 PhpStorm 中使用 JSX 的 Emmet
- mongodb - 我在 mongoDb 中有一个集合。我想获取该集合中的前 10 个文档并将其放入 mongoDB 的另一个集合中
- git - 如何删除源树上的远程分支?
- java - 从(子)目录中的文件中读取文本