<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>基于CSS的轮播图</title> </head> <style> body{ padding: 0px; margin: 0px; } ul{ height: 280px; background: gray; position: relative; margin: 0px; overflow-x:hidden; } ul li{ height: 280px; width: 100%; position: absolute; top: 0; left: 0; color: white; font-size: 50px; list-style: none; display: flex; align-items: center; justify-content: center; transition: .5s transform ease-in-out; } ul li:nth-of-type(1){ background: pink; } ul li:nth-of-type(2){ background: green; margin-left: 100%; } ul li:nth-of-type(3){ background: blue; margin-left: 200%; } ._footer{ width: 100%; z-index: 10; position: absolute; bottom: 12px; left: 0px; text-align: center; } ._footer label{ display: inline-block; width: 20px; height: 20px; background: white; border-radius: 50%; } /* ~ 获取同层级的后元素,对同层级的前边元素不生效 */ ul input[type='radio']:nth-of-type(1):checked ~ ._footer label:nth-of-type(1){ background: black; } ul input[type='radio']:nth-of-type(2):checked ~ ._footer label:nth-of-type(2){ background: black; } ul input[type='radio']:nth-of-type(3):checked ~ ._footer label:nth-of-type(3){ background: black; } ul input[type='radio']:nth-of-type(1):checked ~ .slide{ transform: translateX(0%); } ul input[type='radio']:nth-of-type(2):checked ~ .slide{ transform: translateX(-100%); } ul input[type='radio']:nth-of-type(3):checked ~ .slide{ transform: translateX(-200%); } </style> <body> <div>设计思路:
1、用ul和li标签,ul用相对定位,li标签用绝对定位
2、设置input标签的id,在通过label标签的for属性实现input的checked选中,input选中后再通过元素选择器实现轮播图的切换和label样式的改变
</div> <ul> <input id="check1" type="radio" name="control" checked/> <input id="check2" type="radio" name="control"/> <input id="check3" type="radio" name="control"/> <li class="slide">1</li> <li class="slide">2</li> <li class="slide">3</li> <div class="_footer"> <label for="check1"></label> <label for="check2"></label> <label for="check3"></label> </div> </ul> </body> </html>
标签:基于,checked,轮播,nth,ul,background,input,type,CSS From: https://www.cnblogs.com/ibear/p/17140928.html