swiper 左右保留部分

6 篇文章 0 订阅
6 篇文章 0 订阅

在这里插入图片描述

1、cdn引入


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Swiper demo</title>
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1"
    />
    <!-- Link Swiper's CSS -->
    <link rel="stylesheet" href="https://unpkg.com/swiper@8/swiper-bundle.css">    
    <script src="https://unpkg.com/swiper@8/swiper-bundle.js"> </script> 
    <!-- Demo styles -->
    <style>
      html,
      body {
        position: relative;
        margin: 0;
        padding: 0;
      }

      body {
        background: #eee;
      }

      .swiper {
        width: 100%;
        height: 300px;
      }

      .swiper-slide {
        text-align: center;
        font-size: 18px;
        background: #fff;
      }

      .swiper-slide {
        width: 60%;
      }
    </style>
  </head>

  <body>
    <!-- Swiper -->
    <div class="swiper mySwiper">
      <div class="swiper-wrapper">
        <div class="swiper-slide">Slide 1</div>
        <div class="swiper-slide">Slide 2</div>
        <div class="swiper-slide">Slide 3</div>
        <div class="swiper-slide">Slide 4</div>
        <div class="swiper-slide">Slide 5</div>
        <div class="swiper-slide">Slide 6</div>
        <div class="swiper-slide">Slide 7</div>
        <div class="swiper-slide">Slide 8</div>
        <div class="swiper-slide">Slide 9</div>
      </div>
      <div class="swiper-pagination"></div>
    </div>

    <!-- Swiper JS -->
    <!-- Initialize Swiper -->
    <script>
      var swiper = new Swiper(".mySwiper", {
        slidesPerView: "auto",
        centeredSlides: true,
        spaceBetween: 30,
        pagination: {
          el: ".swiper-pagination",
          clickable: true,
        },
      });
    </script>
  </body>
</html>

2、vue2版本
在这里插入图片描述

7以上版本不支持vue2

npm install swiper@5.4.5
<template>
  <div class="box-wrap">
    <div class="swiper mySwiper">
      <div class="swiper-wrapper">
        <div class="swiper-slide" v-for="(item,index) in swipeList"  :key="index" :style="{background:item.bg}">
          {{item.title}}
        </div>
      </div>
      <div class="swiper-pagination"></div>
    </div>
  </div>
</template>
<script>
import Swiper from 'swiper'
import 'swiper/css/swiper.min.css';

export default {
  data(){
    return{
       swipeList:[
          {
            id:1,
            title:11111,
            bg:'#cfc'
          },
          {
            id:2,
            title:2222,
            bg:'#f00',
          },
          {
            id:3,
            title:3333,
            bg:'#cfc'
          },
          {
            id:4,
            title:4444,
            bg:'#ffc'
          },
        ],
    }
  },
  mounted(){
    setTimeout(()=>{
      this.initSwiper()
    })
  },
  methods:{
    initSwiper(){
      var swiper = new Swiper(".mySwiper", {
        slidesPerView: "auto",
        centeredSlides: true,
        spaceBetween: 30,
        pagination: {
          el: ".swiper-pagination",
          clickable: true,
        },
      });
    },
  }
}
</script>
<style>
.box-wrap{
  position: relative;
  height: 100vh;
  background: #eee;
}
.swiper {
        width: 100%;
        height: 300px;
        
      }
.swiper-slide {
        width: 60%;
        height: 300px;
      }

</style>

3、vue3版本

在这里插入图片描述
版本8+

npm install swiper

文档地址https://swiperjs.com/vue

<template>
  <div class="box-wrap">
     <swiper
     :modules="modules"
     slidesPerView="auto"
     centeredSlides
        :space-between="30"
        :pagination="{ el:'.swiper-pagination',clickable: true }"
        @swiper="onSwiper"
        @slideChange="onSlideChange"
        class="swiper"
      >
        <swiper-slide class="swiper-slide" v-for="(item,index) in swipeList"  :key="index" :style="{background:item.bg}">
          {{item.title}}
        </swiper-slide>
        ...
      </swiper>
      <div class="swiper-pagination aaa111"></div>
  </div>
</template>
<script>
  // Import Swiper Vue.js components
  import { Swiper, SwiperSlide } from 'swiper/vue';
  import { Navigation, Pagination, Scrollbar, A11y } from 'swiper';
  import 'swiper/css';
  import 'swiper/css/pagination';
   
   export default {
    components: {
      Swiper,
      SwiperSlide,
    },
    setup() {
      const onSwiper = (swiper) => {
        console.log(swiper);
      };
      const onSlideChange = () => {
        console.log('slide change');
      };

      return {
        onSwiper,
        onSlideChange,
        modules: [Pagination],
        swipeList:[
          {
            id:1,
            title:11111,
            bg:'#cfc'
          },
          {
            id:2,
            title:2222,
            bg:'#f00',
          },
          {
            id:3,
            title:3333,
            bg:'#cfc'
          },
          {
            id:4,
            title:4444,
            bg:'#ffc'
          },
        ],
      };
    },
  };
</script>
<style scoped>
.box-wrap{
  position: relative;
  background: #eee;
  width: 80vw;
  height: 80vh;
}
.swiper{
  height: 300px;
}
.swiper-slide{
  width: 60vw;
}
</style>

重点

//css
.swiper-slide {
  width: 60%;
 }
 //js
<script>
    var swiper = new Swiper(".mySwiper", {
       slidesPerView: "auto",
       centeredSlides: true,
       spaceBetween: 30,
       pagination: {
         el: ".swiper-pagination",
         clickable: true,
       },
     });
   </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值