使用 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-06-23

53 次浏览

助力你的 职业

完成课程以获得认证

开始
广告
© . All rights reserved.