首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从自定义光标中删除缓动

如何从自定义光标中删除缓动
EN

Stack Overflow用户
提问于 2021-08-12 11:21:43
回答 2查看 155关注 0票数 1

我在这里得到了一个用户的帮助,在我创建的自定义光标中添加了缓动,但我使用的是jQuery,帮助我的人提供了一个非常好用的普通JS解决方案,但现在我需要从光标中删除缓动,这是从那时起一直在做的。

代码语言:javascript
复制
  let cursor = document.querySelector('#custom-cursor');

  document.addEventListener('mousemove', evt => {
    document.body.classList.add('custom-cursor-moved')

    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|Windows Phone/i.test(navigator.userAgent)) {
      $('#custom-cursor').remove();
    } else {
      cursor.style.display = 'block';
    }


    let {
      clientX: x,
      clientY: y
    } = evt;
    let scale = 1;

    if (evt.target.matches('a,span,[onclick],img,video,i')) {
      cursor.classList.add('active');
      scale = 0.5;
    } else {
      cursor.classList.remove('active');
    }

    cursor.style.transform = `translate(${x}px, ${y}px) scale(${scale})`;

  });
代码语言:javascript
复制
* {
  cursor: none;
}


.custom-cursor-moved,
.custom-cursor-moved * {
  cursor: none !important;
}


#custom-cursor {
  display: none;
  position: fixed;
  width: 20px;
  height: 20px;
  top: -10px;
  left: -10px;
  border: 2px solid black;
  border-radius: 50%;
  opacity: 1;
  background-color: #fb4d98;
  pointer-events: none;
  z-index: 99999999;
  transition: transform ease-out 0.15s, border 0.5s, opacity 0.5s, background-color 0.5s;
}

#custom-cursor.active {
  opacity: 0.5;
  background-color: #000;
  border: 2px solid #fb4d98;
}
代码语言:javascript
复制
<div id="custom-cursor"></div>
<span>HOVER OVER ME</span>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-12 11:36:18

要从x/y更改中删除easing,需要删除translate(x,y)

代码语言:javascript
复制
cursor.style.transform = `scale(${scale})`;

然后可以直接添加x/y (而不是通过转换)

代码语言:javascript
复制
cursor.style.top = `${y}px`
cursor.style.left = `${x}px`

更新的代码段

代码语言:javascript
复制
let cursor = document.querySelector('#custom-cursor');

  document.addEventListener('mousemove', evt => {
    document.body.classList.add('custom-cursor-moved')

    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|Windows Phone/i.test(navigator.userAgent)) {
      $('#custom-cursor').remove();
    } else {
      cursor.style.display = 'block';
    }


    let {
      clientX: x,
      clientY: y
    } = evt;
    let scale = 1;

    if (evt.target.matches('a,span,[onclick],img,video,i')) {
      cursor.classList.add('active');
      scale = 0.5;
    } else {
      cursor.classList.remove('active');
    }

    cursor.style.transform = `scale(${scale})`;
    cursor.style.top = `${y}px`
    cursor.style.left = `${x}px`

  });
代码语言:javascript
复制
#custom-cursor {
  display: none;
  position: fixed;
  width: 20px;
  height: 20px;
  top: -10px;
  left: -10px;
  border: 2px solid black;
  border-radius: 50%;
  opacity: 1;
  background-color: #fb4d98;
  pointer-events: none;
  z-index: 99999999;
  transition: transform ease-out 0.15s, border 0.5s, opacity 0.5s, background-color 0.5s;
}

#custom-cursor.active {
  opacity: 0.5;
  background-color: #000;
  border: 2px solid #fb4d98;
}
代码语言:javascript
复制
<div id="custom-cursor"></div>
<span>HOVER OVER ME</span>

票数 3
EN

Stack Overflow用户

发布于 2021-08-12 11:29:25

导致缓解效果的行如下:

代码语言:javascript
复制
transition: transform ease-out 0.15s, border 0.5s, opacity 0.5s, background-color 0.5s;

所以只要删除它就可以做你想做的事情。如果您习惯了jQuery,那么您可能正在寻找javascript本身的解决方案。

不可能编写css来影响缩放的过渡,但不能影响位置。如果这是您需要的,那么您必须将其包装在另一个元素中。

代码语言:javascript
复制
 <div id="cursor-position"><div id="custom-cursor"></div></div>
代码语言:javascript
复制
#cursor-position {
  position: fixed;
}
代码语言:javascript
复制
let cursorPosition = document.querySelector("#cursor-position");
let cursor = document.querySelector("#custom-cursor");
// ...
cursor.style.transform = `scale(${scale})`;
cursorPosition.style.transform = `translate(${x}px, ${y}px)`;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68756527

复制
相关文章

相似问题

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