javascript - Material-UI按钮点击后消失

标签 javascript css reactjs material-ui

我有一个按钮,点击后它会消失。此外,单击按钮一次不会导致任何按钮操作运行。我必须点击按钮,然后点击按钮消失后所在的区域,我的按钮操作才能生效。

<Grid className={classes.container} style={{justifyContent: 'flex-end'}} item xs={12}>
            <Button className={classes.addImage} onClick={this.addPic}>
                <input 
                className={classes.takePic} 
                ref="file"
                id="takePic" 
                type="file" 
                accept="image/*"
                onChange={this.onChange}
                />
                Add 
                <br></br>
                Image

            </Button>
        </Grid>

样式:

 addImage: {
    display: 'flex',
    backgroundColor: 'black',
    color: 'white',
    borderRadius: 90,
    height: 100,
    width: 100,
    justifySelf: 'flex-end',
    marginRight: '12.5%',
},

onChange 函数:

    onChange = () => {
    let newfile = this.refs.file.files[0];
    let reader = new FileReader();
    let url = reader.readAsDataURL(newfile);
    reader.onloadend = () => {
        this.setState({
            ...this.state,
            openModal: true,
            imgSrc : [reader.result],
            imageType: newfile.type,
            newfile: newfile,
            filename: `${this.props.user.id}_${Date.now()}`
        })
        console.log(newfile)
        console.log(this.state)

    }
}

添加图片功能:

addPic = () => {
        document.getElementById('takePic').click()
    }

最佳答案

在覆盖 Material-UI 的 Button 颜色的 CSS 时必须小心。如果您覆盖颜色而不遵循 Button 中使用的模式,很容易在触摸设备上产生不良影响(尤其是“悬停”状态)。

以下是 Button 处理“文本”变体(默认)颜色的样式的异常(exception)情况:

export const styles = theme => ({
  /* Styles applied to the root element. */
  root: {
    color: theme.palette.text.primary,
    transition: theme.transitions.create(['background-color', 'box-shadow', 'border'], {
      duration: theme.transitions.duration.short,
    }),
    '&:hover': {
      backgroundColor: fade(theme.palette.text.primary, theme.palette.action.hoverOpacity),
      // Reset on touch devices, it doesn't add specificity
      '@media (hover: none)': {
        backgroundColor: 'transparent',
      },
      '&$disabled': {
        backgroundColor: 'transparent',
      },
    },
    '&$disabled': {
      color: theme.palette.action.disabled,
    },
  },
  /* Styles applied to the root element if `disabled={true}`. */
  disabled: {},
});

在您的 addImage 类中,您将按钮的 backgroundColor 更改为黑色,将 color 更改为白色,但您没有处理应该发生的事情悬停。由于特殊性,Material-UI 的样式将赢得悬停,并且在触摸设备上 ('@media (hover: none)') 背景将变得透明,但是您更改 color 到“白色”(而不是 theme.palette.text.primary)仍然有效,如果您的页面背景是白色,则意味着您的按钮现在不可见。

您可以通过明确说明悬停时应该发生什么来解决此问题,如我在此处的回答所示:How do I change the ripple background color on Button? .

Button 源代码(有关 Material-UI 样式的完整详细信息):https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/Button/Button.js

关于javascript - Material-UI按钮点击后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55343138/

相关文章:

javascript - 如何在 javascript 中将变量附加(连接)到常量

javascript - 更新 CSS :width onblur with JQuery

javascript - 如何部分显示 Div 并在单击时显示完整

javascript - 尝试访问 Api 中的数组但没有返回任何内容

javascript - 如何使用 js 或 jQuery 向 ajax 请求添加自定义 HTTP header ?

javascript - CKEDITOR 中 allowedContent 的语法

html - 无法让html元素在同一行

javascript - 如何检索页面部分的 html 和样式?

javascript - 使用 ReactJS 处理 AJAX 调用后的 Rails flash 消息

reactjs - React Native - 为什么我需要 babel 或 webpack?