css实现文字滚动

87 阅读1分钟
<!DOCTYPE html>
<html lang="en">

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

    .container {
      width: 300px;
      height: 100px;
      box-sizing: border-box;
      border: 1px solid #000;
      position: relative;
      left: 50%;
      top: 100px;
      transform: translateX(-50%);
      overflow: hidden;
    }

    .box {
      height: 20px;
      background-color: pink;
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      font-size: 14px;
      color: white;
      line-height: 20px;
      animation: 5s wordLoop linear infinite normal;
      white-space: nowrap;
    }

    @keyframes wordLoop {
      0% {
        transform: translate(300px, -50%);
      }

      100% {
        transform: translate(-100%, -50%);
      }
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="box">
      我是一个小韭菜~~
    </div>
  </div>
</body>

</html>