依靠于input的for属性,我们可以使用radio控件的checked来用css实现一个轮播图效果。
代码如下:
代码语言:javascript复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://www.sammh.com/css/reset.css" rel="stylesheet" />
<style>
label{
margin-right: 5px;
border: 1px solid #4C70B1;
border-radius: 100%;
width: 15px;
height: 15px;
cursor: pointer;
transition: ease 300ms;
display:inline-block;
}
input[type=radio]:nth-child(1):checked~div.cur label:nth-child(1){
background-color: #4C70B1;
}
input[type=radio]:nth-child(2):checked~div.cur label:nth-child(2){
background-color: #4C70B1;
}
input[type=radio]:nth-child(3):checked~div.cur label:nth-child(3){
background-color: #4C70B1;
}
input[type=radio]:nth-child(1):checked~div.bg>ul>li:not(:nth-child(1)){
display: none;
}
input[type=radio]:nth-child(2):checked~div.bg>ul>li:not(:nth-child(2)){
display: none;
}
input[type=radio]:nth-child(3):checked~div.bg>ul>li:not(:nth-child(3)){
display: none;
}
.main{
position: relative;
width: 300px;
height: 200px;
margin: 100px auto;
}
.main1{
float: left;
}
.bg{
position: absolute;
width: 100%;
height: inherit;
overflow: hidden;
z-index: 8;
}
.bg>ul{
display: flex;
width: 100%;
}
.bg>ul>li{
height: 200px;
flex: 1;
}
.bg>ul>li:nth-child(1){
background-color: violet;
}
.bg>ul>li:nth-child(2){
background-color:burlywood;
}
.bg>ul>li:nth-child(3){
background-color:darkkhaki;
}
.cur{
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
z-index: 9;
}
</style>
</head>
<body>
<div class="main">
<input type="radio" id="radio1" name="gender" checked hidden />
<input type="radio" id="radio2" name="gender" hidden />
<input type="radio" id="radio3" name="gender" hidden />
<div class="bg">
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<div class="cur">
<label for="radio1"></label>
<label for="radio2"></label>
<label for="radio3"></label>
</div>
</div>
</body>
</html>
在线代码:https://codepen.io/jack-shangs/pen/WNGYGMN