vue 滚屏

 一、效果图

竖屏整屏鼠标滚动,鼠标下滑或者上滑一次就会滚动一个页面【没有使用插件,网上也有比较充足的有关vue滚屏的插件不想手写的也可以去用插件实现,我看着比较麻烦就弃用了】

二、代码

HTML:

<template>
  <div class="Demo">
    <div></div>
    <div></div>
    <div></div>
  </div>
</template>

JS:

<script>
  export default {
    name: 'Demo',
    data () {
      return {
        index: 0, // 索引
        delay: 500, // 滚动事件间隔时间
        lock: false // 锁
      }
    },
    mounted () {
      this.$el.addEventListener('mousewheel', this.mousewheelHandler)
    },
    watch: {
      index (newVal, oldVal) {
        if (newVal !== oldVal) { // 防止往相同的位置滚动后就要无辜等待delay秒才能再次滚动
          this.lock = true
        }
      }
    },
    methods: {
      mousewheelHandler (event) {
        let value = event.wheelDelta > 0 ? 'up' : 'down'
        this.scrollHandler(value)
      },
      scrollHandler (value) {
        if (this.lock) {
          return
        }
        let mapIndex = {
          up: Math.max(0, this.index - 1),
          down: Math.min(this.$el.children.length - 1, this.index + 1)
        }
        this.index = mapIndex[value]
        this.$el.children[this.index].scrollIntoView({ behavior: 'smooth' })
        setTimeout(() => this.lock = false, this.delay)
      }
    }
  }
</script>

 CSS:

<style scoped>
  .Demo {
    overflow: hidden;
  }
  .Demo,
  .Demo > div {
    height: 100%;
  }
  .Demo > div:nth-child(1) {
    background-color: #f00;
  }
  .Demo > div:nth-child(2) {
    background-color: #0f0;
  }
  .Demo > div:nth-child(3) {
    background-color: #00f;
  }
</style>

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值