简体   繁体   English

与Material-ui卡媒体反应,点击添加叠加

[英]React with Material-ui card media, add overlay on click

Its possible to add a opaque colored overlay in plain HTLM, CSS and JavaScript to an image but with Material-UI I'm having some problems. 可以在普通HTLM,CSS和JavaScript中为图像添加不透明的彩色叠加层,但使用Material-UI我遇到了一些问题。

<Card>
   <CardMedia>
      <img alt="" src={this.props.Webpull} />
   </CardMedia>    
</Card>

This will add a card and inbed an a image into it. 这将添加一张卡片并在其中嵌入图像。

How can I add an overlay on-top of the image rgb(0,0,255,0.3) when I click on it, and have the overlay stay forever? 当我点击它时,如何在图像rgb(0,0,255,0.3)的顶部添加叠加层,并使叠加层永远保留?

Thanks! 谢谢!

If I understood the problem right, this is the solution you were looking for. 如果我理解正确的问题,这就是您正在寻找的解决方案。 The component is wrapped in a div that has another div with a class of layer . 该组件包含在一个div ,该div具有另一个带有layer div

class MyCard extends React.Component {
  imageClick = () => {
    this.refs.layer.style.display = "block";
  };
  render() {
    const { classes } = this.props;
    return (
      <Card>
        <div className="cardWrapper" onClick={this.imageClick}>
          <CardMedia
            alt="My cool img"
            component="img"
            className={classes.media}
            image="https://placeimg.com/100/100/nature"
          />
          <div className="layer" ref="layer">
            New layer
          </div>
        </div>
      </Card>
    );
  }
}

...and CSS... ......和CSS ......

.cardWrapper {
  display: inline-block;
  position: relative;
  cursor: pointer;
}

.layer {
  position: absolute;
  background-color: rgba(0, 0, 255, 0.3);
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: none;
}

The layer will stay forever as you wanted. 该层将永远保持不变。

Example

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

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