亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解決使用vue-awesome-swiper組件手動(dòng)滾動(dòng)點(diǎn)擊失效問題

 更新時(shí)間:2023年06月30日 09:48:17   作者:Skywang  
這篇文章主要介紹了使用vue-awesome-swiper組件手動(dòng)滾動(dòng)點(diǎn)擊失效問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

問題

最近做了一個(gè)項(xiàng)目用了vue-awesome-swiper這個(gè)組件,遇到了一個(gè)問題。

就是vue引入swiper后,使用loop循環(huán),然后給每個(gè)swiper-slide滑塊綁定事件@click='fn()' ,由于是循環(huán)模式,swiper會(huì)復(fù)制兩份swiper-slide滑塊做循環(huán)的效果,但是這兩份復(fù)制的滑塊點(diǎn)擊效果無效,其它的正常。

廢話不多說 上代碼

問題代碼

這是把點(diǎn)擊事件綁定在dom上

在 loop 開啟的時(shí)候,dom 綁定事件是有問題的。因?yàn)樵趌oop模式下slides前后會(huì)復(fù)制若干個(gè)slide,從而形成一個(gè)環(huán)路,但是卻不會(huì)復(fù)制綁定在dom上的click事件。

因此只能使用 swiper 提供的 api 方法進(jìn)行解決

解決方案

html

js

<script>
    import {
        swiper,
        swiperSlide
    } from "vue-awesome-swiper";
    var vm = null;
    export default {
        data() {
            return {
                swiperOption: {
                    notNextTick: true,
                    //循環(huán)
                    loop: true,
                    //設(shè)定初始化時(shí)slide的索引
                    initialSlide: 0,
                    //自動(dòng)播放
                    autoplay: true,
                    autoplay: {
                        delay: 3000,
                        stopOnLastSlide: false,
                        disableOnInteraction: false,
                    },
                    // 設(shè)置輪播
                    effect: '',
                    //滑動(dòng)速度
                    speed: 800,
                    //滑動(dòng)方向
                    direction: 'horizontal',
                    //小手掌抓取滑動(dòng)
                    // grabCursor : true,
                    //滑動(dòng)之后回調(diào)函數(shù)
                    on: {
                        slideChangeTransitionEnd: function() {
                            console.log(this.activeIndex);
                            //切換結(jié)束時(shí),告訴我現(xiàn)在是第幾個(gè)slide
                            //                             const realIndex = this.activeIndex;
                            //                             vm.carousel(realIndex);
                        },
                        click: function() {
                          // 這里有坑,需要注意的是:this 指向的是 swpier 實(shí)例,而不是當(dāng)前的 vue, 因此借助 vm,來調(diào)用 methods 里的方法 
                          // console.log(this); // -> Swiper
                          // 當(dāng)前活動(dòng)塊的索引,與activeIndex不同的是,在loop模式下不會(huì)將 復(fù)制的塊 的數(shù)量計(jì)算在內(nèi)。
                            console.log('ss'+this.realIndex)
                            const realIndex = this.realIndex;
                            vm.carousel(vm.carousels[realIndex]);
                        }
                    },
                    //左右點(diǎn)擊
                    //                  navigation: {
                    //                      nextEl: '.swiper-button-next',
                    //                      prevEl: '.swiper-button-prev',
                    //                  },
                    //分頁器設(shè)置         
                    pagination: {
                        el: '.swiper-pagination',
                        clickable: true
                    }
                },
                swiperSlides: [1, 2, 3, 4],
                activeIndex: 0,
                carousels:[]
            }
        },
        components: {
            swiper,
            swiperSlide
        },
        computed: {
            swiper() {
                return this.$refs.mySwiper.swiper;
            }
        },
        created() {
            vm = this;
        },
        methods: {
            carousel(type) {
                if (type.url !== null) {
                    this.$router.push(type.redirect_to);
                } 
            }
        }
    }

如上圖js里定義一個(gè)全局的變量vm,為了在swiper的AIP中調(diào)用vue的方法和綁定的數(shù)據(jù), 這時(shí)候就可以解決點(diǎn)擊事件失效的問題了!

以上就是解決使用vue-awesome-swiper組件手動(dòng)滾動(dòng)點(diǎn)擊失效問題的詳細(xì)內(nèi)容,更多關(guān)于vue-awesome-swiper滾動(dòng)點(diǎn)擊的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論