隐藏scrollbar - Hide scrollbar

本文介绍两种隐藏网页滚动条的方法:一是使用CSS属性scrollbar-width,但兼容性有限;二是利用双层div布局,通过控制内外层div的overflow属性及尺寸差异实现滚动效果,同时隐藏滚动条。

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

 本文主要讲解如何隐藏scrollbar。

方法一:scrollbar-width (兼容性不好)

  利用单一css属性 scrollbar-width设置scrollbar的宽度。

scrollbar-width: auto | thin | none | <length>;
  • auto is the default value and will render the standard scrollbars for the user agent.
  • thin will tell the user agent to use thinner scrollbars, when applicable.
  • none will hide the scrollbar completely, without affecting the element's scrollability.
  • <length> is being debated, but (if added) would be a maximum width of the scrollbar.

  然而,这个css属性存在兼容性问题,它只在一下浏览器版本下生效,相信以后兼容性会更好:

方法二: 双层div,外层overflow: hidden, 内层overflow:auto/scroll/overlay,同时 内层的高度/宽度 - 外层的高度/宽度 >= 滚动条的宽度 (可以将内层的高度/宽度设置很大)。

这里拿横向滚动为例:

code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .parent {
            width: 100px;
            height: 20px;
            border: 1px solid red;
            overflow: hidden;
        }
        .children {
            height: 100px;
            overflow-x: auto;
        }
        .children > div {
            white-space: nowrap;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="children">
            <div>First Line, this is first line</div>
        </div>
    </div>
</body>
</html>

 

转载于:https://www.cnblogs.com/juliazhang/p/10524278.html

要在 layui 中实现横向滚动条,可以使用 layui 自带的滚动条组件,然后通过一些 CSS 样式进行调整。 首先,需要引入 layui 的滚动条组件: ```html <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/layui@2.5.6/dist/css/layui.css"> <script src="https://cdn.jsdelivr.net/npm/layui@2.5.6/dist/layui.js"></script> ``` 然后,在需要添加横向滚动条的元素上加上 `layui-hide` 类,以隐藏原生滚动条,并添加 `layui-scrollbar` 类,以启用 layui 的滚动条。 ```html <div class="layui-scrollbar layui-hide"> <!-- 横向滚动条内容 --> </div> ``` 接下来,可以通过 CSS 样式对滚动条进行一些调整。以下是一个简单的例子: ```css .layui-scrollbar { overflow-x: auto; overflow-y: hidden; white-space: nowrap; height: 100px; width: 500px; } .layui-scrollbar::-webkit-scrollbar { height: 10px; width: 10px; } .layui-scrollbar::-webkit-scrollbar-thumb { background-color: #999; border-radius: 5px; } .layui-scrollbar::-webkit-scrollbar-track { background-color: #eee; } ``` 在上面的例子中,`overflow-x: auto` 和 `overflow-y: hidden` 用于控制元素的横向滚动和纵向滚动;`white-space: nowrap` 用于让内容在同一行上显示;`height` 和 `width` 分别用于控制滚动条的高度和宽度。 接着,通过 `-webkit-scrollbar` 相关的 CSS 样式对滚动条进行调整。`::-webkit-scrollbar` 用于控制整个滚动条的样式,`::-webkit-scrollbar-thumb` 用于控制滚动条拖动块的样式,`::-webkit-scrollbar-track` 用于控制滚动条轨道的样式。 最后,初始化 layui 滚动条组件: ```javascript layui.use('element', function(){ var element = layui.element; element.init(); }); ``` 这样就可以在 layui 中实现横向滚动条了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值