1

我正在使用styled-components我的组件的动态样式,但我似乎无法让它以我想要的方式工作。

我正在尝试在以ease-in-out编程方式和动态聚焦的按钮上添加效果。

在此处输入图像描述

如您所见,样式已应用,但该transition属性没有任何效果。有没有办法做到这一点?

就像信息说明一样,组件(按钮)每 3 秒重新渲染一次以进行切换,这可能是原因吗?如果是这样如何解决这个问题?

零件

class LanguageButton extends PureComponent {
    render() {
        const { children, theme, isFocussed, ...otherProps } = this.props;

        const Button = styled.div`
            border: 1px solid ${theme};
            margin-top: 10px;
            margin-bottom: 10px;
            background-color: transparent;
            text-align: center;
            padding-top: 25px;
            padding-bottom: 25px;
            min-height: 0;
            transition: all 0.1s ease-in-out;

            :hover {
                background-color: ${theme};
                transition: all 0.1s ease-in-out;
            }
            :active {
                background-color: ${theme};
            }

            &.isFocussed {
                transition: all 0.1s ease-in-out;
                background-color: #ebebeb;

                :hover {
                    background-color: ${theme};
                    transition: all 0.1s ease-in-out;
                }
                :active {
                    background-color: ${theme};
                }
            }
        `;

        return (
            <Button
                className={classnames("BUTTON BUTTON--50 GM--noselect", { isFocussed })}
                {...otherProps}
            >
                {children}
            </Button>
        );
    }
}
4

1 回答 1

0

乍一看,基于此代码片段,我假设您正在添加/删除此“isFocused”类,这是您问题的根源。使用过渡,您希望避免添加/删除类本身,因为过渡无法正确呈现,因为它一开始就不存在。

于 2017-08-09T15:43:14.480 回答