CSS3 实现文本与图片横向无限滚动动画

文章目录

  • [1. 实现效果](#1. 实现效果)
  • 2.html结构
  • [3. css代码](#3. css代码)

1. 实现效果

gif录屏比较卡,实际很湿滑,因为是css动画实现的

2.html结构

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Infinite Scrolling Animation</title>
</head>

<body>
  <div class="scroll" style="--t: 20s">
    <div>
      <span>HTML</span>
      <span>CSS</span>
      <span>JavaScript</span>
      <span>Vue</span>
      <span>React</span>
      <span>Figma</span>
      <span>Photoshop</span>
    </div>

    <div>
      <span>HTML</span>
      <span>CSS</span>
      <span>JavaScript</span>
      <span>Vue</span>
      <span>React</span>
      <span>Figma</span>
      <span>Photoshop</span>
    </div>
  </div>

  <div class="scroll" style="--t: 30s">
    <div>
      <span>HTML</span>
      <span>CSS</span>
      <span>JavaScript</span>
      <span>Vue</span>
      <span>React</span>
      <span>Figma</span>
      <span>Photoshop</span>
    </div>

    <div>
      <span>HTML</span>
      <span>CSS</span>
      <span>JavaScript</span>
      <span>Vue</span>
      <span>React</span>
      <span>Figma</span>
      <span>Photoshop</span>
    </div>
  </div>

  <div class="scroll" style="--t: 10s">
    <div>
      <span>HTML</span>
      <span>CSS</span>
      <span>JavaScript</span>
      <span>Vue</span>
      <span>React</span>
      <span>Figma</span>
      <span>Photoshop</span>
    </div>

    <div>
      <span>HTML</span>
      <span>CSS</span>
      <span>JavaScript</span>
      <span>Vue</span>
      <span>React</span>
      <span>Figma</span>
      <span>Photoshop</span>
    </div>
  </div>

  <div class="scroll" style="--t: 35s">
    <div>
      <span>HTML</span>
      <span>CSS</span>
      <span>JavaScript</span>
      <span>Vue</span>
      <span>React</span>
      <span>Figma</span>
      <span>Photoshop</span>
    </div>

    <div>
      <span>HTML</span>
      <span>CSS</span>
      <span>JavaScript</span>
      <span>Vue</span>
      <span>React</span>
      <span>Figma</span>
      <span>Photoshop</span>
    </div>
  </div>

  <div class="scroll img-box" style="--t: 25s">
    <div>
      <div class="bg" style="--r: 0;">1</div>
      <div class="bg" style="--r: 40;">2</div>
      <div class="bg" style="--r: 80;">3</div>
      <div class="bg" style="--r: 120;">4</div>
      <div class="bg" style="--r: 160;">5</div>
      <div class="bg" style="--r: 200;">6</div>
      <div class="bg" style="--r: 240;">7</div>
      <div class="bg" style="--r: 280;">8</div>
      <div class="bg" style="--r: 320;">9</div>
    </div>

    <div>
      <div class="bg" style="--r: 0;">1-1</div>
      <div class="bg" style="--r: 40;">2-1</div>
      <div class="bg" style="--r: 80;">3-1</div>
      <div class="bg" style="--r: 120;">4-1</div>
      <div class="bg" style="--r: 160;">5-1</div>
      <div class="bg" style="--r: 200;">6-1</div>
      <div class="bg" style="--r: 240;">7-1</div>
      <div class="bg" style="--r: 280;">8-1</div>
      <div class="bg" style="--r: 320;">9-1</div>
    </div>
  </div>
</body>

</html>

3. css代码

css 复制代码
@import url('https://fonts.googleapis.com/css?family=Poppins:400,600,700,800&display=swap');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}

body {
  min-height: 100vh;
  background-color: #222;
  color: #fff;

  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;  
}

.scroll {
  position: relative;

  display: flex;
  width: 700px;
  overflow: hidden;
  mask-image: linear-gradient(90deg, transparent, #fff 20%, #fff 80%, transparent);
  -webkit-mask-image: linear-gradient(90deg, transparent, #fff 20%, #fff 80%, transparent);
}

.scroll > div span {
  display: inline-block;
  margin: 10px;
  padding: 5px 10px;
  background-color: #333;
  border-radius: 5px;

  letter-spacing: 0.2em;
  text-transform: uppercase;

  cursor: pointer;
  transition: background-color 0.5s;
}
.scroll > div span:hover {
  background-color: #4caf50;
}

.img-box {
  display: flex;
  column-gap: 10px;
}
.img-box > div,
.img-box > div .bg {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
}
.img-box .bg {
  width: 150px;
  height: 150px;
  background-color: #ff3e3e;
  filter: hue-rotate(calc(var(--r) * 1deg));
  
  cursor: pointer;
  transition: filter 0.5s;
}

.scroll > div {
  white-space: nowrap;
  animation: animate var(--t) linear infinite;
  animation-delay: calc(var(--t) * -1);
}
@keyframes animate {
  0% {
    transform: translateX(100%);
  }

  100% {
    transform: translateX(-100%);
  }
}

.scroll > div:nth-child(2) {
  animation: animate2 var(--t) linear infinite;
  animation-delay: calc(var(--t) / -2);
}
@keyframes animate2 {
  0% {
    transform: translateX(0);
  }

  100% {
    transform: translateX(-200%);
  }
}

.scroll:hover > div {
  animation-play-state: paused;
}


@media screen and (max-width: 768px) {
  .scroll {
    width: 95vw;
  }

  .scroll > div span {
    background-color: #4caf50;
  }

  .img-box .bg {
    width: 15vw;
    height: 15vw;
  }
}
相关推荐
荆州克莱16 小时前
mysql中局部变量_MySQL中变量的总结
spring boot·spring·spring cloud·css3·技术
sunshine64117 小时前
【CSS】实现tag选中对钩样式
前端·css·css3
安冬的码畜日常1 天前
【CSS in Depth 2 精译_086】14.3:CSS 剪切路径(clip-path)的用法
前端·css·css3·html5·clip-path·css剪辑·css剪切路径
安冬的码畜日常2 天前
【CSS in Depth 2 精译_088】第五部分:添加动效概述 + 第 15 章:CSS 过渡特效概述 + 15.1:状态间的由此及彼
前端·css·css3·html5·css过渡
孤留光乩3 天前
从零搭建纯前端飞机大战游戏(附源码)
前端·javascript·游戏·html·css3
东离与糖宝3 天前
说说你对 css3 display:flex 弹性盒模型 的理解
前端·css·css3
WebDesign_Mu4 天前
HTML5+CSS3+JS制作精美的宠物主题网站(内附源码,含5个页面)
前端·javascript·css·html·css3·html5·宠物
荆州克莱4 天前
什么是微端游戏?微端应该选择什么配置的服务器?
spring boot·spring·spring cloud·css3·技术
Welkin_qing5 天前
页面无滚动条,里面div各自有滚动条
javascript·css·css3
秃头小锦鲤5 天前
网页核心页面设计(第10章)
前端·javascript·css·html·css3·html5