简体   繁体   English

按钮的悬停过渡效果

[英]Hover transition effect for Button

I try to add a one-second transition from the normal color scheme to the hovering color for the Button element in Material UI. 我尝试为Material UI中的Button元素添加一秒钟的过渡,从正常配色方案到悬停颜色。 I use the method transitions.create(props, options) talked here in this article: https://medium.com/@octaviocoria/custom-css-transitions-with-react-material-ui-5d41cb2e7c5 . 我使用本文在这里讨论的transitions.create(props, options)方法: https : //medium.com/@octaviocoria/custom-css-transitions-with-react-material-ui-5d41cb2e7c5 Actually the transition can work, but the hovering color will also appear immediately on the button as long as the mouse hovers on it. 实际上过渡效果可以,但是只要鼠标悬停在按钮上,悬停颜色也会立即显示在按钮上。 How can I stop the color change immediately but wait for the transition to make it change. 如何立即停止颜色更改,但要等待过渡使其更改。

Related code: 相关代码:

function Mbutton({ classes }) {
  return (
    <Button variant="outlined" className={classes.cart} disableRipple>
      Add to Cart
    </Button>
  );
}

const styles = theme => ({
  cart: {
    padding: "6px 16px",
    borderRadius: 0,
    border: "2px solid #000",
    "&:hover": {
      transition: theme.transitions.create(["backgroundColor", "color"], {
        duration: 1000
      }),
      backgroundColor: "#000",
      color: "#fff"
    }
  }
});

Also can check it on Code Sandbox: https://codesandbox.io/s/mbutton-fogco 也可以在代码沙箱中检查它: https : //codesandbox.io/s/mbutton-fogco

Thanks. 谢谢。

Write it like you do in plain CSS 像使用普通CSS一样编写

const styles = theme => ({
    cart: {
        padding: "6px 16px",
        borderRadius: 0,
        border: "2px solid #000",
        backgroundColor: "white",
        color: "black",
        transition: "background 1s, color 1s",
        "&:hover": {
        backgroundColor: "#000",
        color: "#fff"
        }
    }
});

CodeSandBox CodeSandBox

The theme passed is unnecessary, you should only use that if you want to use the transition styles available on the default Material-UI theme object, which you didn't need it here. 传递的主题是不必要的,仅当您要使用默认Material-UI主题对象上可用的过渡样式时才应使用该主题,这里不需要。

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

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