
本文将指导你如何使用纯CSS创建一个平滑过渡的文本轮播,避免文本重叠问题。通过调整关键帧动画,控制元素的left属性,实现从右到左的无缝滚动效果。我们将提供详细的代码示例和关键步骤,助你轻松构建出美观且实用的文本轮播组件。
创建一个纯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 {
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%;
}
}关键点: 每个动画的关键帧百分比需要仔细调整,以确保每个元素在离开屏幕前都有足够的时间显示。同时,错开每个元素的动画起始时间,以创建连续的轮播效果。
应用动画:
将定义好的关键帧动画应用到对应的轮播项上。
.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;
}通过使用纯CSS和关键帧动画,我们可以创建一个平滑过渡的文本轮播,避免文本重叠问题。这种方法简单易用,无需JavaScript,并且可以轻松地定制样式和动画效果。 关键在于理解left属性的控制和关键帧动画的 timing,通过调整这些参数,可以实现各种不同的轮播效果。
以上就是创建流畅的纯CSS文本轮播的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号