V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
6fa
V2EX  ›  问与答

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

  •  
  •   6fa · 2020 年 6 月 4 日 · 646 次点击
    这是一个创建于 2137 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问题描述

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

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

    目前尚无回复
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   2674 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 15:20 · PVG 23:20 · LAX 08:20 · JFK 11:20
    ♥ Do have faith in what you're doing.