简体   繁体   English

元素出现后如何滚动到底部(使用react-slidedown)

[英]How to scroll to bottom after an element appears (using react-slidedown)

A bit of a newbie question I'm sure, but I can't figure it out.我敢肯定,这是一个新手问题,但我无法弄清楚。

I have an optin form that I reveal upon clicking on a link.我有一个 optin 表单,我会在单击链接时显示该表单。 The reveal is done using react-slidedown .显示是使用react-slidedown完成的。

The problem I have is that if this is on the bottom of the page, the form that slides down is not seen - you have to scroll down to see it.我遇到的问题是,如果它位于页面底部,则看不到向下滑动的表单-您必须向下滚动才能看到它。 How can I scroll automatically to the bottom of the page after the form slides down?表单向下滑动后如何自动滚动到页面底部?

Here is the codesandbox (scroll to the bottom for the link) 这是代码框(滚动到底部的链接)

I've been dealing with this for the past two hours, but haven't managed to make it work.在过去的两个小时里,我一直在处理这个问题,但还没有成功。

(Note: I'm pretty new at react.) (注意:我对反应很陌生。)

Add the following to your function "handleToggle":将以下内容添加到您的 function“handleToggle”中:

  setTimeout(() => {
    window.scrollTo(0, document.body.scrollHeight);
  }, 100);

since the update of the state and rendering takes some time, we delay our scrollTo function by 100ms由于 state 的更新和渲染需要一些时间,我们将滚动到scrollTo延迟 100 毫秒

It should give you the effect that you're looking for... forked codesandbox here它应该给你你正在寻找的效果...... 在这里分叉的代码和框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM