HTML5+CSS3小实例:纯CSS实现轮播图效果

实例:纯CSS实现轮播图效果
技术栈:HTML+CSS
效果:

源码:
【html】

<!DOCTYPE html>
<html>
 
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
 
    <title>纯CSS实现轮播图效果</title>
    <link rel="stylesheet" href="../css/61.css">
</head>
 
<body>
    <div class="swipe">
        <input type="radio" name="indicator" id="indicator1" hidden checked>
        <label for="indicator1"></label>
        <input type="radio" name="indicator" id="indicator2" hidden>
        <label for="indicator2"></label>
        <input type="radio" name="indicator" id="indicator3" hidden>
        <label for="indicator3"></label>
 
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
        </ul>
    </div>
</body>
 
</html>

【css】

*{
    /* 初始化 */
    margin: 0;
    padding: 0;
}
body{
    /* 100%窗口高度 */
    height: 100vh;
    /* 弹性布局 居中 */
    display: flex;
    justify-content: center;
    align-items: center;
    /* 渐变背景 */
    background: linear-gradient(200deg,#eef1f5,#e6e9f0);
}
.swipe{
    /* 相对定位 */
    position: relative;
    width: 800px;
    height: 500px;
    text-align: center;
    color: #fff;
    overflow: hidden;
}
.swipe ul{
    margin: 10px 0;
    padding: 0;
    /* 计算ul总宽度 */
    width: calc(800px * 3);
    transition: 0.5s;
}
.swipe li{
    float: left;
    width: 800px;
    height: 500px;
    list-style: none;
    line-height: 500px;
    font-size: 50px;
}
.swipe li:nth-child(1){
    background-color: lightcoral;
}
.swipe li:nth-child(2){
    background-color: lightseagreen;
}
.swipe li:nth-child(3){
    background-color: lightsalmon;
}
/* for属性值以“indicator”开头的所有label元素(指示器) */
.swipe label[for^="indicator"]{
    /* 绝对定位 */
    position: absolute;
    top: 450px;
    width: 20px;
    height: 20px;
    background-color: rgba(255,255,255,0.3);
    border-radius: 50%;
    margin: 0 10px;
    cursor: pointer;
    transition: 0.3s;
}
/* for属性值为“indicator1”的label元素 */
.swipe label[for="indicator1"]{
    left: 42%;
}
/* for属性值为“indicator2”的label元素 */
.swipe label[for="indicator2"]{
    left: 47%;
}
/* for属性值为“indicator3”的label元素 */
.swipe label[for="indicator3"]{
    left: 52%;
}
/* 选中指示器设置ul的margin-left */
#indicator1:checked ~ ul{
    margin-left: 0;
}
#indicator2:checked ~ ul{
    margin-left: -800px;
}
#indicator3:checked ~ ul{
    margin-left: -1600px;
}
/* 选中指示器改变指示器颜色 */
#indicator1:checked ~ label[for="indicator1"]{
    background-color: #fff;
}
#indicator2:checked ~ label[for="indicator2"]{
    background-color: #fff;
}
#indicator3:checked ~ label[for="indicator3"]{
    background-color: #fff;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容