HTML 部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>纯CSS实现轮播图</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<ul class="slides">
<input type="radio" name="control" id="control-1" checked>
<input type="radio" name="control" id="control-2">
<input type="radio" name="control" id="control-3">
<li class="slide">1</li>
<li class="slide">2</li>
<li class="slide">3</li>
<div class="controls-visible">
<label for="control-1"></label>
<label for="control-2"></label>
<label for="control-3"></label>
</div>
</ul>
</body>
</html>
CSS部分
*{
margin: 0;
padding: 0;
}
body{
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
ul.slides{
position: relative;
width: 600px;
height: 280px;
list-style: none;
background-color: skyblue;
overflow: hidden;
}
li.slide{
width: inherit;
height: inherit;
position: absolute;
top: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;
font-family: Helvetica;
font-size: 120px;
color: rgb(196, 21, 21);
transition: .5s transform ease-in-out;
}
.slide:nth-of-type(1){
background-color: #ccc;
}
.slide:nth-of-type(2){
background-color: khaki;
left:100%
}
.slide:nth-of-type(3){
background-color: rgb(134, 93, 124);
left:200%
}
input[type="radio"]{
position: relative;
z-index: 100;
display: none;
}
.controls-visible{
position: absolute;
width: 100%;
bottom: 12px;
text-align: center;
}
.controls-visible label{
display: inline-block;
width: 10px;
height: 10px;
background-color: white;
border-radius: 50%;
margin: 0 3px;
border:2px solid #FFF
}
.slides input[type="radio"]:nth-of-type(1):checked ~ .controls-visible label:nth-of-type(1){
background-color: #333;
}
.slides input[type="radio"]:nth-of-type(2):checked ~ .controls-visible label:nth-of-type(2){
background-color: #333;
}
.slides input[type="radio"]:nth-of-type(3):checked ~ .controls-visible label:nth-of-type(3){
background-color: #333;
}
.slides input[type="radio"]:nth-of-type(1):checked ~ .slide{
transform: translate(0%);
}
.slides input[type="radio"]:nth-of-type(2):checked ~ .slide{
transform: translate(-100%);
}
.slides input[type="radio"]:nth-of-type(3):checked ~ .slide{
transform: translate(-200%);
}
参考链接