具有不透明度和粒子的 CSS 点击动画

CSS click animation with opacity and particles

提问人:H3lltronik 提问时间:9/16/2019 最后编辑:azeósH3lltronik 更新时间:9/16/2019 访问量:901

问:

我想为我的网站制作一个自定义点击动画,我想做这样的事情: 这是从那个 gif

enter image description here 中截取的 我的第一个 aproach 是这样的: expected click animation

enter image description here

它有一些问题,例如,当我点击动画被触发时,动画跟随鼠标而不是停留在点击坐标中,它缺少很多东西,比如那些散布在点击区域的闪亮粒子和模糊的光晕,我不知道该怎么做,有人知道我应该怎么做才能做到这一点?比如,我应该学习或搜索什么才能得到我想要的东西?我缺乏专业知识,所以我真的想要一些小指导或任何东西

我不知道这是否有一点帮助,但我仍然会粘贴我第一种方法的代码

const cursor = document.querySelector('.cursor');

document.addEventListener('mousemove', e => {
  cursor.setAttribute("style", "top: " + (e.pageY - 10) + "px; left: " + (e.pageX - 10) + "px;");
})

document.addEventListener('click', () => {
  cursor.classList.add("expand");

  setTimeout(() => {
    cursor.classList.remove("expand");
  }, 500);
})
body {
  margin: 0;
  height: 100vh;
  background-color: black;
}

.cursor {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  position: absolute;
}

@keyframes cursorAnim3 {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.5);
    opacity: 0.5;
  }
  100% {
    transform: scale(1);
    opacity: 0;
  }
}

.expand {
  animation: cursorAnim3 .3s forwards;
  border: 2px solid rgba(255, 255, 255, 0.7);
}
<div class="cursor"></div>

欢迎任何建议:c

JavaScript HTML CSS

评论

0赞 EGC 9/16/2019
也许你可以调整这样的事情,在你的光标下以更小、更暗的尺度发生?codepen.io/osublake/pen/avbPON
0赞 H3lltronik 9/16/2019
这肯定会对我有所帮助,谢谢!但是外部渐变状边框呢?发光模糊的光环?我做了一些搜索,找到了如何在边框中使用渐变,但结果并不完全是光晕的样子,有什么想法吗?
1赞 EGC 9/16/2019
是的!检查这个!jsfiddle.net/zf18bwen/1,也看到这个(你想要的是一个盒子阴影):html-css-js.com/css/generator/box-shadow

答:

2赞 Rojo 9/16/2019 #1

我添加到您的代码中的是事件中的 if 语句。我不知道如何解释,我只是添加了它,它有效......希望这就是你想要的!:)PS:我还添加了,因为体型正在增加。它位于mousemoveoverflow-x: hiddenoverflow-y: hidden

body {

}

const cursor = document.querySelector('.cursor');

document.addEventListener('mousemove', e => {
  if (cursor.classList.length === 1) {
    cursor.setAttribute("style", "top: " + (e.pageY - 10) + "px; left: " + (e.pageX - 10) + "px;");
  }
})

document.addEventListener('click', () => {
  cursor.classList.add("expand");

  setTimeout(() => {
    cursor.classList.remove("expand");
  }, 500);
})
body {
  margin: 0;
  height: 100vh;
  overflow-x: hidden;
  overflow-y: hidden;
  background-color: black;
}

.cursor {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  position: absolute;
}

@keyframes cursorAnim3 {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.5);
    opacity: 0.5;
  }
  100% {
    transform: scale(1);
    opacity: 0;
  }
}

.expand {
  animation: cursorAnim3 .3s forwards;
  border: 2px solid rgba(255, 255, 255, 0.7);
}
<div class="cursor"></div>

评论

0赞 Rojo 9/16/2019
@H3lltronik 如果这有帮助,请接受这是正确的答案。谢谢!