div--可滚动但不显示滚动条

本文展示了如何使用CSS让div元素可滚动却不显示滚动条,通过代码示例帮助理解实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>可滚动但不显示滚动条的div</title>
  </head>
  <script>
    function getData() {
      let arr = [];
      for (let i = 0; i < 10; i++) {
        let string = "";
        let j = i;
        while (j >= 0) {
          string += i;
          console.log("string", string);
          j--;
        }
        arr.push(string);
      }
      arr.forEach((item) => {
        let node = document.createElement("LI");
        let textnode = document.createTextNode(item);
        node.appendChild(textnode);
        document.getElementById("showList").appendChild(node);
      });

      arr.forEach((item) => {
        let node = document.createElement("LI");
        let textnode = document.createTextNode(item);
        node.appendChild(textnode);
        document.getElementById("showList01").appendChild(node);
      });
    }
  </script>
  <style>
    .container {
      display: flex;
      height: 100vh;
      align-items: center;
      justify-content: center;
    }
    .relative-box {
      position: relative;
      height: 200px;
      width: 170px;
      background: #ff9800;
      overflow: hidden;
      color: #fff;
    }
    .absolute-box {
      position: absolute;
      height: 200px;
      width: 170px;
      overflow: hidden;
      left: -20px;
    }
    .content-box {
      position: absolute;
      height: 200px;
      width: 170px;
      left: 20px;
      background: #ff9800;
      overflow-y: scroll;
    }
    .scroll-box {
      height: 200px;
      width: 170px;
      overflow: auto;
      color: #ff9800;
    }
    .box {
      display: flex;
      border: 1px solid #ff9800;
    }
  </style>
  <body onload="getData()">
    <div class="container">
      <div class="box">
        <div class="relative-box">
          <div class="absolute-box">
            <div class="content-box">
              <ul id="showList">
                <h2>可滚动不显示滚动条</h2>
              </ul>
            </div>
          </div>
        </div>
        <div class="scroll-box">
          <ul id="showList01">
            <h2>滚动显示滚动条</h2>
          </ul>
        </div>
      </div>
    </div>
  </body>
</html>

效果展示(静态图片): 

 

查看效果:复制代码到html文件中即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值