html+css实现轮播图

1、利用a标签的锚点属性来实现。
2、给对应的图片盒子设置ID属性值。
3、将图片盒子的ID属性值加入对应的a标签中的href属性。
4、图片盒子加浮动或者flex让其在一行内显示。
5、父盒子添加overflow:hidden;属性让超出的图片隐藏。
注意:图片与a标签在同一个父级元素下不同的元素中

示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #wrap{
            width: 600px;
            height: 400px;
            border: 1px solid #000;
            margin: 100px auto;
            position: relative;
        }
        .imgBox{
            width: 600px;
            height: 400px;
            overflow: hidden;
        }
        img{
            width: 600px;
            height: 400px;
        }
        .btnBox{
            width: 150px;
            height: 30px;
            background: rgba(00, 00,00, 0.5)  ;  
            position: absolute;
            right: 10px;
            bottom: 10px;
        }
        a{
            float: left;
            width: 30px;
            height: 30px;
            color: #fff;
            text-align: center;
            line-height: 30px;
            text-decoration: none
        }
    </style>
</head>
<body>
    <div id="wrap">
        <!-- 图片 -->
        <div class="imgBox">
            <img src="" alt="" id="img1">
            <img src="" alt="" id="img2">
            <img src="" alt="" id="img3">
            <img src="" alt="" id="img4">
            <img src="" alt="" id="img5">
        </div>
        <!-- 按钮 -->
        <div class="btnBox">
            <a href="#img1">1</a>
            <a href="#img2">2</a>
            <a href="#img3">3</a>
            <a href="#img4">4</a>
            <a href="#img5">5</a>
        </div>
    </div>
</body>
</html>

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐