【问题标题】:Smooth scroll in next js在下一个 js 中平滑滚动
【发布时间】:2021-11-03 13:25:42
【问题描述】:

如何在 Next.js 中将滚动效果设置为平滑(全局)? 我尝试在全局 css 上执行此操作,但它停用了 Next js 已经拥有的滚动到顶部。

我也尝试了在互联网上找到的这个解决方案,但也没有用。

 componentDidMount() {
 Router.events.on('routeChangeComplete', () => {
    window.scroll({
       top: 0,
       left: 0,
       behavior: 'smooth'
    });
 });

}

【问题讨论】:

    标签: reactjs next.js smooth-scrolling


    【解决方案1】:

    只需将style={{scrollBehavior:'smooth'} 放在'_document.tsx' 文件的标签Html 中。

    喜欢:

    class MyDocument extends Document {
      static async getInitialProps(ctx: DocumentContext) {
        const initialProps = await Document.getInitialProps(ctx);
        return { ...initialProps };
      }
    
      render() {
        return (
          <Html className='scroll-smooth' style={{scrollBehavior:'smooth'}}>
            <Head>
              <link rel='icon' href='/favicon.ico' />
              <meta
                name='description'
                content='A place to find a great film to watch'
              />
            </Head>
            <body className='bg-gray-50 screen'>
              <Main />
              <NextScript />
            </body>
          </Html>
        );
      }
    }
    

    【讨论】:

    • 这也会在页面路径上添加不需要的滚动。
    【解决方案2】:

    我确实使用 npm-package smooth-scroll。你可以在这里测试:https://easy-car-service.de/

    实现起来真的很简单:

    【讨论】:

    • 请以sn-p形式提供代码,请勿使用图像作为代码。
    【解决方案3】:

    我解决了!您可以使用 npm 包 react-scroll 执行此操作(不是全局的,但可以正常工作) 这是链接:https://www.npmjs.com/package/react-scroll

    【讨论】:

      【解决方案4】:

      如果您使用的是类组件,您可以复制并粘贴以下代码:

      import React, { Component } from 'react';
      
      export default class ScrollToTopButton extends Component {
        // this is function that will scroll to the top of the page when the button is clicked
        scrollToTop = () => {
          window.scrollTo({
            top: 0,
            behavior: 'smooth',
          });
        };
      
        render() {
          return (
            <>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              <div> textInComponent </div>
              {/* This is the button that will go up */}
              <button onClick={() => this.scrollToTop()}> textInComponent </button>
            </>
          );
        }
      }
      

      或者,如果你正在实现一个功能组件

      import React, { Component } from 'react';
      
      function ScrollToTopButton() {
        // this is function that will scroll to the top of the page when the button is clicked
        const scrollToTop = () => {
          window.scrollTo({
            top: 0,
            behavior: 'smooth',
          });
        };
      
        return (
          <>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            <div> textInComponent </div>
            {/* This is the button that will go up */}
            <button onClick={() => scrollToTop()}> textInComponent </button>
          </>
        );
      }
      
      export default ScrollToTopButton;
      

      我不知道如何描述这段代码,但它对我有用;希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-13
        • 1970-01-01
        • 2015-12-16
        • 1970-01-01
        • 1970-01-01
        • 2011-12-31
        • 1970-01-01
        • 2017-08-20
        相关资源
        最近更新 更多