如何更改按钮上的波纹背景颜色?

IT技术 javascript reactjs material-ui
2021-05-09 02:59:12

所以API(远v3.9.2),我看到了一个提TouchRipplePropsButtonBasehttps://material-ui.com/api/button-base/

我的按钮看起来像

<Button variant="text"
                  size={"large"}
                  fullWidth
                  className={classes.button}
          >
            {value}
          </Button>

我的按钮style是 .

 button: {
    backgroundColor: colors.surface,
    borderRadius: 0, // to make buttons sharp edged
    touchRipple: colors.primary
  }

当我触摸一个按钮时,我会看到一个白色的背景(见数字5),因为 在此处输入图片说明 我的问题是当我触摸一个按钮时,我该如何改变那个背景white,让我们说blue,然后让它消失?

更新在此处输入图片说明

2个回答

我通过对您的以下更改实现了合理的行为numberPadStyle

const numberPadStyle = theme => ({
  button: {
    backgroundColor: colors.surface,
    borderRadius: 0, // to make buttons sharp edged
    "&:hover": {
      backgroundColor: colors.primary,
      // Reset on touch devices, it doesn't add specificity
      "@media (hover: none)": {
        backgroundColor: colors.surface,
        "&:active": {
          backgroundColor: colors.primary
        }
      }
    },
    "&:active": {
      backgroundColor: colors.primary
    }
  }
});

触摸屏的问题在于触摸会触发“悬停”效果,并且在您触摸其他地方之前它不会消失。"@media (hover: none)"针对触摸设备的悬停效果(https://developer.mozilla.org/en-US/docs/Web/CSS/@media/hover)。“活动”CSS 在触摸/点击期间生效,然后内置的涟漪Button负责其余部分。

当然,您可以根据需要调整悬停和活动颜色。

编辑计算器

除了波纹之外,它不会出现其他动画。但是,您可以创建类似于此TriggeredAnimation包装器组件的内容:

class TriggeredAnimationWrapper extends Component {
  constructor(...args) {
    super(...args)
    this.state = {
      wasClicked: false
    }
    this.triggerAnimation = this.triggerAnimation.bind(this)
  }

  triggerAnimation(callback) {
    return (...args) => {
      this.setState(
        {wasClicked: true}, 
        () => requestAnimationFrame(()=>this.setState({wasClicked: false}))
      )
      if (callback) return callback(...args)
    }
  }

  render() {
    const {
      triggerAnimation,
      props: {
        children
      },
      state: {
        wasClicked
      }
    } = this.props
    return children({wasClicked, triggerAnimation})
  }
}

并像这样使用它:

<TriggeredAnimationWrapper>({wasClicked, triggerAnimation}) => (
  <Button 
    onClick={triggerAnimation((e) => console.log('clicked', e))}
    className={wasClicked ? 'clicked' : ''}
  />
)</TriggeredAnimationWrapper>

然后,您可以创建一个 css 动画并在单击类存在时更改背景。