前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用css实现一个轮播图效果

用css实现一个轮播图效果

作者头像
小明爱学习
发布2021-01-18 10:47:46
6300
发布2021-01-18 10:47:46
举报
文章被收录于专栏:smh的技术文章smh的技术文章

依靠于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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年01月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档