创建流畅的纯CSS文本轮播

聖光之護
发布: 2025-08-25 23:50:01
原创
151人浏览过

创建流畅的纯css文本轮播

本文将指导你如何使用纯CSS创建一个平滑过渡的文本轮播,避免文本重叠问题。通过调整关键帧动画,控制元素的left属性,实现从右到左的无缝滚动效果。我们将提供详细的代码示例和关键步骤,助你轻松构建出美观且实用的文本轮播组件。

纯CSS文本轮播实现详解

创建一个纯CSS文本轮播,关键在于使用@keyframes定义动画,并通过调整元素的left属性来控制文本的显示和隐藏,从而实现轮播效果。以下是如何避免文本重叠,并实现从右到左平滑过渡的步骤:

  1. HTML结构:

    首先,我们需要一个包含轮播项的容器。每个轮播项都应该是一个绝对定位的元素,以便我们可以使用left属性来控制其位置。

    立即学习前端免费学习笔记(深入)”;

    <div class="trev-top-bar overflow-hidden" style="height: 56px;">
      <div class="container-fluid">
        <div class="row position-relative">
          <div class="col-12 col-md trev-top-bar-item text-center position-absolute">
            <i class="trev-top-bar-fa-icon fa-solid fa-truck-fast"></i> Fast Shipping
          </div>
    
          <div class="col-12 col-md trev-top-bar-item text-center position-absolute">
            <i class="trev-top-bar-fa-icon fa-solid fa-arrow-right-arrow-left"></i> 100 Days Right of Return
          </div>
    
          <div class="col-12 col-md trev-top-bar-item text-center position-absolute">
            <i class="trev-top-bar-fa-icon fa-solid fa-file-invoice-dollar"></i> Buy with Invoice
          </div>
    
          <div class="col-12 col-md trev-top-bar-item text-center position-absolute">
            <i class="trev-top-bar-fa-icon fa-solid fa-gears"></i> Proven Quality
          </div>
        </div>
      </div>
    </div>
    登录后复制
  2. CSS样式:

    • .trev-top-bar: 设置背景颜色、文字颜色、字体大小和内边距。overflow-hidden 确保内容超出容器时被隐藏。
    • .trev-top-bar-item: 设置文本居中和绝对定位。
    • .trev-top-bar-fa-icon: 设置图标颜色和右边距。
    .trev-top-bar {
      background-color: #256dff;
      color: #fff;
      font-size: 14px;
      padding-top: 1rem;
      padding-bottom: 1rem;
    }
    
    .trev-top-bar .trev-top-bar-item {
      text-align: center;
      position: absolute;
    }
    
    .trev-top-bar .trev-top-bar-fa-icon,
    .trev-top-bar .icon {
      color: #fff;
      margin-right: .3rem;
    }
    
    .trev-top-bar .trev-top-bar-fa-icon {
      font-size: 16px;
    }
    
    .trev-top-bar .icon svg {
      width: 16px;
      height: 16px;
    }
    登录后复制
  3. 关键帧动画:

    这是实现轮播效果的核心部分。我们需要为每个轮播项定义一个关键帧动画,控制其从右侧移入,显示一段时间,然后从左侧移出。

    @keyframes top-bar-animation-1 {
      0% {
        left: 100%;
      }
      8.33% {
        left: 0;
      }
      16.66% {
        left: 0;
      }
      24.99% {
        left: -100%;
      }
      100% {
        left: -100%;
      }
    }
    
    @keyframes top-bar-animation-2 {
      0% {
        left: 100%;
      }
      24.99% {
        left: 100%;
      }
      33.33% {
        left: 0;
      }
      41.66% {
        left: 0;
      }
      49.99% {
        left: -100%;
      }
      100% {
        left: -100%;
      }
    }
    
    @keyframes top-bar-animation-3 {
      0% {
        left: 100%;
      }
      49.99% {
        left: 100%;
      }
      58.33% {
        left: 0;
      }
      66.66% {
        left: 0;
      }
      74.99% {
        left: -100%;
      }
      100% {
        left: -100%;
      }
    }
    
    @keyframes top-bar-animation-4 {
      0% {
        left: 100%;
      }
      74.99% {
        left: 100%;
      }
      83.33% {
        left: 0;
      }
      91.66% {
        left: 0;
      }
      100% {
        left: -100%;
      }
    }
    登录后复制
    • left: 100%: 元素位于容器右侧,不可见。
    • left: 0: 元素位于容器中心,可见。
    • left: -100%: 元素位于容器左侧,不可见。

    关键点: 每个动画的关键帧百分比需要仔细调整,以确保每个元素在离开屏幕前都有足够的时间显示。同时,错开每个元素的动画起始时间,以创建连续的轮播效果。

  4. 应用动画:

    将定义好的关键帧动画应用到对应的轮播项上。

    .trev-top-bar .trev-top-bar-item:first-child {
      -webkit-animation: top-bar-animation-1 16s ease infinite;
      animation: top-bar-animation-1 16s ease infinite;
    }
    
    .trev-top-bar .trev-top-bar-item:nth-child(2) {
      -webkit-animation: top-bar-animation-2 16s ease infinite;
      animation: top-bar-animation-2 16s ease infinite;
    }
    
    .trev-top-bar .trev-top-bar-item:nth-child(3) {
      -webkit-animation: top-bar-animation-3 16s ease infinite;
      animation: top-bar-animation-3 16s ease infinite;
    }
    
    .trev-top-bar .trev-top-bar-item:nth-child(4) {
      -webkit-animation: top-bar-animation-4 16s ease infinite;
      animation: top-bar-animation-4 16s ease infinite;
    }
    登录后复制
    • animation: 指定动画名称、持续时间、缓动函数和循环次数。

注意事项

  • 动画持续时间: 根据轮播项的数量和期望的轮播速度,调整动画的持续时间。
  • 关键帧百分比: 仔细调整关键帧的百分比,确保每个轮播项都有足够的时间显示,并且避免重叠。
  • 响应式设计: 根据需要在不同的屏幕尺寸下调整样式,以确保轮播在所有设备上都能正常工作。

总结

通过使用纯CSS和关键帧动画,我们可以创建一个平滑过渡的文本轮播,避免文本重叠问题。这种方法简单易用,无需JavaScript,并且可以轻松地定制样式和动画效果。 关键在于理解left属性的控制和关键帧动画的 timing,通过调整这些参数,可以实现各种不同的轮播效果。

以上就是创建流畅的纯CSS文本轮播的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号