欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【代码】js - 触碰 logo 字节跳动

程序员文章站 2022-05-20 14:57:08
...

【代码】js - 触碰 logo 字节跳动

思路:

  • 先把字符串取出来,转换成 span 包裹的字符
  • 监听 mouseover 事件,添加关键帧
  • 监听 animationend事件,移除 class
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    body {
      width: 200vh;
      height: 100vh;
      /*弹性盒模型*/
      display: flex;
      /*对齐方式*/
      justify-content: center;
      align-items: center;
      background: #a29bfe;
    }

    div {
      font-size: 5em;
      font-weight: bold;
      text-transform: uppercase;
      color: #00cec9;
    }

    div>span {
      position: relative;
      display: inline-block;
    }

    .color {
      /*关键帧名*/
      animation-name: color;
      /*过程时间*/
      animation-duration: 1s;
      /*重复次数*/
      animation-iteration-count: 1;
      /*动画方式*/
      animation-timing-function: linear;
      /*动画来回切换*/
      animation-direction: alternate;
    }

    @keyframes color {
      50% {
        color: #ffeaa7;
        /*缩放x2*/
        transform: scale(2);
      }

      to {
        color: #fd79a8;
        transform: scale(0.5);
      }
    }
  </style>
</head>

<body>
  <div>lawsssscat</div>
</body>
<script>
  const div = document.querySelector('div');
  [...div.textContent].reduce((previous, current, index) => {
    // 替换字符
    previous == index && (div.innerHTML = "");
    let span = document.createElement('span');
    span.innerHTML = current;
    div.appendChild(span);
    // 动画开始
    span.addEventListener('mouseover', (e) => {
      e.target.classList.add('color')
    })
    // 动画结束
    span.addEventListener('animationend', (e) => {
      e.target.classList.remove('color');
    })
  }, 0);
</script>

</html>
相关标签: # ECMAscript 6