简体   繁体   English

如何防止在 React JS 中单击按钮时发生布局偏移

[英]how to prevent layout shift on button click 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 bad as layout is shifting when drawer opens.Is there any way to prevent.I saw example here the layout is not shifting example ( https://codesandbox.io/s/j9je2v?file=/demo.tsx ).一切都按预期工作。但是当我的抽屉打开时我遇到一个问题我的内容向右移动并且当它关闭时它会保持原来的状态 position 。它看起来很糟糕,因为抽屉打开时布局正在移动。有什么办法可以防止.我在这里看到示例布局没有移动示例( https://codesandbox.io/s/j9je2v?file=/demo.tsx )。 can we create drawer like this我们可以像这样创建抽屉吗

Expected Behavior : Drawer should not shift layout预期行为抽屉不应改变布局

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)
  })
}));

Current behaviour当前行为

在此处输入图像描述

Expected behavior : Drawer doesn't shift layout.It comes over the text预期行为:抽屉不改变布局。它覆盖文本在此处输入图像描述 any suggestion?有什么建议吗?

Expected output simpler to预期 output 更简单

Just adding position:fixed to const Drawer did the trick只需将 position:fixed 添加到 const Drawer 就可以了

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

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

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

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