Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ควรทำอย่างไรกับขอบซ้าย/ขวาของเนื้อหาที่溢出 (Overflow) ด้วย JavaScript

เมื่อเนื้อหา溢出 (overflow) ให้ใช้คุณสมบัติ overflowX เพื่อจัดการขอบแนวนอน ตั้งค่าเป็น scroll จะแสดง scrollbar แนวนอนให้ผู้ใช้เลื่อนอ่านได้

ตัวอย่าง

<!DOCTYPE html>
<html>
  <head>
    <style>
      #box {
        width: 350px;
        height: 150px;
        background-color: orange;
        border: 3px solid red;
        white-space: nowrap;
        margin-left: 20px;
      }
    </style>
  </head>
  <body>
    <p>Click to use overflow property and set scroll.</p>
    <button type="button" onclick="display()">Set Scroll</button>
    <div id="box">
      <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
      <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
      <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
      <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
      <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
      <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
    </div>
    <br><br>
    <script>
      function display() {
        document.getElementById("box").style.overflowX = "scroll";
      }
    </script>
  </body>
</html>