vue+element实现鼠标滚轮控制轮播图

实现鼠标滚轮控制轮播图:

利用element走马灯,给其添加鼠标滚轮事件来实现鼠标滚轮控制轮播图

使用element走马灯:

<template>
  <div class="box">
  //添加@mousewheel鼠标滚轮事件
    <div style="height: 100%" @mousewheel="rollScroll($event)">
      <el-carousel
        direction="vertical"
        :autoplay="false"
        trigger="click"
        ref="carousel"
        @mousewheel="rollScroll($event)"
      >
        <el-carousel-item class="item" v-for="(item, index) in 4" :key="index">
          <div class="font">
            <h3>{{ item }}</h3>
          </div>
        </el-carousel-item>
      </el-carousel>
    </div>
  </div>
</template>

鼠标滚轮事件(节流处理)

rollScroll(event) {
      let _that = this;
      // chrome、ie使用的wheelDelta,火狐使用detail
      let scrollVal = event.wheelDelta || event.detail;
      // 节流
      if (!_that.timeOut) {
        _that.timeOut = setTimeout(() => {
          _that.timeOut = null;
          scrollVal > 0
            ? _that.$refs.carousel.prev()
            : _that.$refs.carousel.next();
        }, 300);
      } else {
      }
    },

完整代码

<template>
 <div class="box">
   <div style="height: 100%" @mousewheel="rollScroll($event)">
     <el-carousel
       direction="vertical"
       :autoplay="false"
       trigger="click"
       ref="carousel"
       @mousewheel="rollScroll($event)"
     >
       <el-carousel-item class="item" v-for="(item, index) in 4" :key="index">
         <div class="font">
           <h3>{{ item }}</h3>
         </div>
       </el-carousel-item>
     </el-carousel>
   </div>
 </div>
</template>

<script>
export default {
 name: "home",

 data() {
   return {
     timeOut: null,
   };
 },
 methods: {
   rollScroll(event) {
     let _that = this;
     // chrome、ie使用的wheelDelta,火狐使用detail
     let scrollVal = event.wheelDelta || event.detail;
     // 节流
     if (!_that.timeOut) {
       _that.timeOut = setTimeout(() => {
         _that.timeOut = null;
         scrollVal > 0
           ? _that.$refs.carousel.prev()
           : _that.$refs.carousel.next();
       }, 300);
     } else {
     }
   },
 },
};
</script>

<style lang="scss">
.box {
 background-color: #ccc;
}
.el-carousel-item {
 width: 800px;
 height: 600px;
 background-color: skyblue;
}
.font {
 height: 100%;
 width: 100%;
 display: flex;
 justify-content: center;
 align-items: center;
 h3 {
   font-size: 24px;
 }
}
</style>

结果:

在这里插入图片描述

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值