值得一看
广告
彩虹云商城
广告

热门广告位

创建流畅的纯CSS文本轮播

创建流畅的纯css文本轮播

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

纯CSS文本轮播实现详解

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

  1. HTML结构:

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

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

    <div class="trev-top-bar overflow-hidden">
    <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,通过调整这些参数,可以实现各种不同的轮播效果。

温馨提示: 本文最后更新于2025-08-26 10:39:59,某些文章具有时效性,若有错误或已失效,请在下方留言或联系在线客服
文章版权声明 1 本网站名称: 创客网
2 本站永久网址:https://new.ie310.com
1 本文采用非商业性使用-相同方式共享 4.0 国际许可协议[CC BY-NC-SA]进行授权
2 本站所有内容仅供参考,分享出来是为了可以给大家提供新的思路。
3 互联网转载资源会有一些其他联系方式,请大家不要盲目相信,被骗本站概不负责!
4 本网站只做项目揭秘,无法一对一教学指导,每篇文章内都含项目全套的教程讲解,请仔细阅读。
5 本站分享的所有平台仅供展示,本站不对平台真实性负责,站长建议大家自己根据项目关键词自己选择平台。
6 因为文章发布时间和您阅读文章时间存在时间差,所以有些项目红利期可能已经过了,能不能赚钱需要自己判断。
7 本网站仅做资源分享,不做任何收益保障,创业公司上收费几百上千的项目我免费分享出来的,希望大家可以认真学习。
8 本站所有资料均来自互联网公开分享,并不代表本站立场,如不慎侵犯到您的版权利益,请联系79283999@qq.com删除。

本站资料仅供学习交流使用请勿商业运营,严禁从事违法,侵权等任何非法活动,否则后果自负!
THE END
喜欢就支持一下吧
点赞14赞赏 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容