简体   繁体   English

如何从 NextJS 中的另一个页面平滑滚动到锚标记?

[英]How to smooth-scroll to an anchor tag from another page in NextJS?

I have 2 pages in my NextJS (1 is Home and another is Leaderboard page).我的 NextJS 中有 2 个页面(1 个是主页,另一个是排行榜页面)。 And I have a navbar as such:我有一个这样的navbar

<div className={styles.navItem}>
        <Link href="/">
          <a className={styles.navLinks} onClick={toggle}>
            Home
          </a>
        </Link>
      </div>
      <div className={styles.navItem} onClick={toggle}>
        <Link href="/">
          <a
            className={styles.navLinks}
            onClick={setTimeout((e) => {
              document.getElementById("about") &&
                document
                  .getElementById("about")
                  .scrollIntoView({ behavior: "smooth", block: "end" });
            }, 2000)}
          >
            About Us
          </a>
        </Link>
      </div>
      <div className={styles.navItem} onClick={toggle}>
        <Link href="/">
          <a
            className={styles.navLinks}
            onClick={setTimeout((e) => {
              document.getElementById("program") &&
                document
                  .getElementById("program")
                  .scrollIntoView({ behavior: "smooth", block: "end" });
            }, 2000)}
          >
            Program Overview
          </a>
        </Link>
      </div>
      <div className={styles.navItem}>
        <Link href="/leaderboard/">
          <a className={styles.navLinks} onClick={toggle}>
            Leaderboard
          </a>
        </Link>
      </div>

I thought that maybe by using a setTimeout delay it would, redirect me first to the homepage (when I click the Link tag) and then, after 2 seconds, it would smooth scroll me to the section I wanted to go to.我想也许通过使用setTimeout延迟,它会首先将我重定向到主页(当我单击Link标签时),然后在 2 秒后,它会将我平滑滚动到我想去的部分。 But this produces an error document is not defined and when I reload the page, the page would smooth-scroll to #program after 2 seconds even without clicking on the tag.但这会产生一个错误document is not defined ,当我重新加载页面时,即使不点击标签,页面也会在 2 秒后平滑滚动到#program What am I doing wrong?我究竟做错了什么?

ERROR (ON TERMINAL)错误(在终端上)

error - components/main/Nav/MenuRight.js (18:23) @ Timeout.eval [as _onTimeout]
ReferenceError: document is not defined
  16 |         <Link href="/">
  17 |           <a
> 18 |             className={styles.navLinks}
     |                       ^
  19 |             onClick={() =>
  20 |               setTimeout((e) => {
  21 |                 document.getElementById("about") &&

Your problem is you call setTimeout directly which is executed right away on the server (more specifically, NextJS does not have window and document objects on the server-side) that's why document is not available.您的问题是您直接调用setTimeout ,它会立即在服务器上执行(更具体地说,NextJS 在服务器端没有windowdocument对象),这就是document不可用的原因。

You should wrap it into a function like below and onClick will be only triggered once you click on that element您应该将其包装到如下所示的函数中,并且只有在您单击该元素后才会触发onClick

<div className={styles.navItem}>
        <Link href="/">
          <a className={styles.navLinks} onClick={toggle}>
            Home
          </a>
        </Link>
      </div>
      <div className={styles.navItem} onClick={toggle}>
        <Link href="/">
          <a
            className={styles.navLinks}
            onClick={(e) => {
             setTimeout(() => {
              document.getElementById("about") &&
                document
                  .getElementById("about")
                  .scrollIntoView({ behavior: "smooth", block: "end" 
              });
            }, 2000)
            }}
          >
            About Us
          </a>
        </Link>
      </div>
      <div className={styles.navItem} onClick={toggle}>
        <Link href="/">
          <a
            className={styles.navLinks}
            onClick={(e) => {
               setTimeout(() => {
                 document.getElementById("program") &&
                   document
                     .getElementById("program")
                     .scrollIntoView({ behavior: "smooth", block: "end" });
               }, 2000)
            }}
          >
            Program Overview
          </a>
        </Link>
      </div>
      <div className={styles.navItem}>
        <Link href="/leaderboard/">
          <a className={styles.navLinks} onClick={toggle}>
            Leaderboard
          </a>
        </Link>
      </div>

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

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