如何處理超出元素框並利用 JavaScript 渲染的內容?


如果內容已超出元素框並渲染,使用 overflow 屬相新增一個捲軸。這將緩解訪問者閱讀全部內容的過程。

示例

您可以嘗試執行以下程式碼,以瞭解如何在 JavaScript 中使用 overflow 屬性 −

線上演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         #box {
            width: 450px;
            height: 150px;
            background-color: orange;
            border: 3px solid red;
            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.overflow = "scroll";
         }
      </script>
   </body>
</html>

更新於: 23-6 月-2020

208 次瀏覽

開啟您的職業生涯

完成課程即可獲取認證

開始
廣告
© . All rights reserved.