首页 > 解决方案 > 反应滚动到顶部不起作用?需要新的解决方案

问题描述

因此,当用户提交表单时,我试图滚动到屏幕顶部。堆叠溢出的答案似乎对我不起作用。

这是我的代码:

handleClick (evt) {
    evt.preventDefault();
    this.calculateScore();
    this.setState({submittingQuiz: true});

    // let myPage = document.querySelector('body');
    // myPage.scrollTo(0,0); Couldn't get this way to work either

    window.scrollTo({top: 0, behavior: 'smooth'})
}

有关如何解决此问题的任何想法。我有一些样式,我认为可能会弄乱它..

html {
  background-image: url('./tupac.jpeg');
  background-position-y: -580px;
  background-repeat: no-repeat;
  background-size: cover;
  overflow: hidden;
}

Anyone got a clue?

标签: javascripthtmlreactjs

解决方案


这可能是因为应用程序的body一个height: 100vh;或一些主容器具有相同的height: 100vh;

尝试删除应用程序height: 100vh;body主容器并再次测试。


推荐阅读