MUI幻灯片动画流畅推出元素

huangapple go评论65阅读模式
英文:

MUI Slide animation smooth push out of elements

问题

我目前正在尝试在卡片上的按钮中使用 React MUI 的 [Slide][1]。幻灯片动画效果很好,但是当元素挂载时,它会立即将其上方的内容弹出到顶部,因为在挂载时元素的高度是完整的。

有人有解决这个问题的想法吗?当前的结构如下:

          <Slide
            unmountOnExit
            key={2}
            direction="up"
            in={showLink}
            container={containerRef.current}>
            <Button
              sx={{
                mt: 1,
              }}
              variant="contained"
              color="secondary">
              立即支持
            </Button>
          </Slide>

[![输入图像说明][2]][2]

  [1]: https://mui.com/material-ui/transitions/#slide
  [2]: https://i.stack.imgur.com/vPZPi.gif
英文:

I am currently trying to use the React MUI Slide in a button over a card. The slide animation works well, however it snaps out the content over it to the top instantly when it mounts since the element is with full height when it mounts.

Does anyone have ideas on how to solve that. This is the structure currently:

      &lt;Slide
        unmountOnExit
        key={2}
        direction=&quot;up&quot;
        in={showLink}
        container={containerRef.current}&gt;
        &lt;Button
          sx={{
            mt: 1,
          }}
          variant=&quot;contained&quot;
          color=&quot;secondary&quot;&gt;
          Support now
        &lt;/Button&gt;
      &lt;/Slide&gt;

MUI幻灯片动画流畅推出元素

答案1

得分: 1

以下是代码部分的翻译:

如果您将position: "absolute"应用于按钮,并将position: "relative"应用于containerRef所引用的组件,您将取消滑动按钮的块定位,并获得我认为您想要的行为。

const containerRef = useRef();
[...]
<ContainerBox
  ref={containerRef} 
  sx={{ position: "relative", overflow: "hidden" }}
>
  <Slide {...slideProps} >
    <Button 
      sx={{ position: "absolute" }}
    >
      Button
    </Button>
  </Slide>
</ContainerBox>

编辑:

响应评论,尝试使文本在悬停时与按钮一起过渡。

由于 MUI 的 Slide 仅为子元素添加内联过渡样式,您可以使用一些简单的 CSS 模仿相同的行为。通过将相同的过渡属性应用于文本和按钮元素,它们应该表现相同。此外,您可以使用 useTheme() 钩子从主题中获取过渡变量。

例如:

const theme = useTheme();

const transitionStyle = {
  position: "absolute",
  bottom: `${hovered ? "0px" : "-36px"}`,
  transitionProperty: "bottom",
  transitionDuration: `${theme.transitions.duration.standard}ms`,
  transitionTimingFunction: `${theme.transitions.easing.easeIn}`
};

[...]

<Typography
  sx={{
    ...transitionStyle,
    marginBottom: "36px" //将文本推上按钮的高度
  }}
>
  Stuff
</Typography>
<Button variant="contained" sx={transitionStyle}>
  Button
</Button>

这是我想出的代码:CodeSandbox 链接

英文:

If you apply position: &quot;absolute&quot; to the button and position: &quot;relative&quot; to the component to which containerRef refers, you will remove the block positioning of the sliding button and get the behavior that I think you want.

const containerRef = useRef();
[...]
&lt;ContainerBox
  ref={containerRef} 
  sx={{ position: &quot;relative&quot;, overflow: &quot;hidden&quot; }}
&gt;
  &lt;Slide {...slideProps} &gt;
    &lt;Button 
      sx={{ position: &quot;absolute&quot; }}
    &gt;
      Button
    &lt;/Button&gt;
  &lt;/Slide&gt;
&lt;/ContainerBox&gt;

Edit:

In response to the comment and to try and to get the text to transition along with the button on hover.

Since MUI Slide simply adds in-line transition styling to the child element, you can mimic the same behavior with some simple CSS. By applying the same transition properties to both the text and button elements, they should act the same. Also, you can pull the transition variables from the theme by the useTheme() hook.

eg:

const theme = useTheme();

const transitionStyle = {
  position: &quot;absolute&quot;,
  bottom: `${hovered ? &quot;0px&quot; : &quot;-36px&quot;}`,
  transitionProperty: &quot;bottom&quot;,
  transitionDuration: `${theme.transitions.duration.standard}ms`,
  trasitionTimingFunction: `${theme.transitions.easing.easeIn}`
};

[...]

&lt;Typography
  sx={{
    ...transitionStyle,
    marginBottom: &quot;36px&quot; //push the text up by the button height
  }}
&gt;
  Stuff
&lt;/Typography&gt;
&lt;Button variant=&quot;contained&quot; sx={transitionStyle}&gt;
  Button
&lt;/Button&gt;

Here's what I came up with: CodeSandbox Link

huangapple
  • 本文由 发表于 2023年2月14日 21:17:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定