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
| <template> <div class="single-page"> <swiper class="single-swiper-page" direction="vertical" parallax :modules="modules" navigation :allowSlidePrev="swiperConf.allowSlidePrev" :allowSlideNext="swiperConf.allowSlideNext" :pagination="{ clickable: true }" @swiper="onSwiper" @slideChange="onSlideChange" > <swiper-slide> Slide 1 <div class="btn" @click="handleStartOpen">立即开启</div> </swiper-slide> <swiper-slide> <div class="title" data-swiper-parallax="-100">从右边100px开始进入</div> Slide 2 <div data-swiper-parallax="0" data-swiper-parallax-opacity="0.1" >透明度变化</div> <div data-swiper-parallax-scale="0.55" >缩放变化</div> </swiper-slide> <swiper-slide>Slide 3</swiper-slide> </swiper> </div> </template> <script setup> import { onMounted, ref, nextTick } from 'vue'
import {Pagination } from "swiper"
import { Swiper, SwiperSlide } from "swiper/vue/swiper-vue"
import "swiper/swiper.scss" const modules = ref([Pagination]) const swiperRef = ref(null) const swiperConf = ref({ allowSlideNext: true, allowSlidePrev: true, length: 0 }) const onSwiper = (swiper) => { swiperRef.value = swiper } const onSlideChange = (change) => { isAllowSlidePageTurning(change) console.log('%c [ slide change ]-51', 'font-size:14px; background:#cf222e; color:#fff;', change.activeIndex) }
const isAllowSlidePageTurning = ({ activeIndex, slides }) => { if (activeIndex === 0) { swiperConf.value.allowSlideNext = false swiperConf.value.allowSlidePrev = false } else if (activeIndex && activeIndex === slides.length - 1) { swiperConf.value.allowSlideNext = false swiperConf.value.allowSlidePrev = true } else { swiperConf.value.allowSlideNext = true swiperConf.value.allowSlidePrev = true } }
const handleStartOpen = () => {
console.log('%c [ handleStartOpen ]-77', 'font-size:14px; background:#cf222e; color:#fff;',swiperRef.value) if (swiperRef.value) { swiperConf.value.allowSlideNext = true nextTick(() => swiperRef.value.slideNext()) } }
onMounted(() => { if (swiperRef.value) { const { activeIndex, slides } = swiperRef.value isAllowSlidePageTurning(swiperRef.value) swiperConf.value.length = slides.length console.log('%c [ swiperRef.value ]-56', 'font-size:14px; background:#cf222e; color:#fff;', activeIndex, slides.length) } }) </script> <style lang="scss" scoped> .single-page { width: 100vw; height: 100vh; .single-swiper-page:deep() { height: 100%; width: 100%; } }
.swiper-slide { height: 300px; width: 100vw; line-height: 300px; font-size: 30px; text-align: center; background-color: pink; } </style>
|