html ::-webkit-scrollbar不能通过jquery应用

问题描述 投票:-1回答:1

我可以使用以下方法在Chrome中隐藏滚动条:

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

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

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

但是,滚动条在滚动过程中保持可见。

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

jquery css css-selectors scrollbar pseudo-element
1个回答
0
投票

根据此答案: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>
© www.soinside.com 2019 - 2024. All rights reserved.