Переполнение HTML без прокрутки

#javascript #html #css #scroll #overflow

#javascript #HTML #css #прокрутка #переполнение

Вопрос:

Я создаю 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@300amp;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>  

Когда я нажимаю больше символов, чем может обработать интервал ответа, он становится больше, а не прокручивается.

Комментарии:

1. Использовать таблицы для позиционирования элементов на веб-странице — плохая практика. С этим у вас возникнет множество проблем. Лучше используйте css grid.

Ответ №1:

Для 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@300amp;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>