【发布时间】:2019-11-22 16:09:20
【问题描述】:
我有一个按钮,我想在单击它时对其进行动画处理(理想情况下,它会将状态永久更改为第二种颜色)。虽然我尝试了背景图像/线性渐变解决方案,但它没有达到我想要的圆形效果并且没有持续存在。
以this CodePen 为灵感,我想我会尝试使用 ::before 伪选择器为圆圈设置动画。
const ClickAnimation = keyframes`
from {
transform: scale(0);
opacity: 1;
}
to {
transform: scale(1);
opacity: 1;
}
`;
const StyledButton = styled.button`
color: white;
background-color: ${colors.secondary500};
text-align: center;
text-transform: uppercase;
&:hover {
background-color: ${colors.secondary400};
}
&::before {
content: "";
background-color: ${colors.secondary300};
}
&:active::before {
animation: ${ClickAnimation} 0.5s cubic-bezier(0.250, 0.460, 0.450, 0.940) both;
}
&:disabled {
background-color: ${colors.primary300};
}
`
const LargeButton = styled(StyledButton)`
width: 100%;
line-height: 23px;
font-size: 19px;
font-weight: 600;
padding: 8px 0;
border-radius: 4px;
`
const SmallButton = styled(StyledButton)`
height: 28px;
width: 148px;
border-radius: 16px;
font-size: 15px;
font-weight: 500;
line-height: 18px;
`
export default Button;
虽然我的悬停效果有效,但目前我实际上并没有在点击 (:active) 时获得任何动画。
【问题讨论】:
标签: css reactjs css-animations styled-components