问题描述
我正在制作HTML计算器,并且正在使用表格来对齐所有内容。但是,即使有overflow-x:scroll属性,当键入许多字符时,计算器也会变大而不是滚动。到目前为止,这是我的代码:
var problem = ""
var answer = document.getElementById("responseSpan")
var chars = ["1","2","3","4","5","6","7","8","9","0","."]
function hit(key) {
//console.log(key)
if (chars.includes(key)) {
problem = problem + key
}
answer.innerText = problem
}
body {
background-color: #e0e0e0;
}
table {
width: 25%;
border-spacing: 0px;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
position: absolute;
}
td {
border: 1px solid gray;
padding: 1px;
text-align: right;
}
.clickable {
cursor: pointer;
text-align: center;
color: black;
padding-top: 5%;
padding-bottom: 5%;
font-size: 1.5em;
user-select: none;
-moz-user-select: none;
-khtml-user-select: none;
-webkit-user-select: none;
-o-user-select: none;
}
.clickable:hover {
filter: brightness(85%);
}
#response {
color: white;
background-color: gray;
font-size: 70px;
padding-top: 2%;
padding-bottom: 2%;
}
#responseSpan {
overflow-x: scroll;
}
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8">
<title>Calculator</title>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans+Condensed:wght@300&display=swap" rel="stylesheet">
</head>
<body>
<table>
<tr>
<td colspan="4" id="response"><span id="responseSpan">0</span></td>
</tr>
<tr>
<td class="clickable" style="background-color:darkgray;" onclick="hit('AC')">AC</td>
<td class="clickable" style="background-color:darkgray;" onclick="hit('+/-')">+/-</td>
<td class="clickable" style="background-color:darkgray;" onclick="hit('%')">%</td>
<td class="clickable" style="background-color:orange;" onclick="hit('/')">➗</td>
</tr>
<tr>
<td class="clickable" style="background-color:lightgray;" onclick="hit('7')">7</td>
<td class="clickable" style="background-color:lightgray;" onclick="hit('8')">8</td>
<td class="clickable" style="background-color:lightgray;" onclick="hit('9')">9</td>
<td class="clickable" style="background-color:orange;" onclick="hit('x')">x</td>
</tr>
<tr>
<td class="clickable" style="background-color:lightgray;" onclick="hit('4')">4</td>
<td class="clickable" style="background-color:lightgray;" onclick="hit('5')">5</td>
<td class="clickable" style="background-color:lightgray;" onclick="hit('6')">6</td>
<td class="clickable" style="background-color:orange;" onclick="hit('-')">-</td>
</tr>
<tr>
<td class="clickable" style="background-color:lightgray;" onclick="hit('1')">1</td>
<td class="clickable" style="background-color:lightgray;" onclick="hit('2')">2</td>
<td class="clickable" style="background-color:lightgray;" onclick="hit('3')">3</td>
<td class="clickable" style="background-color:orange;" onclick="hit('+')">+</td>
</tr>
<tr>
<td colspan="2" class="clickable" style="background-color:lightgray;" onclick="hit('0')">0</td>
<td class="clickable" style="background-color:lightgray;" onclick="hit('.')">.</td>
<td class="clickable" style="background-color:orange;" onclick="hit('=')">=</td>
</tr>
</table>
</body>
</html>
每当我按下超出响应范围的字符时,它就会变大而不是滚动。
解决方法
要使overflow-x: scroll
正常工作,您的span元素应显示inline-block
(或block
)以及定义的宽度,例如:
#responseSpan {
overflow-x: scroll;
display: inline-block;
width: 25vw;
}
演示:
var problem = ""
var answer = document.getElementById("responseSpan")
var chars = ["1","2","3","4","5","6","7","8","9","0","."]
function hit(key) {
//console.log(key)
if (chars.includes(key)) {
problem = problem + key
}
answer.innerText = problem
}
body {
background-color: #e0e0e0;
}
table {
width: 25%;
border-spacing: 0px;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
position: absolute;
}
td {
border: 1px solid gray;
padding: 1px;
text-align: right;
}
.clickable {
cursor: pointer;
text-align: center;
color: black;
padding-top: 5%;
padding-bottom: 5%;
font-size: 1.5em;
user-select: none;
-moz-user-select: none;
-khtml-user-select: none;
-webkit-user-select: none;
-o-user-select: none;
}
.clickable:hover {
filter: brightness(85%);
}
#response {
color: white;
background-color: gray;
font-size: 70px;
padding-top: 2%;
padding-bottom: 2%;
}
/* Changes are here */
#responseSpan {
overflow-x: scroll;
display: inline-block;
width: 25vw;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Calculator</title>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans+Condensed:wght@300&display=swap" rel="stylesheet">
</head>
<body>
<table>
<tr>
<td colspan="4" id="response"><span id="responseSpan">0</span></td>
</tr>
<tr>
<td class="clickable" style="background-color:darkgray;" onclick="hit('AC')">AC</td>
<td class="clickable" style="background-color:darkgray;" onclick="hit('+/-')">+/-</td>
<td class="clickable" style="background-color:darkgray;" onclick="hit('%')">%</td>
<td class="clickable" style="background-color:orange;" onclick="hit('/')">➗</td>
</tr>
<tr>
<td class="clickable" style="background-color:lightgray;" onclick="hit('7')">7</td>
<td class="clickable" style="background-color:lightgray;" onclick="hit('8')">8</td>
<td class="clickable" style="background-color:lightgray;" onclick="hit('9')">9</td>
<td class="clickable" style="background-color:orange;" onclick="hit('x')">x</td>
</tr>
<tr>
<td class="clickable" style="background-color:lightgray;" onclick="hit('4')">4</td>
<td class="clickable" style="background-color:lightgray;" onclick="hit('5')">5</td>
<td class="clickable" style="background-color:lightgray;" onclick="hit('6')">6</td>
<td class="clickable" style="background-color:orange;" onclick="hit('-')">-</td>
</tr>
<tr>
<td class="clickable" style="background-color:lightgray;" onclick="hit('1')">1</td>
<td class="clickable" style="background-color:lightgray;" onclick="hit('2')">2</td>
<td class="clickable" style="background-color:lightgray;" onclick="hit('3')">3</td>
<td class="clickable" style="background-color:orange;" onclick="hit('+')">+</td>
</tr>
<tr>
<td colspan="2" class="clickable" style="background-color:lightgray;" onclick="hit('0')">0</td>
<td class="clickable" style="background-color:lightgray;" onclick="hit('.')">.</td>
<td class="clickable" style="background-color:orange;" onclick="hit('=')">=</td>
</tr>
</table>
</body>
</html>