没有反弹 css 时,Javascript scrollTo() 不工作?

标签 javascript html css

我有一个用 Vue 构建的单页应用程序。我正在尝试创建一个类似 native 的 View ,在浏览页面时不会弹跳并滚动到顶部。问题是,当没有提供反弹 css(溢出、宽度/高度等)时,它不会滚动。

document.getElementById('test').scrollTo({
    top: 1000,
    left: 0
})
html {
    visibility: inherit;
    width: 100%;
    height: 100%;
}

body {
    -webkit-font-smoothing: antialiased;
    background-color: #edf2f9;
    color: #000000;
    position: fixed;
    width: 100%;
    max-width: 100%;
    height: 100%;
    max-height: 100%;
    text-rendering: optimizeLegibility;
    overflow: hidden;
    touch-action: manipulation;
    -webkit-user-drag: none;
    -ms-content-zooming: none;
    word-wrap: break-word;
    overscroll-behavior-y: none;
    margin: 0;
    padding: 0;
}

main {
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    display: flex;
    position: absolute;
    flex-direction: column;
    justify-content: space-between;
    contain: layout size style;
    overflow: hidden;
    z-index: 0;
}

section {
    padding-top: 96px;
    padding-bottom: 96px;
    overflow: scroll;
}
<main>
    <section>
        <div style="height: 5000px;" id="test">
            <p>test</p>
        </div>
    </section>
</main>

有人可以帮帮我吗?谢谢!

最佳答案

为section 元素使用ID“test”。 该部分是具有比其自身容器大的内容的元素,因此可以滚动。因此 scrollTo 可以应用于此元素。 <div> below 没有比它自己的容器大的内容,所以它不能滚动。

关于没有反弹 css 时,Javascript scrollTo() 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59085606/

相关文章:

html - Angular-cli 从外部 url 添加样式

css - 将一个元素垂直放置在两个其他元素之间(和之上)

javascript - 单击 div 而不是点击提交然后传递参数

javascript - 将 ISO 8601 日期的排序数组拆分为 Javascript 中每一天的新二维数组

javascript - setMinutes 未得到预期结果

html - 如何为 HTML 5 <picture> 设置空图像?

javascript - Grunt-injector 忽略来自 bower 依赖的 css 文件

html - 我怎样才能让这个页脚最终做我想做的事?会喜欢一些建议

javascript - 如何使用 jQuery 更改特定文本?

html - textarea 在其他元素之后填充未使用的垂直空间 - 在叠加层中