如何固定 div 的高度,以便动态向其添加数据不会改变其高度?



我正在尝试制作一个简单的计算器,除了无法固定它的屏幕高度外,它几乎完成了。当我输入数据时,计算器屏幕的高度会发生变化。要重现问题,您可以尝试添加1+2。我一直在尝试添加最小高度和最大高度,但没有成功。我可以用js来固定它的高度,但我想要一个纯CSS的解决方案。这是代码:

let screen = document.getElementById("screen");
let firstArg = document.querySelector(".firstArg");
let operator = document.querySelector(".operator");
let secondArg = document.querySelector(".secondArg");
let numberBtns = document.querySelectorAll(".data-number");
let acBtn = document.querySelector(".ac");
let clearBtn = document.querySelector(".clear");
let backBtn = document.querySelector(".back");
let decimalBtn = document.querySelector(".decimal");
let equalBtn = document.querySelector(".equal");
let operationBtn = document.querySelectorAll(".operation");
let regex = /[+-/*]/;
let digits = /[0-9]/;
numberBtns.forEach(n => n.addEventListener("click", e => {
if (firstArg.innerHTML === "0") {
firstArg.innerHTML = "";
}
if (operator.textContent === "" && firstArg.clientWidth + 40 < screen.offsetWidth) {
firstArg.append(n.textContent);
} else if (operator.textContent && secondArg.clientWidth + 40 < screen.offsetWidth) {
secondArg.append(n.textContent);
}
}));
decimalBtn.addEventListener("click", e => {
if (!firstArg.textContent.includes(".") && operator.textContent === "") {
firstArg.append(decimalBtn.textContent);
} else if (!secondArg.textContent.includes(".") && operator.textContent) {
secondArg.append(decimalBtn.textContent);
}
});
operationBtn.forEach(n => n.addEventListener("click", e => {
if (!regex.test(operator.textContent)) {
operator.append(n.textContent);
firstArg.append(" ");
firstArg.append(operator.textContent);
}
}))
equalBtn.addEventListener("click", e => {
let firstParam = firstArg.textContent.split(" ")[0];
let oper = operator.textContent;
let secondParam = secondArg.textContent;
if (secondParam == "") return;
solve(firstParam, oper, secondParam);
})
document.addEventListener("keydown", e => {
if (e.key == "+") {
if (!regex.test(operator.textContent)) {
operator.append(e.key);
firstArg.append(" ");
firstArg.append(operator.textContent);
}
}
if (e.key == "/") {
if (!regex.test(operator.textContent)) {
operator.append(e.key);
firstArg.append(" ");
firstArg.append(operator.textContent);
}
}
if (e.key == "-") {
if (!regex.test(operator.textContent)) {
operator.append(e.key);
firstArg.append(" ");
firstArg.append(operator.textContent);
}
}
if (e.key == "*") {
if (!regex.test(operator.textContent)) {
operator.append(e.key);
firstArg.append(" ");
firstArg.append(operator.textContent);
}
}
if (digits.test(e.key)) {
if (firstArg.innerHTML === "0") {
firstArg.innerHTML = "";
}
if (operator.textContent === "" && firstArg.clientWidth + 40 < screen.offsetWidth) {
firstArg.append(e.key);
} else if (operator.textContent && secondArg.clientWidth + 40 < screen.offsetWidth) {
secondArg.append(e.key);
}
}
if (e.code === "Enter") {
let firstParam = firstArg.textContent.split(" ")[0];
let oper = operator.textContent;
let secondParam = secondArg.textContent;
if (secondParam == "") return;
solve(firstParam, oper, secondParam);
}
})
function solve(first, oper, second) {
switch (oper) {
case "-":
firstArg.innerHTML = `${((Number(first) * 10 - Number(second) * 10) / 10).toFixed(1)}`;
operator.textContent = "";
secondArg.textContent = "";
break;
case "+":
firstArg.innerHTML = `${((Number(first) * 10 + Number(second) * 10) / 10).toFixed(1)}`;
operator.textContent = "";
secondArg.textContent = "";
break;
case "/":
firstArg.innerHTML = `${((Number(first) * 10 / Number(second) * 10) / 10).toFixed(1)}`;
operator.textContent = "";
secondArg.textContent = "";
break;
case "*":
firstArg.innerHTML = `${((Number(first) * 10 * Number(second) * 10) / 10).toFixed(1)}`;
operator.textContent = "";
secondArg.textContent = "";
break;
}
}
backBtn.addEventListener("click", e => {
if ((operator.textContent || firstArg.textContent.length > 1) && secondArg.textContent == "") {
operator.innerHTML = "";
let remaining = firstArg.textContent.slice(0, firstArg.textContent.length - 1).trim();
firstArg.innerHTML = "";
firstArg.innerHTML = remaining;
} else if (firstArg.textContent.length === 1 && secondArg.textContent == "") {
firstArg.innerHTML = "0";
} else if (secondArg.textContent) {
let remaining = secondArg.textContent.slice(0, secondArg.textContent.length - 1).trim();
secondArg.innerHTML = "";
secondArg.innerHTML = remaining;
}
})
window.onload = function() {
firstArg.innerHTML = 0;
}
acBtn.addEventListener("click", function() {
firstArg.innerHTML = 0;
secondArg.innerHTML = "";
operator.textContent = "";
})
.claculator {
display: grid;
grid-template-rows: 70px 1fr;
grid-gap: 35px 0;
margin: 0 auto;
border: 2px solid black;
width: 350px;
height: 370px;
border-radius: 27px;
padding: 12px 9px;
align-items: center;
background-color: #191a1c;
}
#screen {
background-color: #ffffff;
border-radius: 5px;
min-height: 70px;
min-width: 200px;
margin-top: 20px;
}
#funBtns {
display: grid;
grid-template-columns: repeat(4, 50px);
grid-template-rows: repeat(5, 30px);
grid-gap: 20px 40px;
justify-content: center;
}
button {
background-color: #191a1c;
color: white;
border: none;
border: 1px solid white;
border-radius: 5px;
width: 100%;
}
button:hover {
transform: translateY(-5px);
transition: 200ms ease-out;
background-color: white;
color: black;
}
.button-plus {
grid-row: span 2;
}
#screen {
display: flex;
flex-direction: column;
align-items: flex-end;
row-gap: 25px;
padding-top: 10px;
padding-right: 10px;
}
.firstArg {
float: right;
font-size: 20px;
font-weight: 500;
color: rgb(0, 0, 0);
text-overflow: clip;
}
.secondArg {
float: right;
font-size: 25px;
font-weight: 900;
color: rgb(24, 22, 22);
}
.operator {
display: none;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Calculator</title>
<link rel="stylesheet" href="style.css">
<script src="calculator.js" defer></script>
</head>
<body>
<div class="claculator">
<div id="screen">
<span class="firstArg"></span>
<span class="operator"></span>
<span class="secondArg"></span>
</div>
<div id="funBtns">
<button class="button ac">AC</button>
<button class="button clear">Clear</button>
<button class="button back">Back</button>
<button class="operation">/</button>
<button class="data-number">1</button>
<button class="data-number">2</button>
<button class="data-number">3</button>
<button class="operation">*</button>
<button class="data-number">4</button>
<button class="data-number">5</button>
<button class="data-number">6</button>
<button class="operation">-</button>
<button class="data-number">7</button>
<button class="data-number">8</button>
<button class="data-number">9</button>
<button class="operation button-plus">+</button>
<button class="button decimal">.</button>
<button class="data-number">0</button>
<button class="button equal">=</button>
</div>
</div>
</body>
</html>

将字体大小更改为1 rem

.secondArg {
float: right;
font-size: 1rem;
font-weight: 900;
color: rgb(24, 22, 22)}

使#screen的字体大小等于1rem
,然后将高度设置为1rem(如果希望屏幕高一行(或2rem(高两行(
1rem=:以像素为单位的根字体大小
由于您添加了填充(显然有两个#屏幕(,因此也可以根据rem-to-height添加填充高度
总屏幕高度=高度(以雷姆计(+填充(以雷米计(
雷姆到像素转换链接:http://www.standardista.com/px-to-rem-conversion-if-root-font-size-is-16px/.

尝试将max-height的css值设置为div的class或id。

最新更新