• Swiper实现轮播效果


    swiper官网:https://3.swiper.com.cn/

    文章目录

    html实现

    DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Documenttitle>
        <link
          rel="stylesheet"
          href="https://unpkg.com/swiper@8/swiper-bundle.min.css"
        />
        <script src="https://unpkg.com/swiper@8/swiper-bundle.min.js">script>
      head>
      <style>
        .swiper {
          width: 620px;
          height: 720px;
        }
    
        /* 左右箭头 */
        .swiper-button-prev:after {
          display: none;
        }
        .swiper-button-prev {
          background: url("./image/left-1.png");
          background-size: contain;
          background-repeat: no-repeat;
          width: 110px;
          height: 120px;
          left: 5px;
        }
        .swiper-button-next:after {
          display: none;
        }
        .swiper-button-next {
          background: url("./image/right-1.png");
          background-size: contain;
          background-repeat: no-repeat;
          width: 110px;
          height: 120px;
          right: 0px;
          z-index: 999;
          position: absolute;
        }
    
        /* 图片显示 */
        .swiper-slide {
          display: flex;
          justify-content: center;
          align-items: center;
        }
        .swiper-slide img {
          width: 300px;
        }
        .swiper-slide-active img {
          position: absolute;
          width: 395px;
          transition: width 0.5s;
        }
        .swiper-slide-next img {
          position: absolute;
          left: -230px;
        }
        .swiper-slide-prev img {
          position: absolute;
          right: -230px;
        }
        .swiper-slide-active {
          z-index: 999;
        }
        .swiper-slide-next {
          z-index: 888;
        }
        .swiper-slide-prev {
          z-index: 888;
        }
        .bullet-style {
          width: 0px;
          height: 0px;
          border: 6px solid black;
          background-color: black;
          background-clip: padding-box;
          transform: rotateZ(45deg);
          display: inline-block;
          margin: 0 8px;
          cursor: pointer;
        }
        .bullet-style:hover {
          border-color: gray;
          background-color: gray;
        }
        .bullet-style-active {
          border-style: double;
          border-width: 8px;
        }
      style>
      <body>
        <div class="swiper">
          <div class="swiper-wrapper">
            <div class="swiper-slide">
              <img src="./image/world-3-3-1.png" />
            div>
            <div class="swiper-slide">
              <img src="./image/world-4-4-1.png" />
            div>
            <div class="swiper-slide">
              <img src="./image/world-2-2-1.png" />
            div>
          div>
          
          <div class="swiper-pagination">div>
    
          
          <div class="swiper-button-prev">div>
          <div class="swiper-button-next">div>
        div>
        
        <script type="module">
          import Swiper from "https://unpkg.com/swiper@8/swiper-bundle.esm.browser.min.js";
          let addBullentsEvent = () => {};
          const mySwiper = new Swiper(".swiper", {
            loop: true, // 循环模式选项
            // 分页器
            pagination: {
              el: ".swiper-pagination",
              clickable: true,
              // 自定义分页其样式
              type: "custom",
              renderCustom: function (swiper, current, total) {
                let renderHTML = "";
                for (let i = 0; i < total; i++) {
                  if (i + 1 == current) {
                    renderHTML += `
    `
    ; } else { renderHTML += `
    `
    ; } } return renderHTML; }, }, // 前进后退按钮 navigation: { nextEl: ".swiper-button-next", prevEl: ".swiper-button-prev", }, slidesPerView: 2, //设置slider容器能够同时显示的slides数量 spaceBetween: 80, //slide之间的距离(单位px) centeredSlides: true, //设定为true时,active slide会居中,而不是默认状态下的居左。 observer: true, //修改swiper自己或子元素时,重新加载 observeParents: true, //修改swiper的父元素时,重新加载 centerInsufficientSlides: true, //当slides 的总数小于 slidesPerView 时,slides居中。 autoplay: { delay: 3000, //3秒切换一次 }, // 事件 on: { paginationRender: function () { console.log("分页器渲染了"); // 重新添加事件 addBullentsEvent(); }, autoplayStart: function () { console.log("开始自定切换"); }, autoplayStop: function () { console.log("关闭自动切换"); }, }, }); addBullentsEvent = () => { const bullents = document.getElementsByClassName("bullet-style"); for (let i = 0; i < bullents.length; i++) { console.log("obj.onclick", bullents[i].onclick); bullents[i].removeEventListener("click", () => {}); bullents[i].addEventListener("click", function () { mySwiper.slideToLoop(i); //切换到对应的slide mySwiper.autoplay.start(); }); } }; window.addEventListener("load", () => { addBullentsEvent(); document .getElementsByClassName("swiper-button-next")[0] .addEventListener("click", () => { mySwiper.autoplay.start(); }); document .getElementsByClassName("swiper-button-prev")[0] .addEventListener("click", () => { mySwiper.autoplay.start(); }); });
    script> 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
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201

    效果
    在这里插入图片描述

    vue实现

    swiperComponent.vue

    <template>
      <div class="container">
        <swiper-container init="false">
          <template v-if="imageList.length > 3">
            <swiper-slide v-for="item in imageList" :key="item">
              <img :src="item" />
            swiper-slide>
          template>
          <template v-else>
            <swiper-slide v-for="item in imageList" :key="item">
              <img :src="item" />
            swiper-slide>
            <swiper-slide v-for="item in imageList" :key="item">
              <img :src="item" />
            swiper-slide>
          template>
        swiper-container>
      div>
    template>
    <script setup>
    import { onMounted, onBeforeUnmount } from "vue";
    import { register } from "swiper/element/bundle";
    // 注册swiper组件 —— 引入swiper-container和swiper-slide组件
    register();
    const props = defineProps({
      imageList: {
        type: Array,
        default: () => [
          "https://gd-hbimg.huaban.com/5c32ab9c3fc4ff636fe89aa78f6de3104b5c6c3526000-vKlkAM_fw240webp",
          "https://gd-hbimg.huaban.com/7beade1e1d116a61c35fc380cd0bc220833b2eba2fdc7-vSPKhr_fw658webp",
          // "https://gd-hbimg.huaban.com/36c59291bb5de1038b18999f296b22a738848a0a48f75-cm8YJS",
          // "/images/rabbit/preheat/world-3-3-1.png",
        ],
      },
    });
    // 分页器style
    const swiperPaginationStyle = `
      .bullet-style {
        width: 0px;
        height: 0px;
        border: 6px solid black;
        background-color: black !important;
        background-clip: padding-box;
        transform: rotateZ(45deg);
        display: inline-block;
        margin: 0 8px;
        cursor: pointer;
      }
      .bullet-style:hover {
        border-color: gray;
        background-color: gray;
      }
      .bullet-style-active {
        border-style: double;
        border-width: 8px;
      }`;
    // 前进后退按钮style
    const swiperNavigationStyle = `
      .swiper-button-prev svg {
        display: none;
      }
      .swiper-button-prev {
        background: url("/images/rabbit/preheat/left.png");
        background-size: contain;
        background-repeat: no-repeat;
        width: 110px;
        height: 120px;
        left: 5px;
      }
      .swiper-button-next svg {
        display: none;
      }
      .swiper-button-next {
        background: url("/images/rabbit/preheat/right.png");
        background-size: contain;
        background-repeat: no-repeat;
        width: 110px;
        height: 120px;
        right: 0px;
        z-index: 999;
        position: absolute;
      }
      `;
    
    onMounted(() => {
      const swiperEl = document.querySelector("swiper-container");
      const swiperParams = {
        loop: true, // 循环模式选项
        // loopAdditionalSlides: 1,
        // 分页器
        pagination: {
          clickable: true,
          // 自定义分页其样式
          type: "custom",
          renderCustom: function (swiper, current, total) {
            const activeLength =
              props.imageList.length > 3 ? total : props.imageList.length;
    
            current =
              props.imageList.length > 3
                ? current
                : current % props.imageList.length || props.imageList.length;
            let renderHTML = "";
            for (let i = 0; i < activeLength; i++) {
              if (i + 1 == current) {
                renderHTML += `
    `
    ; } else { renderHTML += `
    ${i})">
    `
    ; } } return renderHTML; }, }, // 前进后退按钮 navigation: true, slidesPerView: 2, //设置slider容器能够同时显示的slides数量 spaceBetween: 80, //slide之间的距离(单位px) centeredSlides: true, //设定为true时,active slide会居中,而不是默认状态下的居左。 observer: true, //修改swiper自己或子元素时,重新加载 observeParents: true, //修改swiper的父元素时,重新加载 centerInsufficientSlides: true, //当slides 的总数小于 slidesPerView 时,slides居中。 // autoplay: { // delay: 2000, //3秒切换一次 // }, lazy: { loadPrevNext: true, // 允许将延迟加载应用到最接近的slide的图片 loadPrevNextAmount: 3, }, injectStyles: [swiperPaginationStyle, swiperNavigationStyle], // 不是生效为什么 // injectStylesUrls: ["./index.css"], }; Object.assign(swiperEl, swiperParams); swiperEl.initialize(); }); onBeforeUnmount(() => { const swiperEl = document.querySelector("swiper-container"); swiperEl.destroy(); });
    script> <style lang="less"> body::-webkit-scrollbar { // 隐藏滚动条 display: none; /* Chrome, Safari and Opera */ -ms-overflow-style: none; /* IE and Edge */ scrollbar-width: none; /* Firefox */ } swiper-container { width: 620px; height: 720px; } swiper-slide { display: flex; justify-content: center; align-items: center; position: relative; transition: 0.5s; img { width: 300px; } } swiper-slide.swiper-slide-active { img { position: absolute; width: 395px; transition: width 0.5s; } z-index: 999; } swiper-slide.swiper-slide-prev { img { position: absolute; right: -230px; } z-index: 888; } swiper-slide.swiper-slide-next { img { position: absolute; left: -230px; } z-index: 888; } style>
    • 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
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187

    效果:
    在这里插入图片描述

  • 相关阅读:
    web开发初级工程师学习笔记
    rust的Defef和DerefMut学习
    奇想大白话之《羊了个羊》为何火,技术很厉害吗?
    微信小程序获取剪切板的内容到输入框中
    刷题笔记(js)
    DC3算法相关题目
    【修车案例】一波形一案例(9)
    德思特分享丨一文带你了解ADC测试参数有哪些?
    linux下的文本处理工具awk学习
    在 KubeSphere 上部署 Apache Pulsar
  • 原文地址:https://blog.csdn.net/mantou_riji/article/details/136377626