css 我怎么能把正面和背面有不同内容的卡片

cvxl0en2  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(117)

你好,我正试图与CSS程序卡,旋转时,点击和内容的变化,在旋转。但是,内容在旋转后会发生变化。如何在旋转过程中更改内容。
我试着将Map旋转到90度,改变内容,然后从90度旋转回0度。但我不知道该怎么做。
下面是代码

const [isCardTurned, setIsCardTurned] = useState(false)
    const animationInterval = useRef();
    const cardContainerRef = useRef();

  function turnCard(e) {

        cardContainerRef.current.style.transition = "1s";
        cardContainerRef.current.style.transform = "rotateY(90deg)";
        animationInterval.current = setInterval(() => {
            setIsCardTurned((prevState) => { return !prevState })
        }, 1000);
    

    }

    useEffect(() => {
        cardContainerRef.current.style.transition = "1s";
        cardContainerRef.current.style.transform = "rotateY(0deg)";
        clearInterval(animationInterval.current)

    }, [isCardTurned])

    return (
        <>
            {isCardTurned == false &&
                <>
                    <div className="cardContainer" onClick={turnCard} ref={cardContainerRef}>
                        <CardHead srcHead={props.srcHead} projektTitel={props.projektTitel} datumBegin={props.datumBegin} datumEnd={props.datumEnd} />
                        <div className="cardContent" >
                            <div className="cardImage">
                                <img src={props.imageSrc} />
                            </div>
                            <div className="cardShortDescription">
                                <div className="shortDescriptionText">
                                    {props.shortDescription}
                                </div>
                            </div>
                        </div>
                    </div>
                </>
            }
            {isCardTurned == true &&
                <>
                    <div className="cardContainer" onClick={turnCard} ref={cardContainerRef}>
                        <CardHead srcHead={props.srcHead} projektTitel={props.projektTitel} datumBegin={props.datumBegin} datumEnd={props.datumEnd} />
                        <div className="cardLongDescription">
                            <div className="cardLongDescriptionText">
                                {props.longDescription}
                            </div>
                        </div>
                    </div>
                </>
            }
        </>
    )
.cardContainer {
    height: 100%;
    display: flex;
    flex-direction: column;
    background-color: rgb(199, 220, 248);
    transition: 0.5s;
}

.cardContainer:hover {
    box-shadow: 0px 0px 50px rgb(5, 48, 112);
}

ha5z0ras

ha5z0ras1#

好吧我有办法了。我知道它是可行的,但我不能百分之百地证明它,因为我没有处理前端很长一段时间
卡片只改变其内容,而不是整个容器。出于这个原因,容器必须呈现1x,并且每次卡片转动时内容都必须呈现。
作为解决方案,我调整了呈现条件,您可以在下面的代码中看到

return (
        <>
            <div className="cardContainer" onClick={turnCard} ref={cardContainerRef}>
                <CardHead srcHead={props.srcHead} projektTitel={props.projektTitel} datumBegin={props.datumBegin} datumEnd={props.datumEnd} />
                <div className="cardContent" >
                    {isCardTurned == false &&
                        <>
                            <div className="cardImage">
                                <img src={props.imageSrc} />
                            </div>
                            <div className="cardShortDescription">
                                <div className="shortDescriptionText">
                                    {props.shortDescription}
                                </div>
                            </div>
                        </>
                    }
                    {isCardTurned == true &&
                        <>
                            <div className="cardLongDescription">
                                <div className="cardLongDescriptionText">
                                    {props.longDescription}
                                </div>
                            </div>
                        </>
                    }
                </div>
            </div>
        </>
    )

相关问题