V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
6fa
V2EX  ›  问与答

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

  •  
  •   6fa · Jun 4, 2020 · 706 views
    This topic created in 2163 days ago, the information mentioned may be changed or developed.

    问题描述

    在 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)
    

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

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3335 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 13:02 · PVG 21:02 · LAX 06:02 · JFK 09:02
    ♥ Do have faith in what you're doing.