【发布时间】:2022-01-17 20:24:15
【问题描述】:
我正在尝试制作一个抽认卡应用来记住有关 Javascript 的基础知识。
我可以按应有的方式显示卡片,但如果卡片背面的文字比卡片正面长,则文字会消失,然后在悬停时偶尔会闪烁。
会发生什么:https://agitated-shaw-22f47c.netlify.app/
第一张卡片是问题所在,因为前面的短文本导致它出现故障,但第二张卡片可以正常工作。
这是我的 github 仓库:https://github.com/SillasPoulsen/flashcard
这是我的css:
background-color: #c8d0d2;
margin: 0;
}
.container {
max-width: 900px;
margin: 1rem 2rem;
}
.header {
display: flex;
justify-content: flex-end;
align-items: center;
flex-wrap: wrap;
background-color: white;
padding: 0 1.5rem;
box-shadow: 0 0 5px 2px rgba(0, 0, 0, 0.3);
}
.form-group {
display: flex;
flex-direction: column;
margin: 0.5rem;
}
.form-group > label {
color: #777;
font-size: 0.75rem;
margin-bottom: 0.25rem;
}
.btn {
background-color: hsl(200, 100%, 50%);
color: white;
padding: 0.5em 1em;
border-radius: 0.3em;
border: none;
cursor: pointer;
}
.btn:hover {
background-color: hsl(200, 100%, 40%);
}
.card-grid {
display: grid;
align-items: center;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 1rem;
}
.card {
display: flex;
justify-content: center;
align-items: center;
position: relative;
border-radius: 0.25rem;
box-shadow: 0 0 5px 2px rgba(0, 0, 0, 0.3);
background-color: white;
transform-style: preserve-3d;
transition: 150ms;
cursor: pointer;
transform: perspective(1000px) rotateY(var(--rotate-y, 0))
translateY(var(--translate-y, 0));
}
.card:hover {
--translate-y: -2px;
box-shadow: 0 0 5px 2px rgba(0, 0, 0, 0.5);
}
.card.flip {
--rotate-y: 180deg;
}
.card .front {
left: 0;
}
.card .front,
.card .back {
position: absolute;
padding: 1rem;
backface-visibility: hidden;
}
.card .back {
transform: rotateY(180deg);
}
.flashcard-options {
margin-top: 0.5rem;
}
.flashcard-option {
margin-top: 0.25rem;
color: #555;
font-size: 0.75rem;
}
.flashcard-option:first-child {
margin-top: 0;
}
这是我的组件反应代码:
import React, { useState, useEffect, useRef } from "react";
export default function Flashcard({ flashcard }) {
const [flip, setFlip] = useState(false);
const [height, setHeight] = useState("initial");
const frontEl = useRef();
const backEl = useRef();
function setMaxHeight() {
const frontHeight = frontEl.current.getBoundingClientRect().height;
const backHeight = backEl.current.getBoundingClientRect().height;
setHeight(Math.max(frontHeight, backHeight, 100));
}
useEffect(setMaxHeight, [flashcard.question, flashcard.answer]);
useEffect(() => {
window.addEventListener("resize", setMaxHeight);
return () => window.removeEventListener("resize", setMaxHeight);
}, []);
return (
<div
className={`card ${flip ? "flip" : ""}`}
style={{ height: height }}
onClick={() => setFlip(!flip)}
>
<div className="front" ref={frontEl}>
<p>{flashcard.question}</p>
</div>
<div className="back" ref={backEl}>
<p>{flashcard.answer}</p>
</div>
</div>
);
}
【问题讨论】:
-
我刚刚看了看,它似乎工作正常。我看不到您提到的任何闪烁/故障问题。你用的是什么浏览器?
-
我使用的是 Google Chrome:版本 96.0.4664.110(官方构建)(x86_64)
-
这是你的 css 的问题,有一个叫做 fit-content 的 css 属性,试着把它放在你卡片的 css 的某个地方,它应该可以解决问题。
标签: javascript html css reactjs