6fa
V2EX  ›  问与答

请教大家一个 js 问题:给 scrollTop 赋值在移动端滚动条没有响应

By 6fa at 2020 年 6 月 4 日 · 1018 次点击

问题描述

在 IntersectionObserver 里面使用了 xx.scrollTop = xx,经过测试在 pc 端完全正常,滚动条有滚动。但是在移动端(安卓),虽然有赋值成功,但是没有滚动。

环境

没有用到框架,为原生 js

相关代码

    this.selectedItem = new IntersectionObserver((entries) => {
      entries.forEach((item) => {
        if (item.isIntersecting) {
        // 就是这行
          this.yearScrollEl.scrollTop = 100
        }
      })
    }, {
      root: this.element.querySelector('.timePicker-wheel'),
      threshold: [0.6]
    })
    
    this.element.querySelectorAll('li').forEach((item) => {
      this.selectedItem.observe(item)
    })

自己尝试的方法(原理不懂)

加了一个定时器之后,在移动端就正常了:

setTimeout(() => {
    this.yearScrollEl.scrollTop = 100
},300)

但是不知道原理,希望顺便能请教各位这一点

目前尚无回复
© 2026 V2EX · 27ms · 3.9.8.5