滑动列表数据按照A-Z首字母排列,右边A-Z能动态定位到相应字母位置

核心实现,循环右边字母列表,然后找出左边数据列表,对应id为相应字母的元素,计算元素到顶部和底部的距离,就是范围,然后滑动监听,通过滑动的距离在哪个范围确定右边要定位到哪个字母,实现最终效果:

this.$nextTick(() => {
        this.itemsParent = []
        for (let i = 0; i < this.listforgirht.length; i++) {
          const height = document.getElementById(this.listforgirht[i]).offsetHeight
          const top = document.getElementById(this.listforgirht[i]).offsetTop
          var items = { tag: '', topInstance: '', bottomInstance: '', position: '' }
          if (i === 1) {
            this.$set(items, 'tag', this.listforgirht[i])
            this.$set(items, 'topInstance', 0)
            this.$set(items, 'bottomInstance', height)
            this.$set(items, 'position', i)
          }
          this.$set(items, 'tag', this.listforgirht[i])
          this.$set(items, 'topInstance', top)
          this.$set(items, 'bottomInstance', top + height)
          this.$set(items, 'position', i)
          this.itemsParent.push(items)
        }
        console.log(this.itemsParent)
      })
    },
    scrollListen (event) {
      const top = event.target.scrollTop
      for (const item6 of this.itemsParent) {
        if (top >= item6.topInstance && top <= item6.bottomInstance) {
          const pos = item6.position
          this.indexm = pos
          console.log(pos)

          return
        }
      }
      console.log(event.target.scrollTop)

      console.log('123456')
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值