JavaScript 中使用溢位時頁面內容的左右邊緣應該如何處理?


如果內容出現溢位,可以使用 overflowX 屬性來解決左右邊緣的問題並設定捲軸。新增捲軸可以讓訪問者輕鬆閱讀全部內容。

示例

你可以嘗試執行以下程式碼,瞭解如何使用 JavaScript 處理頁面內容的左右邊緣的問題 −

線上演示

<!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>

更新於: 2020 年 6 月 23 日

53 次瀏覽

開啟你的事業

完成課程即可獲得認證

開始學習
廣告
© . All rights reserved.