vue中点击滚动到指定位置,监听滚动返回选中状态

点击一个元素滚动到页面某个位置,可以简单通过锚点和scrollTop方法来实现。
但在此处我们通过scrollTo方法来实现,再设置滚动行为smooth平滑滑动,让过程更加顺畅,话不多话,马上上代码!

<template>
  <div class="wrap">
    <div class="pou">
      <div class="blo" 
      :class="{'active': active1 === index}" 
      v-for="(item, index) in list" :key="index" 
      @click="clickHandle(item.option, index)"
      ><span>{{item.values}}</span></div>
    </div>
    <div>
      <div ref="select" style="height: 500px">
        <span style="color: #f0f;">精选内容:</span>
        Lorem ipsum dolor sit amet consectt!
      </div>
      <div ref="comduce" style="height: 800px">
        <span style="color: #f0f;">推荐内容:</span>
        Lorem ipsum dolor sit amet consectetur o?
      </div>
      <div ref="normal" style="height: 900px">
        <span style="color: #f0f;">普通内容:</span>
        Lorem ipsum dolor sit, amet consectetur adipisicing elrum at.
      </div>
    </div>
  </div>
</template>
 data () {
     return {
       list: [],
       active1: 0
     }
   },
 mounted () {
    this.list = [
      {
        values: '精选',
        active: true,
        option: this.$refs.select.offsetTop
      },
      {
        values: '推荐',
        active: false,
        option: this.$refs.comduce.offsetTop
      },
      {
        values: '普通',
        active: false,
        option: this.$refs.normal.offsetTop
      }
    ]
  }

渲染完成后,加入点击事件!

clickHandle (top, index) {
      this.active1 = index
      window.scrollTo({
        top: top,
        behavior: 'smooth'
      })

现在已经实现点击滚动了, 然而我们加入元素选中状态后,还可以实现滚动到哪个位置选中状态到哪个元素!
首先在mounted生命周期里监听页面滚动事件

window.addEventListener('scroll', this.handleScroll, false)

然后在定时器里翻转list数组后遍历数组,当卷上去的高度小于等于scrollTop高度时,计算出最后的元素选中状态,最后再赋值给active1

handleScroll () {
     const aa = document.documentElement.scrollTop
     setTimeout(() => {
       const arr = [...this.list].reverse()
       for (var i = 0; i < arr.length; i++) {
         if (arr[i].option <= aa) {
           this.active1 = arr.length - 1 - i
           break
         }
       }
     }, 200)
     this.active1 = this.active
   }

最后一步千万别忘了移除监听滚动事件哟!

destroyed () {
    window.removeEventListener('scroll', this.handleScroll, false)
  }

再最后贴上css和效果图

<style lang="scss">
.wrap {
  background: #ffcdff;
  padding: 0 20px;
  .pou {
    position: fixed;
    bottom: 60px;
    right: 10px;
    text-align:center;
    z-index:2;
    background: #dad8d8;
    .blo {
      padding: 9px;
      &:hover, &.active {
        background: #f0f;
        padding: 9px;
        color: #fff;
      }
    }
  }
}
</style>

图示

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 实现滚动监听到锚点,可以通过以下步骤: 1. 在需要监听滚动的容器上,绑定 `@scroll` 事件。 2. 通过 `getBoundingClientRect()` 方法获取每个锚点元素的位置信息,得到它们距离容器顶部的距离。 3. 在滚动事件处理函数,获取当前容器滚动的距离,并与每个锚点元素的位置信息进行比较,判断当前容器滚动到了哪个锚点的位置。 4. 将滚动到的锚点信息保存在 Vue 实例的数据,并在模板根据该数据展示对应的内容。 以下是一个简单的示例代码: ```vue <template> <div class="scroll-container" @scroll="handleScroll"> <div class="section" v-for="(section, index) in sections" :key="index"> <h2>{{ section.title }}</h2> <p>{{ section.content }}</p> </div> </div> </template> <script> export default { data() { return { activeSection: 0, // 默认选中第一个锚点 sections: [ { title: "Section 1", content: "Content 1" }, { title: "Section 2", content: "Content 2" }, { title: "Section 3", content: "Content 3" }, { title: "Section 4", content: "Content 4" }, ], }; }, mounted() { this.calculatePositions(); }, methods: { calculatePositions() { // 获取每个锚点元素的位置信息 this.positions = this.sections.map((section) => { const element = document.getElementById(section.title); return { title: section.title, top: element.getBoundingClientRect().top + this.$refs.container.scrollTop, }; }); }, handleScroll() { const scrollTop = this.$refs.container.scrollTop; // 判断当前滚动到了哪个锚点 for (let i = 0; i < this.positions.length; i++) { const position = this.positions[i]; if (scrollTop >= position.top && scrollTop < position.top + 400) { this.activeSection = i; break; } } }, }, }; </script> ``` 在该示例,使用 `getBoundingClientRect()` 方法获取每个锚点元素的位置信息,并保存在 `positions` 数组。在滚动事件处理函数,获取当前容器滚动的距离,并与每个锚点元素的位置信息进行比较,判断当前容器滚动到了哪个锚点的位置。最后将滚动到的锚点信息保存在 `activeSection` ,用于在模板展示对应的内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值