给scrollTop赋值在移动端滚动条没有响应。

问题描述

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)

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

阅读 1.8k
1 个回答
console.log(this.yearScrollEl)这个值
一般用延迟正常就说明没拿到this.yearScrollEl
推荐问题