简体   繁体   English

在 React JS 中打开抽屉时如何防止内容移动?

[英]how to prevent content shift when drawer open in react js?

I am creating Drawer example.我正在创建抽屉示例。 I am following this link https://mui.com/material-ui/react-drawer/我正在关注此链接https://mui.com/material-ui/react-drawer/

Everything work as expected.But I am facing one issue when my drawer open my content shift to right and when it close it come to remain to it's original position .It's look like bad layout changed when drawer opens.一切都按预期工作。但是当我的抽屉打开时,我遇到了一个问题,我的内容向右移动,当它关闭时,它会保持原来的状态 position 。当抽屉打开时,布局似乎发生了变化。

Expected Behavior: Drawer should come Overlay/ above the content预期行为:抽屉应该覆盖/在内容之上

here my code这是我的代码

https://codesandbox.io/s/priceless-hooks-5jcjym?file=/demo.tsx:1545-1568 https://codesandbox.io/s/priceless-hooks-5jcjym?file=/demo.tsx:1545-1568

const drawerWidth = 240;

const openedMixin = (theme: Theme): CSSObject => ({
  width: drawerWidth,
  transition: theme.transitions.create("width", {
    easing: theme.transitions.easing.sharp,
    duration: theme.transitions.duration.enteringScreen
  }),
  overflowX: "hidden"
});

const closedMixin = (theme: Theme): CSSObject => ({
  transition: theme.transitions.create("width", {
    easing: theme.transitions.easing.sharp,
    duration: theme.transitions.duration.leavingScreen
  }),
  overflowX: "hidden",
  width: `calc(${theme.spacing(7)} + 1px)`,
  [theme.breakpoints.up("sm")]: {
    width: `calc(${theme.spacing(8)} + 1px)`
  }
});

const DrawerHeader = styled("div")(({ theme }) => ({
  display: "flex",
  alignItems: "center",
  justifyContent: "flex-end",
  padding: theme.spacing(0, 1),
  // necessary for content to be below app bar
  ...theme.mixins.toolbar
}));

interface AppBarProps extends MuiAppBarProps {
  open?: boolean;
}

const AppBar = styled(MuiAppBar, {
  shouldForwardProp: (prop) => prop !== "open"
})<AppBarProps>(({ theme, open }) => ({
  zIndex: theme.zIndex.drawer + 1,
  transition: theme.transitions.create(["width", "margin"], {
    easing: theme.transitions.easing.sharp,
    duration: theme.transitions.duration.leavingScreen
  }),
  ...(open &&
    {
      // marginLeft: drawerWidth,
      // width: `calc(100% - ${drawerWidth}px)`,
      // transition: theme.transitions.create(["width", "margin"], {
      //   easing: theme.transitions.easing.sharp,
      //   duration: theme.transitions.duration.enteringScreen
      // })
    })
}));

const Drawer = styled(MuiDrawer, {
  shouldForwardProp: (prop) => prop !== "open"
})(({ theme, open }) => ({
  width: drawerWidth,
  flexShrink: 0,
  whiteSpace: "nowrap",
  boxSizing: "border-box",
  ...(open && {
    ...openedMixin(theme),
    "& .MuiDrawer-paper": openedMixin(theme)
  }),
  ...(!open && {
    ...closedMixin(theme),
    "& .MuiDrawer-paper": closedMixin(theme)
  })
}));

在此处输入图像描述

any suggestion?有什么建议吗?

Addposition property for make overlay effect.添加position属性以制作叠加效果。 absolute for normal case, sticky for sticky overlay. absolute用于正常情况, sticky用于粘性覆盖。

const DrawerHeader = styled("div")(({ theme }) => ({
  position: "absolute", // Add this: absolute or sticky
  display: "flex",
  alignItems: "center",
  justifyContent: "flex-end",
  padding: theme.spacing(0, 1),
  // necessary for content to be below app bar
  ...theme.mixins.toolbar
}));

also, you can add left and top for your drawer position.另外,您可以为抽屉添加lefttop position。

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

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