繁体   English   中英

在反应中滚动到可滚动div的底部

[英]Scroll to the bottom of a scrollable div in react

我尝试了许多解决方案来尝试滚动到 div 的底部,因为在底部可以找到关于他们的注册表单是否成功提交的用户反馈。

var objDiv = document.getElementById("form");
objDiv.scrollTop = objDiv.scrollHeight;

window.scrollTo(0,document.body.scrollHeight);

window.scrollTo(0,document.querySelector("#form").scrollHeight);

没有成功

这是表单的反应代码

return (
    <div
      className={styles.register_container}
      id="register-container"
      style={{
        height: "100%",
        overflow: "scroll",
      }}
    >
      <HomeIcon />
      <div className={styles.login_button_container}>
        <p>Already have an account? </p>
        <button
          onClick={() => {
            router.push("/login");
          }}
        >
          Log in
        </button>
      </div>
      <div
        style={{
          padding: "60px 0",
          height: "100%",
          maxWidth: "300px",
          display: "flex",
          flexDirection: "column",
          alignItems: "center",
        }}
        id="form"
      >
        <img
          src="/assets/Logo/ROA_logowhite.png"
          style={{
            height: "100px",
            maxWidth: "100px",
          }}
          onClick={() => {
            var objDiv = document.getElementById("form");
            objDiv.scrollTop = objDiv.scrollHeight;
          }}
        />
        {page === 2 && (
          <div style={{ width: "100%" }}>
            <button className="back-button" onClick={backButtonHandler}>
              <FontAwesomeIcon icon={faAngleLeft} />
              Back
            </button>
          </div>
        )}
        {page === 1 && loginDetails}
        {page === 2 && personalDetails}

        <div style={{ width: "100%", padding: "5px 0" }}>
          <button
            className="form-button"
            onClick={buttonHandler}
            style={{ minHeight: "50px" }}
          >
            {submitButton}
          </button>
        </div>
        {loading && (
          <div
            style={{
              padding: "5px",
              width: "100%",
              display: "flex",
              justifyContent: "center",
              alignItems: "center",
            }}
          >
            <ReactLoading
              type="spin"
              color="#1dd760"
              height={50}
              width={50}
              id="loader"
            />
          </div>
        )}

        {registrationError && (
          <p className="form-submit-error">
            Registration error, refresh your page and try again
          </p>
        )}
        {true && (
          <div>
            <p
              style={{ marginBottom: "10px" }}
              className="form-submit"
              id="submit"
            >
              Redirecting to login ... You should receive a welcome email, if
              not please try registering again or visit the{" "}
              <a href="/contact" style={{ color: "#1dd760" }}>
                contact
              </a>{" "}
              page.
            </p>
          </div>
        )}
      </div>
    </div>
  );

我不能直接关注提交成功消息,因为 setState 在反应中是异步的。 只需要一个 function 允许我在单击下一步/提交按钮时滚动 div 的底部

在此处输入图像描述

在此处输入图像描述

在 React function 组件中访问/修改 DOM 元素的正确方法是使用useRef挂钩。 https://reactjs.org/docs/hooks-reference.html#useref

这是一个如何解决问题的示例:

import * as React from "react";

const Component: React.FunctionComponent = () => {
  const ref = React.useRef<HTMLDivElement>(null);
  const [success, updateSuccess] = React.useState(false);

  const buttonHandler = () => {
    updateSuccess(true);
    if (ref.current) {
      const offsetBottom = ref.current.offsetTop + ref.current.offsetHeight;
      window.scrollTo({ top: offsetBottom });
    }
  };

  return (
    <div>
    ...
      <button onClick={buttonHandler}>Scroll</button>
      {success ? (
        <div
          ref={ref}
          style={{ height: "2000px", display: !success && "none" }}
        >
          Long Success Message
        </div>
      ) : null}
    </div>
  );
};

export default Component;

暂无
暂无

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

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