vue3 + swiper 实现卡片效果切换

2 篇文章 0 订阅
1 篇文章 0 订阅

swiper官网:https://swiperjs.com

npm i swiper
<template>
  <swiper
    :style="{ height: swiperHeight }"
    direction="vertical"
    :grabCursor="true"
    :effect="'creative'"
    :creativeEffect="{
      prev: {
        shadow: true,
        translate: [0, '-120%', -500],
      },
      next: {
        shadow: true,
        translate: [0, '120%', -500],
      },
    }"
    :modules="modules"
    :observer="true"
    :observeParents="true"
    :loop="false"
    :autoplay="false"
    :autoplayDisableOnInteraction="false"
    @swiper="onSwiper"
    @slideChange="onSlideChange"
    @touch-end="touchEnd"
  >
    <swiper-slide :class="isExp ? '' : 'swiper-no-swiping'">
      <div class="content">Slide 1</div>
    </swiper-slide>
    <swiper-slide>
      <div class="content">Slide 2</div>
    </swiper-slide>
    <swiper-slide>
      <div class="content">Slide 3</div>
    </swiper-slide>
    <swiper-slide>
      <div class="content">Slide 4</div>
    </swiper-slide>
    <swiper-slide>
      <div class="content">Slide 5</div>
    </swiper-slide>
  </swiper>
</template>

<script setup lang="ts">
import { EffectCreative } from "swiper";
import { Swiper, SwiperSlide } from "swiper/vue/swiper-vue.js";
import "swiper/swiper-bundle.min.css"; // 所有样式
import { ref } from "@vue/reactivity";

// Import Swiper styles
// import "swiper/swiper.css";

// import required modules
const modules = [EffectCreative];
const swiperRef = ref();
const swiperIndex = ref(0);
const swiperHeight = ref("300px"); // 自定义轮播图高度

const onSwiper = (swiper: any) => {
  swiperRef.value = swiper;
};
const onSlideChange = (swiper: any) => {
  console.log("slide change swiper.realIndex;", swiper.realIndex);
  console.log("slide change swiper.previousIndex;", swiper.previousIndex);
  swiperIndex.value = swiper.realIndex;
};
const touchEnd = (swiper: any) => {
  setTimeout(() => {
    if (swiper.realIndex === 4) {
      console.log("The End :>> ");
    }
  }, 200);
};
// 点击滑动到下一个
const getNextFn = () => {
  swiperRef.value.slideTo(swiperIndex.value + 1, 1000, false);
};
</script>
<style scoped>
.swiper {
  margin: 100px auto;
  width: 320px;
  height: 240px;
}

.swiper-slide {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  font-weight: bold;
  color: #fff;
}

.swiper-slide:nth-child(1n) {
  background-color: rgb(206, 17, 17);
}

.swiper-slide:nth-child(2n) {
  background-color: rgb(0, 140, 255);
}

.swiper-slide:nth-child(3n) {
  background-color: rgb(10, 184, 111);
}
</style>

遇到的问题:

  1. Q:某些区域无法正常滑动?
    A:原因是,滑块包裹的内容超过swiper容器高度导致,给容器设置高度,如例子 :style=“{ height: swiperHeight }”
  2. Q:整体区域头尾都能正常切换滑动,但是中间有部分区域无法滑动?
    A: 原因可能是因为该区域内容抽离为组件,跟swiper不在同一个组件中导致的,可试试改为同一组件。
    3: 禁止页面滑动?
    A: 给页面添加类名 ‘swiper-no-swiping’,即可
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值