0

这个反应滚动我做错了什么?我想单击导航栏链接并平滑滚动到同一页面上的相应部分。但是,单击导航项目不会执行任何操作。有人可以帮我解释为什么这不起作用吗?

我跑了 npm install react-scroll。

应用程序.js

import './App.css';
import React from 'react';

import Navbar from './components/Navbar/Navbar';
import About from './components/About/About';
import Skills from './components/Skills/Skills';

function App() {
  return (
    <React.Fragment>
      <header>
        <Navbar />
      </header>
      <main>
        <About id='about' />
        <Skills id='skills' />
      </main>
    </React.Fragment>
  );
}

export default App;

导航栏.js

import { Link as LinkScroll } from 'react-scroll';

const Navbar = () => {
  return (
    <nav> 
      <LinkScroll
        activeClass='active'
        to='about'
        spy={true}
        smooth={true}
        offset={-70}
        duration={500}
      >
        About
      </LinkScroll>
      <LinkScroll
        activeClass='active'
        to='skills'
        spy={true}
        smooth={true}
        offset={-70}
        duration={500}
      >
        Skills
      </LinkScroll>
    </nav>
  );
};

export default Navbar;
4

1 回答 1

0

id从组件标签中删除:

<main>
    <About />
    <Skills />
</main>

然后在这些组件的根选项卡中添加这些 id。假设您的组件是:

import React from "react";

export default ({ name }) => (
  <div id="skills" style={{ height: "500px" }}>
    <h1>Hello {name}!</h1>
  </div>
);
于 2021-09-09T08:26:15.793 回答