首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react-scroll的活动类不能正常工作

react-scroll的活动类不能正常工作
EN

Stack Overflow用户
提问于 2020-03-19 11:42:26
回答 2查看 3.7K关注 0票数 2

当我运行页面并滚动到<Element></Element>时,活动类没有显示,也没有在DOM中呈现

我在某处读到几年前发布的关于你必须给scrollSpy.update()打电话的文章。但是,还是不能工作。

我正在跟踪Docs实现

这是我的代码。

代码语言:javascript
复制
import React, { useEffect } from "react";
import { makeStyles } from "@material-ui/core/styles";
import AppBar from "@material-ui/core/AppBar";
import Toolbar from "@material-ui/core/Toolbar";
import Typography from "@material-ui/core/Typography";
import styles from "./navbar.module.css";
import { Link, Element } from "react-scroll";
import * as Scroll from "react-scroll";

const useStyles = makeStyles(theme => ({
  root: {
    flexGrow: 1
  },
  menuButton: {
    marginRight: theme.spacing(2)
  },
  title: {
    flexGrow: 1
  }
}));

export default function ButtonAppBar(props) {
  let scrollSpy = Scroll.scrollSpy;
  const classes = useStyles();

  useEffect(() => {
    scrollSpy.update();
  });

  return (
    <div className={classes.root}>
      <AppBar style={{ backgroundColor: "#343567" }} position="fixed">
        <Toolbar>
          <Typography variant="h6" className={classes.title}>
            Welcome
          </Typography>
          <Link
            activeClass="active"
            to="test1"
            spy={true}
            smooth={true}
            offset={-70}
            duration={800}
          >
            Test 1
          </Link>
          <Link
            activeClass="active"
            to="test2"
            spy={true}
            smooth={true}
            offset={-70}
            duration={800}
          >
            Test 2
          </Link>
          <Link
            activeClass="active"
            to="test3"
            spy={true}
            smooth={true}
            offset={-70}
            duration={800}
          >
            Test 3
          </Link>
          <Link
            activeClass="active"
            to="test4"
            spy={true}
            smooth={true}
            offset={-70}
            duration={800}
          >
            Test 4
          </Link>
        </Toolbar>
      </AppBar>
    </div>
  );
}

下面是为了便于访问而滚动到的元素。

代码语言:javascript
复制
<Element name="test1" className="element">
  test 1
</Element>

<Element name="test2" className="element">
  test 2
</Element>

<Element name="test3" className="element">
  test 1
</Element>

<Element name="test4" className="element">
  test 2
</Element>
EN

回答 2

Stack Overflow用户

发布于 2020-03-19 12:45:05

我想,你忘了这一点。

代码语言:javascript
复制
import { Link, Element } from "react-scroll";

希望就是帮助。

票数 0
EN

Stack Overflow用户

发布于 2020-07-30 00:11:23

您需要将元素设置为“id”,而不是“name”

例如:

代码语言:javascript
复制
<Element id="test1" className="element">
  test 1
</Element>

<Element id="test2" className="element">
  test 2
</Element>

<Element id="test3" className="element">
  test 1
</Element>

<Element id="test4" className="element">
  test 2
</Element> 
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60750600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档