门户网站首页向下滚动一屏

Vue3实现鼠标滚动切换一屏,直接上代码,section中可直接使用<component> <component/>动态渲染组件

<script setup>
import { ref, onMounted, watch } from 'vue'
import _ from 'loadsh'
const sections = ref([
  {
    title: 'Banner',
    id: 'section0',
    component: ''
  },
  {
    title: '第一屏',
    id: 'section1',
    component: ''
  },
  {
    title: '第二屏',
    id: 'section2',
    component: ''
  },
  {
    title: '第三屏',
    id: 'section3',
    component: ''
  },
  {
    title: '第四屏',
    id: 'section4',
    component: ''
  },
  {
    title: '第五屏',
    id: 'section5',
    component: ''
  },
  {
    title: '第六屏',
    id: 'section6',
    component: ''
  }
])
const section = ref()
const wheelIndex = ref(0)
const handleWheel = (event) => {
  event.preventDefault();
  handleScroll(event)

}
const handleScroll = _.debounce((event) => {
  if (event.deltaY > 0) {
    if (wheelIndex.value >= 6) return
    wheelIndex.value += 1
  }
  if (event.deltaY < 0) {
    if (wheelIndex.value <= 0) return
    wheelIndex.value -= 1
  }
}, 500)
watch(wheelIndex, (n, o) => {
  if (!section.value[wheelIndex.value]) return

  window.scrollTo({
    top: section.value[wheelIndex.value].offsetTop,
    behavior: 'smooth'
  })
})
onMounted(() => {
  // window.addEventListener('scroll',handleScroll)
  window.addEventListener('wheel', handleWheel, { passive: false })
})

</script>
<template>
  <div class="container" ref="container">
    <section class="section" ref="section" v-for="(item, index) in sections" :key="index">{{ item.title }}</section>
  </div>
</template>

<style scoped>
body {

  overflow: hidden;
}

::-webkit-scrollbar {
  width: 0 !important;
  display: none;
  overflow: hidden;
}

.section {
  height: 100vh;

}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值