CSS100Day(19)-纯css实现滑动radio轮播图

544 字
3 分钟
CSS100Day(19)-纯css实现滑动radio轮播图

设计简介#

没有必要使用Javascript实现轮播图,直接使用css然后transition: translateX来解决轮播

  1. 知识点1:使用<input type="radio">来实现radio button,然后对于背景图也可translateX轮播

效果展示#

实现代码和步骤#

HTML部分#

HTML部分如下,模拟轮播图的背景是一个.bg元素

<div class="frame">
<div class="center">
<input type="radio" id="check-1" name="rd" />
<label class="circle" for="check-1" id="c1"></label>
<input type="radio" id="check-2" name="rd" />
<label class="circle" for="check-2" id="c2"></label>
<input type="radio" id="check-3" name="rd" />
<label class="circle" for="check-3" id="c3"></label>
<div class="active"></div>
<div class="bg">
<div class="bg-slide bg-1"></div>
<div class="bg-slide bg-2"></div>
<div class="bg-slide bg-3"></div>
</div>
</div>
</div>

CSS部分#

.frame {
width: 400px;
height: 400px;
box-shadow: 1px 2px 10px 0 rgba(0, 0, 0, 0.3);
border-radius: 2px;
position: relative;
}
.center {
width: 400px;
height: 400px;
position: absolute;
inset: 0;
overflow: hidden;
}
input[type="radio"] {
display: none;
}
.circle {
box-sizing: border-box;
position: absolute;
z-index: 5;
top: 175px;
left: 115px;
width: 50px;
height: 50px;
border: 2px solid #fff;
border-radius: 50%;
cursor: pointer;
}
#c2 {
left: 175px;
}
#c3 {
left: 235px;
}
.active {
position: absolute;
z-index: 10;
width: 40px;
height: 40px;
background: #fff;
border-radius: 50%;
top: 180px;
left: 120px;
transition: all 1s ease;
}
#check-1:checked ~ .active {
transform: translateX(0px);
}
#check-2:checked ~ .active {
transform: translateX(60px);
}
#check-3:checked ~ .active {
transform: translateX(120px);
}
.bg {
position: absolute;
width: 400px;
height: 400px;
top: 0;
left: 0;
display: flex;
transition: all 1s ease;
transform: translateX(0px);
}
.bg-slide {
min-width: 400px;
height: 400px;
}
.bg-1 {
background: #9b59b6;
}
.bg-2 {
background: #3498db;
}
.bg-3 {
background: #1abc9c;
}
#check-1:checked ~ .bg {
transform: translateX(0px);
}
#check-2:checked ~ .bg {
transform: translateX(-400px);
}
#check-3:checked ~ .bg {
transform: translateX(-800px);
}
分享给其他人
如果该文章对您有帮助,欢迎分享给其他人阅读!
复制链接

评论区

图片
犟哟
Hello, I'm Juyial.
公告
欢迎来到我的博客,在这里可以一起进步,一起学习!2026年起航!
分类
站点统计
文章
19
分类
1
标签
3
总字数
35,134
运行时长
0
最后活动
0 天前

目录