项目上需要一个轮播图的,之前用js和jq写过,最近,我想用element的走马灯效果改成轮播图的效果,我就百一下,发现还真有这个功能,然后我就借鉴一下,然后就有此文章了,文章最后有转载地址。

<template>
    <div @wheel="goWheel">
        <div class="block">
            <el-carousel 
                height="100vh" 
                direction="horizontal" 
                :autoplay="true"              
                trigger="click" 
                ref="swiper" 
                :loop="true"
            >
                <el-carousel-item v-for="item in 4" :key="item" >
                    <h3 class="small">{{ item }}</h3>
                </el-carousel-item>
            </el-carousel>
        </div>
    </div>
</template>
<script> 
    export default {
        data(){
            return {
                one:true,//以此来控制每次轮播图切换的张数
            }
        },
        created(){
  
        },
        mounted(){
        },
        methods: {
            goWheel() {
                if (event.deltaY > 0 && this.one==true) { 
                    this.$refs.swiper.next();   
                    this.one=false;
                    setTimeout(()=>{
                        this.one=true
                    },1000)
                }
                
                if (event.deltaY < 0 && this.one==true) {
                    this.$refs.swiper.prev();
                    this.one=false;
                    setTimeout(()=>{
                        this.one=true
                    },1000)
                }
            },
        },
    }
</script>
<style>
    .el-carousel__item h3 {
        color: #475669;
        font-size: 14px;
        opacity: 0.75;
        line-height: 150px;
        margin: 0;
    }

    .el-carousel__item:nth-child(2n) {
        background-color: #99a9bf;
    }
    
    .el-carousel__item:nth-child(2n+1) {
        background-color: #d3dce6;
    }
</style>
<style type="text/css">
</style>
  

@wheel 是vue鼠标滚动事件。

转载:https://blog.csdn.net/Mayisheart/article/details/122384551?spm=1001.2014.3001.5502

Logo

前往低代码交流专区

更多推荐