欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Swiper插件简单使用

程序员文章站 2022-07-10 21:51:22
...

Swiper插件简单使用

官网链接    https://www.swiper.com.cn/api/index.html

源码cdn引入

swiper5
<link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.css">  
<link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.min.css">    
<script src="https://unpkg.com/swiper/js/swiper.js"> </script>  
<script src="https://unpkg.com/swiper/js/swiper.min.js"> </script>

注意的是: 必须同时引入 swiper.js和swiper.css 才能使用swiper插件

使用demo

<!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>Swiper插件使用</title>
    <!-- 引入swiper所需的css和js -->
    <link rel="stylesheet" href="./swiper/swiper.min.css">
    <script src="./swiper//swiper.min.js"></script>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      .container {
        width: 100vw;
        height: 100vh;
        display: flex;
        flex-direction: column;
        overflow: hidden;
      }

      header,
      section,
      footer {
        background: #000;
        line-height: 50px;
        color: rgb(209, 60, 60);
        text-align: center;
      }

      section {
        background: #ccc;
        flex: 1;
      }

      /* ul li {
        list-style: none;
      } */
      .swiper-wrapper{
        width:100%;
        height:0;
        background: #ccc;
        /* 防止掉块,50%需要自己算 */
        padding-bottom: 50%;
      }
      img {
        width: 100%;
      }
    </style>
  </head>

  <body>
    <div class="container">
      <header>11</header>
      <section>
        <div class="swiper-container">
          <div class="swiper-wrapper">
            <div class="swiper-slide"><img src="./img/1.jpg" alt=""></div>
            <div class="swiper-slide"><img src="./img/2.jpg" alt=""></div>
            <div class="swiper-slide"><img src="./img/4.jpg" alt=""></div>
          </div>
          <!-- 如果需要分页器 -->
          <div class="swiper-pagination"></div>

          <!-- 如果需要导航按钮 -->
          <div class="swiper-button-prev"></div>
          <div class="swiper-button-next"></div>

          <!-- 如果需要滚动条 -->
          <!-- <div class="swiper-scrollbar"></div> -->
        </div>
        <!-- <ul>
        <li><img src="./img/1.jpg" alt=""></li>
        <li><img src="./img/2.jpg" alt=""></li>
        <li><img src="./img/3.jpg" alt=""></li>
        <li><img src="./img/4.jpg" alt=""></li>
      </ul> -->
      </section>
      <footer>222</footer>
    </div>
  </body>
  <script>
    var mySwiper = new Swiper('.swiper-container', {
      // direction: 'vertical', // 垂直切换选项
      autoplay:true, //自动轮播 默认3秒
      loop: true, // 循环模式选项

      // 如果需要分页器
      pagination: {
        el: '.swiper-pagination',
      },

      // 如果需要前进后退按钮
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },

      // 如果需要滚动条
      scrollbar: {
        // el: '.swiper-scrollbar',
      },
    })
  </script>

</html>


效果图


Swiper插件简单使用

相关标签: js插件使用篇