移动端轮播图swiper

/*
    /*
     1.什么是swiper?
     Swiper是纯javascript打造的滑动特效插件,面向PC、平板电脑等移动终端。
     Swiper能实现触屏焦点图、触屏Tab切换等常用效果。
     Swiper开源、免费、稳定、使用简单、功能强大,是架构移动终端网站的重要选择!
     
     2.如何使用:
     2.1引入swiper对应的css和js文件
     2.2按照框架的需求搭建三层结构
     2.3创建一个Swiper对象, 将容器元素传递给它
      */
*/

示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>04-移动端轮播图</title>
    <link rel="stylesheet" href="css/swiper.css">
    <script src="js/swiper.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
            touch-action: none;
        }
        div{
            width: 100%;
        }
        ul{
            list-style: none;
        }
        ul>li>img{
            width: 100%;
            vertical-align: bottom;
        }
        .my-bullet{
            display: inline-block;
            width: 20px;
            height: 20px;
            background: #fff;
            border-radius: 50%;
            margin: 0 5px;
        }
        .my-bullet-active{
            background: #f40;
            opacity: 1;
        }
        .swiper-button-prev, .swiper-button-next{
            width: 30px;
            height: 50px;
            background: rgba(0,0,0,0.3);
        }
        .swiper-button-prev::before{
            content: "<";
            display: inline-block;
            width: 30px;
            height: 50px;
            line-height: 50px;
            text-align: center;
            font-weight: bold;
            font-size: 30px;
            color: #fff;
        }
        .swiper-button-next::before{
            content: ">";
            display: inline-block;
            width: 30px;
            height: 50px;
            line-height: 50px;
            text-align: center;
            font-weight: bold;
            font-size: 30px;
            color: #fff;
        }
    </style>
</head>
<body>
<div class="swiper-container">
    <ul class="swiper-wrapper">
        <li class="swiper-slide"><img src="images/img1.jpg" alt=""></li>
        <li class="swiper-slide"><img src="images/img2.jpg" alt=""></li>
        <li class="swiper-slide"><img src="images/img3.jpg" alt=""></li>
        <li class="swiper-slide"><img src="images/img4.jpg" alt=""></li>
    </ul>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>
    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
</div>
<script>
    let mySwiper = new Swiper ('.swiper-container', {
        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
            bulletClass : 'my-bullet',
            bulletActiveClass: 'my-bullet-active',
        },
        // 如果需要前进后退按钮
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
        loop: true, // 循环模式选项
        autoplay: {
            delay: 2000,
        },
    });
</script>
</body>
</html>

在这里插入图片描述

发布了133 篇原创文章 · 获赞 1 · 访问量 5433

猜你喜欢

转载自blog.csdn.net/weixin_42139212/article/details/104332493