【发布时间】:2023-03-13 22:25:01
【问题描述】:
所以我使用 useState 来处理边距,并使描述滑下 div,该 div 最初隐藏在图像本身的后面。我正在使用 map() 来显示具有相同属性的图像列表。但是使用单个图像时 useState 可以正常工作。对于多个图像,单击 o 1 个图像会激活所有图像的向下滑动 div。我希望他们分开。有什么解决办法吗?
import React,{useState} from 'react'
import Image1 from '../../images/background1.jpg'
import Image2 from '../../images/background2.jpg'
import {Arts,ArtHeading,CardContainer, Card,CardImg1,CardDetails,CardText} from './ArtsElements';
const images = [
{ title:"Title1", src:Image1},
{ title:"Title2", src:Image1},
{ title:"Title3", src:Image1},
{ title:"Title4", src:Image1},
{ title:"Title5", src:Image1},
{ title:"Title6", src:Image1},
{ title:"Title7", src:Image1},
{ title:"Title8", src:Image1},
{ title:"Title9", src:Image1},
{ title:"Title10", src:Image1},
{ title:"Title11", src:Image1},
{ title:"Title12", src:Image1},
{ title:"Title13", src:Image1},
{ title:"Title14", src:Image1},
];
const Artworks = () => {
const [isOpen,setIsOpen] = useState(true);
const toggle = () => {
setIsOpen(!isOpen)
}
return (
<Arts >
<ArtHeading>
Artworks
</ArtHeading>
<CardContainer>
{
images.map((img,index) => (
<Card >
{/* <CardImg1 key={index} src={img.src} /> */}
<CardImg1 src={Image1} onClick={toggle} />
<CardDetails isOpen={isOpen} onClick={toggle} >
<CardText >
title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title
4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4
title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4
title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4
title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4
title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title
title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4
title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4title 4
</CardText>
</CardDetails>
</Card>
))}
</CardContainer>
</Arts>
)
}
export default Artworks
样式化组件文件:
import styled from 'styled-components'
export const Arts = styled.div`
background-color: #fff;
margin-bottom: 150px;
@media screen and (max-width:768px){
margin-bottom: 100px;
}
@media screen and (max-width:480px){
margin-bottom: 100px;
}
`
export const ArtHeading = styled.h1`
font-size: 50px;
margin-left: 0px;
font-weight: bold;
font-family: Verdana, Geneva, Tahoma, sans-serif;
text-align: center;
@media screen and (max-width:768px){
font-size: 50px;
margin-left: 10px;
}
@media screen and (max-width:480px){
font-size: 30px;
margin-left: 0px;
}
`
export const CardContainer = styled.section`
margin: 50px auto;
width: 90%;
display: grid;
grid-template-columns: repeat(3,1fr);
grid-gap: 20px;
`
export const Card = styled.div`
position: relative;
height: 430px;
width:350px;
border-radius: 10px;
box-shadow: 0px 5px 10px rgb(0,0,0,0.5);
`
export const CardImg1 = styled.img`
width: 100%;
height:100%;
box-sizing: border-box;
border-radius: 10px;
`
export const CardText = styled.p`
`
export const CardDetails = styled.div`
background: white;
border-radius: 10px;
margin-top:${({isOpen}) => (isOpen ? '40px' : '440px')};
transition: 1s ease-in-out;
cursor: pointer;
`
【问题讨论】: