首页 > 解决方案 > html ::-webkit-scrollbar 未通过 jquery 应用

问题描述

我可以通过以下方式隐藏 Chrome 中的滚动条:

# app.css loaded in <head>
html ::-webkit-scrollbar {
  display: none;
}

现在我想手动隐藏滚动条,所以我尝试使用 jQuery 运行以下命令:

$("html ::-webkit-scrollbar").css("display", "none")

但是,滚动条在滚动期间保持可见。

如何手动隐藏/显示滚动条?

标签: jquerycsscss-selectorsscrollbarpseudo-element

解决方案


根据这个答案:webkit scrollbar using jQuery.css() method你不能使用 JQuery 处理伪元素,但你可以使用一个类来“破解”它:

$('html').addClass('hide-scrolling')
.hide-scrolling::-webkit-scrollbar {
       width: 30px;
}
<p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p>

<script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>


推荐阅读