CSS实现隐藏滚动条同时可以滚动内容

1.通过定位把滚动条隐藏了起来

div1设置绝对对位,向右移动了17个像素,刚好等于滚动条的宽度,
再利用div2把溢出的17像素隐藏起来。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style> .div1{
          width: 200px;
          height: 200px;
          background: red;
          position: relative;   
          overflow: hidden;
          margin: 20px auto;   
        }
        .div2{
            background: yellow;
            top: 0px;
            left: 0px;
            bottom: 0px;
            right: -17px;
            position:absolute;
            overflow-y: scroll;   
        }
        p{
            list-style: none;
            height: 30px;
            background: green;
            margin-top: 20px;
            color:#fff;
        } </style>
</head>
<body>
    <div class="div1">
        <div class="div2">
          <p>1</p>
          <p>2</p>
          <p>3</p>
          <p>4</p>
          <p>5</p>
          <p>6</p>
        </div>
    </div>
</body>
</html>

评论

暂无评论