如何使用关键帧从右向左滑动循环图像
我有这样的 html:
<div class="container-fluid">
<div class="about-section">
<div class="slider">
<div class="slider-row card">
<img class="slider-column card-background" src="https://images.unsplash.com/photo-1557004396-66e4174d7bf6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="Road Map">
<img class="slider-column card-background" src="https://images.unsplash.com/photo-1557004396-66e4174d7bf6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="Road Map">
<img class="slider-column card-background" src="https://images.unsplash.com/photo-1557004396-66e4174d7bf6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="Road Map">
<img class="slider-column card-background" src="https://images.unsplash.com/photo-1557004396-66e4174d7bf6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="Road Map">
<img class="slider-column card-background" src="https://images.unsplash.com/photo-1557004396-66e4174d7bf6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="Road Map">
<img class="slider-column card-background" src="https://images.unsplash.com/photo-1557004396-66e4174d7bf6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="Road Map">
</div>
</div>
</div>
</div>
这是我的 css:
.slider {
display: flex;
}
.slider .slider-row {
width: 100%;
top: calc(50% - 97.25px);
overflow: hidden;
}
.slider .slider-row .slider-column {
width: 160ppx;
height: 194.5px;
background-size: 2956px 194.5px;
animation: slide-left 50s linear infinite;
}
@keyframes slide-left {
0%{transform:translateX(0)}to{transform:translateX(-3200px)}
}
.card {
list-style: none;
position: relative;
filter: brightness(0.9) saturate(0);
}
.card:before {
content: "";
display: block;
padding-bottom: 150%;
width: 100%;
}
.card-background {
background-size: cover;
background-position: center;
border-radius: 24px;
bottom: 0;
filter: brightness(0.75) saturate(1.2) contrast(0.85);
left: 0;
position: absolute;
right: 0;
top: 0;
transform-origin: center;
trsnsform: scale(1) translateZ(0);
transition: filter 200ms linear, transform 200ms linear;
}
.card:hover {
filter: unset;
}
.card:hover .card-background {
transform: scale(1.05) translateZ(0);
cursor: pointer;
}
我想要实现的目标是在循环中从右到左无限滑动这些图像。因此,当第一个项目向左移动时,其余的应该从右侧的开头开始。你认为我只能用CSS来做吗?每个答案都将受到赞赏。
I have html like this:
<div class="container-fluid">
<div class="about-section">
<div class="slider">
<div class="slider-row card">
<img class="slider-column card-background" src="https://images.unsplash.com/photo-1557004396-66e4174d7bf6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="Road Map">
<img class="slider-column card-background" src="https://images.unsplash.com/photo-1557004396-66e4174d7bf6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="Road Map">
<img class="slider-column card-background" src="https://images.unsplash.com/photo-1557004396-66e4174d7bf6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="Road Map">
<img class="slider-column card-background" src="https://images.unsplash.com/photo-1557004396-66e4174d7bf6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="Road Map">
<img class="slider-column card-background" src="https://images.unsplash.com/photo-1557004396-66e4174d7bf6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="Road Map">
<img class="slider-column card-background" src="https://images.unsplash.com/photo-1557004396-66e4174d7bf6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60" alt="Road Map">
</div>
</div>
</div>
</div>
And here is my css:
.slider {
display: flex;
}
.slider .slider-row {
width: 100%;
top: calc(50% - 97.25px);
overflow: hidden;
}
.slider .slider-row .slider-column {
width: 160ppx;
height: 194.5px;
background-size: 2956px 194.5px;
animation: slide-left 50s linear infinite;
}
@keyframes slide-left {
0%{transform:translateX(0)}to{transform:translateX(-3200px)}
}
.card {
list-style: none;
position: relative;
filter: brightness(0.9) saturate(0);
}
.card:before {
content: "";
display: block;
padding-bottom: 150%;
width: 100%;
}
.card-background {
background-size: cover;
background-position: center;
border-radius: 24px;
bottom: 0;
filter: brightness(0.75) saturate(1.2) contrast(0.85);
left: 0;
position: absolute;
right: 0;
top: 0;
transform-origin: center;
trsnsform: scale(1) translateZ(0);
transition: filter 200ms linear, transform 200ms linear;
}
.card:hover {
filter: unset;
}
.card:hover .card-background {
transform: scale(1.05) translateZ(0);
cursor: pointer;
}
What I am trying to achieve slide these images from the right to left infinitely in loop. So when the first item goes to the left, the rest of it should start from the beginning of the right. Do you think I can do it only with css? Every answer will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,你想要实现的目标是用普通的 css 不可能实现的。您需要始终计算 x 坐标才能知道每张幻灯片的位置。我的建议是使用一些滑块 javascript 库。我最喜欢的是 swiperjs。它允许很多功能来显示幻灯片。但是有很多 js 库可供选择。
If I have understood right, what you are trying to achieve it is just not possible with plain css. You need to calculate x coord all the time in order to know where each slide is. My recommendation is to use some slider javascript library. My favorite is swiperjs. It allows a lot of features to display slides. But there are many options of js libraries to use.