简体   繁体   English

React中用于图像模态的动态CSS类

[英]Dynamic CSS classes in React for image modals

I'm trying to make a simple photo gallery in React. 我正在尝试在React中制作一个简单的照相馆。 When I click on the photo preview, I want a modal of the larger image to pop up. 当我单击照片预览时,我希望弹出较大图像的模态。

I have this working so far. 到目前为止,我已经完成了这项工作。 The modal element itself is originally set to a class with display: none; 模态元素本身最初设置为带有display: none;的类display: none; . My problem is that when I click on one photo, every single modal pops up. 我的问题是,当我单击一张照片时,会弹出每个模态。

How can I change the class name of one specific modal element so this does not happen? 如何更改一个特定模态元素的类名,这样就不会发生?

Here is my code: 这是我的代码:

 import React, { Component } from 'react'; import images from './../data/images'; import './../css/Photos.css'; class Photos extends Component { constructor(props) { super(props); this.state = { images: images, visible: false, //invisibleClass: 'photo-modal-invisible', visibleClass: 'photo-modal-invisible' }; } /* Applies the appropriate class for the modal when the image is clicked. Class is handled in state. */ handleClick() { const isVisible = this.state.visible; if(isVisible === false) { this.setState({ visible: true, visibleClass: 'photo-modal-visible' }); } else { this.setState({ visible: false, visibleClass: 'photo-modal-invisible' }); } } render() { var correctClass; return( <React.Fragment> <div className="photo-container"> { this.state.images.map((image, index) => <a onClick={ () => this.handleClick() }><img src={ image.imageSrc } key={ index } /></a> ) } </div> { this.state.images.map(( image, index ) => <div className={ this.state.visibleClass } key={ index }> <a href="#" onClick={ () => this.handleClick() }><span class="close">&times;</span></a> <img src={ image.imageSrc } /> </div> ) } </React.Fragment> ); } } export default Photos; 
 .photo-container { height: 100%; width: 100%; margin-top: 50px; display: flex; flex-wrap: wrap; justify-content: center; align-items: flex-start; } .photo-container img { width: 300px; height: 300px; margin-right: 30px; object-fit: cover; filter: grayscale(100%); transition: 1s; } .photo-container img:hover { filter: grayscale(0); } .photo-modal-invisible { display: none; } .photo-modal-visible { position: fixed; /* stay in place */ z-index: 1; /* sit on top */ padding-top: 100px; /* location of the box */ left: 0; top: 0; width: 100%; height: 100%; object-fit: cover; background-color: rgb(0, 0, 0); /* fallback color */ background-color: rgba(0, 0, 0, 0.9); /* black with opacity */ } .photo-modal-visible img { margin: auto; display: block; width: 80%; max-width: 700px; } .close { position: absolute; top: 15px; right: 35px; color: #f1f1f1; font-size: 40px; font-weight: bold; transition: 0.3s; } 

You are setting the class name on all the images in the map function which is why all modal images area visible/hidden at the same time. 您要在地图功能中的所有图像上设置类别名称,这就是为什么所有模态图像同时可见/隐藏的原因。

In the handleClick pass the index of the image and store that index in the state and use that to toggle the modal 在handleClick中传递图像的索引,并将该索引存储在状态中,并使用它来切换模式

Why don't you create another component for a single image and keep the visible state on a single image. 为什么不为单个图像创建另一个组件,并在单个图像上保持可见状态。 So when you click on a single image, it's not affecting other image state. 因此,当您单击单个图像时,它不会影响其他图像状态。

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

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