本文将指导你如何使用纯css创建一个平滑过渡的文本轮播,避免文本重叠问题。通过调整关键帧动画,控制元素的left属性,实现从右到左的无缝滚动效果。我们将提供详细的代码示例和关键步骤,助你轻松构建出美观且实用的文本轮播组件。
纯CSS文本轮播实现详解
创建一个纯CSS文本轮播,关键在于使用@keyframes定义动画,并通过调整元素的left属性来控制文本的显示和隐藏,从而实现轮播效果。以下是如何避免文本重叠,并实现从右到左平滑过渡的步骤:
-
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>
-
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; }
-
关键帧动画:
这是实现轮播效果的核心部分。我们需要为每个轮播项定义一个关键帧动画,控制其从右侧移入,显示一段时间,然后从左侧移出。
@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%: 元素位于容器左侧,不可见。
关键点: 每个动画的关键帧百分比需要仔细调整,以确保每个元素在离开屏幕前都有足够的时间显示。同时,错开每个元素的动画起始时间,以创建连续的轮播效果。
-
应用动画:
将定义好的关键帧动画应用到对应的轮播项上。
.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,通过调整这些参数,可以实现各种不同的轮播效果。
评论(已关闭)
评论已关闭