我用纯HTML和CSS实现了包含五个图像的幻灯片显示。我尽量避免使用JavaScript。幻灯片效果很好,但是现在我想添加按钮以转到下一张幻灯片并返回到上一张。但是,它没有用。我该如何实现?
这是重要的代码段。我认为一种解决方案是使用单选按钮,但我无法使其正常工作。
.slider {
width: 100%;
max-width: 100%;
height: 300px;
margin: auto;
position: relative;
}
.sliderbox {
width: 100%;
overflow: hidden;
position: absolute;
}
.slider ul {
width: 500%;
margin: 0;
padding: 0;
-webkit-animation: slide 40s infinite alternate;
-moz-animation: slide 40s infinite alternate;
animation: slide 40s infinite alternate;
}
.slider ul:hover {
-webkit-animation-play-state: paused;
-moz-animation-play-state: paused;
animation-play-state: paused;
}
.slider li {
float: left;
width: 20%;
height: 300px;
list-style-type: none;
position: relative;
}
.slider img {
position: absolute;
left: 50%;
top: 50%;
-webkit-transform: translateY(-50%) translateX(-50%);
max-width: 100%;
}
.slider li .slider-caption {
position: absolute;
bottom: 0;
text-align: center;
left: 0;
background-color: rgba(255, 255, 255, 0.8);
padding: 2%;
width: 96%;
}
.slider li .slider-text {
padding: 5%;
background-color: #f5f5f5;
}
.slider li .slider-text ul {
width: 100%;
animation: none;
-webkit-animation: none;
-moz-animation: none;
padding: 0 20px;
}
.slider li .slider-text ul li {
list-style-type: inherit;
list-style-position: outside;
padding-left: 25px;
float: none;
height: auto;
}
@keyframes slide {
0% {
margin-left: 0%;
}
21% {
margin-left: 0%;
}
23% {
margin-left: -100%;
}
43% {
margin-left: -100%;
}
45% {
margin-left: -200%;
}
65% {
margin-left: -200%;
}
67% {
margin-left: -300%;
}
87% {
margin-left: -300%;
}
89% {
margin-left: -400%;
}
100% {
margin-left: -400%;
}
}
@-webkit-keyframes slide {
0% {
margin-left: 0%;
}
21% {
margin-left: 0%;
}
23% {
margin-left: -100%;
}
43% {
margin-left: -100%;
}
45% {
margin-left: -200%;
}
65% {
margin-left: -200%;
}
67% {
margin-left: -300%;
}
87% {
margin-left: -300%;
}
89% {
margin-left: -400%;
}
100% {
margin-left: -400%;
}
}
@-moz-keyframes slide {
0% {
margin-left: 0%;
}
21% {
margin-left: 0%;
}
23% {
margin-left: -100%;
}
43% {
margin-left: -100%;
}
45% {
margin-left: -200%;
}
65% {
margin-left: -200%;
}
67% {
margin-left: -300%;
}
87% {
margin-left: -300%;
}
89% {
margin-left: -400%;
}
100% {
margin-left: -400%;
}
}
table a {
display: block;
}
<div class="slider">
<div class="sliderbox">
<ul>
<li>
<img src="https://via.placeholder.com/250C/O%20https://placeholder.com/" alt="" />
</li>
<li>
<img src="https://via.placeholder.com/250C/O%20https://placeholder.com/" alt="" />
</li>
<li>
<img src="https://via.placeholder.com/250C/O%20https://placeholder.com/" alt="" />
</li>
<li>
<img src="https://via.placeholder.com/250C/O%20https://placeholder.com/" alt="" />
</li>
<li>
<img src="https://via.placeholder.com/250C/O%20https://placeholder.com/" alt="" />
</li>
</ul>
</div>
</div>