简体   繁体   English

如何在 React 中隐藏组件的 div 内的滚动?

[英]How to hide scroll inside div of a Component in React?

In a React project I've details of wallet scrolling in a div named 'History'.在一个 React 项目中,我在名为“History”的 div 中详细介绍了钱包滚动。 Issue is that history details is scrolling without any issue, but, I want to hide inside 'History' div.问题是历史详细信息正在滚动没有任何问题,但是,我想隐藏在“历史”div 中。 How could it be done?怎么可能做到? Any appropriate solution?有什么合适的解决方案吗?

Below is the code reference下面是代码参考

const navScrollStyle = makeStyles((theme) => ({
  root: {
    marginTop: "120px",
    display: "table",
    overflowY: "auto",
    maxWidth: "auto",
    display: "flex",
    justifyContent: "center"
  }
}));

const navBodyStyle = makeStyles((theme) => ({
  root: {
    flexGrow: 1,
    top: "0px",
    position: "absolute",
    width: "100%",
    height: "100vh",
    textAlign: "center",
    background: "white",
    zIndex: "9",
    height: "100%",
    overflowY: "auto"
  },
  menuButton: {
    marginRight: theme.spacing(2),
    color: "purple"
  },
  title: {
    flexGrow: 1,
    textAlign: "center",
    color: "black"
  }
}));

const gridClassStyle = makeStyles((theme) => ({
  root: {
    zIndex: "100",
    flexGrow: 1,
    position: "fixed",
    top: "0px",
    background: "white",
    flexWrap: "nowrap",
    boxShadow: "5px 10px 18px #888888"
  },
  paper: {
    padding: theme.spacing(2),
    textAlign: "center",
    color: theme.palette.text.secondary
  },
  menuButton: {
    marginRight: theme.spacing(2),
    color: "purple"
  },
  title: {
    flexGrow: 1,
    textAlign: "center",
    color: "black"
  }
}));

const useStyles5 = makeStyles((theme) => ({
  root: {
    "& > *": {
      margin: theme.spacing(1)
    },
    textAlign: "center",
    width: "100%"
  }
}));

const useStyles = makeStyles((theme) => ({
  root: {
    top: "20px",
    width: "100%",
    maxWidth: "auto"
  },
  nested: {
    paddingLeft: theme.spacing(4)
  }
}));

const TestPageScroll = () => {
  const navBody = navBodyStyle();
  const navScroll = navScrollStyle();
  const gridClass = gridClassStyle();

  const classes5 = useStyles5();

  const classes = useStyles();

  const NavPart = () => (
    <Grid className={gridClass.root} container spacing={3}>
      <Grid item xs={4} style={{ textAlign: "left" }}>
        <IconButton
          edge="start"
          className={gridClass.menuButton}
          color="inherit"
          aria-label="menu"
        >
          <Link to="/">
            <ArrowBackIcon />
          </Link>
        </IconButton>
      </Grid>
      <Grid
        item
        xs={4}
        style={{ textAlign: "center", justifyContent: "center" }}
      >
        <Typography variant="h6" className={gridClass.title}>
          <h2>Test Scroll</h2>
        </Typography>
      </Grid>
      <Grid item xs={4} style={{ textAlign: "right", marginTop: "2%" }}>
        <IconButton
          aria-label="account of current user"
          aria-controls="menu-appbar"
          aria-haspopup="true"
        >
          <AccountCircle style={{ fontSize: "30px" }} />
        </IconButton>
      </Grid>
    </Grid>
  );
  return (
    <div className={navBody.root}>
      {NavPart()}
      <div className={navScroll.root}>

      </div>

      {/* Here the main code begins */}
      <div style={{ height: "100px" }}>
        <div
          style={{
            position: "fixed",
            zIndex: "100",
            background: "white",
            width: "100%"
          }}
        >
          <h2>History</h2>
        </div>
        <hr />

        <List
          component="nav"
          aria-labelledby="nested-list-subheader"
          className={classes.root}
        >
          <>
            {/* Here purposely List are added to test overflow */}
            <div style={{ overflowY: "auto", background: "red" }}>
              <ListItem>
                <h6>Wallet History1</h6>
              </ListItem>
              <Divider />

              <ListItem>
                <h6>Wallet History</h6>
              </ListItem>
              <Divider />

              <ListItem>
                <h6>Wallet History</h6>
              </ListItem>
              <Divider />

              <ListItem>
                <h6>Wallet History</h6>
              </ListItem>
              <Divider />

              <ListItem>
                <h6>Wallet History</h6>
              </ListItem>
              <Divider />

              <ListItem>
                <h6>Wallet History</h6>
              </ListItem>
              <Divider />

              <ListItem>
                <h6>Wallet History</h6>
              </ListItem>
              <Divider />

              <ListItem>
                <h6>Wallet History</h6>
              </ListItem>
              <Divider />

              <ListItem>
                <h6>Wallet History</h6>
              </ListItem>
              <Divider />

              <ListItem>
                <h6>Wallet History</h6>
              </ListItem>
              <Divider />
            </div>
          </>
        </List>
      </div>
    </div>
  );
};

As you can see below in the picture, Wallet history details is seen clearly which I want to hide inside 'History' div正如您在图片中看到的那样,可以清楚地看到钱包历史详细信息,我想将其隐藏在“历史”div 中

在此处输入图像描述

Here is the codesandbox link for your reference: https://codesandbox.io/s/react-material-forked-71op5 Please click on 'WALLET' button when page loads这是供您参考的代码框链接: https://codesandbox.io/s/react-material-forked-71op5请在页面加载时单击“钱包”按钮

The problem is your History div is fixed and your Wallet History List div has no bounds on height which is getting scrolled behind the fixed div s above.问题是您的History divfixed的,并且您的Wallet History List div没有height限制,它在上面的fixed div后面滚动。 one way to solve this problem is to limit your height of your Wallet History List div and make it fixed as well.解决此问题的一种方法是限制您的Wallet History List divheight并使其fixed

eg.例如。 add these style to your div for list将这些样式添加到您的div列表中

maxHeight: "800px", position:"fixed", width:"100%"

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

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