当前位置:   article > 正文

【H5】 swiper轮播图效果插件(适配所有屏幕)_h5轮播插件

h5轮播插件

【H5】 swiper轮播图效果插件(适配所有屏幕)

效果图如下:

当屏幕发生变化 轮播图等比例不变!
在这里插入图片描述

全部代码如下:

<!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>
  <!-- 引入轮播图css样式  -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.0/css/swiper.min.css">
  <!-- 引入轮播图js样式  -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.0/js/swiper.min.js"></script>
  <script>
    //rem为html的字体大小 通过改变html的字体大小达到适配的效果
    remChange();
    //监听屏幕改变resize事件 触发remChange方法
    window.addEventListener("resize", remChange)
    function remChange() {
      const html = document.querySelector("html");
      const width = html.getBoundingClientRect().width; //拿到适配器的宽度值
       //若屏幕宽为375px则1rem = 100px 若不是则按比例增大或减小
      html.style.fontSize = width / 3.75 + 'px';
    }


    //页面加载后执行
    window.onload = function () {
      var mySwiper = new Swiper('.swiper-container', {
        //direction: 'vertical', // 垂直切换选项
        loop: true, // 循环模式选项
        //分页器小圆点
        pagination: {
          el: '.swiper-pagination',
        },
        autoplay: {
          delay: 1000, //自动轮播时间
          disableOnInteraction: true  //用户操作后不轮播
        },
        // 如果需要前进后退按钮
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev',
        },
        // 如果需要滚动条
        // scrollbar: {
        //   el: '.swiper-scrollbar',
        // },
      })
    }
  </script>
  <style>
    body {
      font-size: 40px;
    }
    .swiper-container {
      width: 2rem;
      height: 1rem;
    }
    .swiper-slide {
      background-color: aqua;
    }
  </style>
</head>

<body>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide">页面 1</div>
      <div class="swiper-slide">页面 2</div>
      <div class="swiper-slide">页面 3</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>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/296998
推荐阅读
相关标签
  

闽ICP备14008679号