简体   繁体   English

切换滚动条可见性不适用于Safari(适用于Chrome)

[英]Toggling scrollbar visibility not working on Safari (working on Chrome)

Im trying to implement a behaviour where a scrollbar on a div becomes visible based on a class. 我试图实现一种行为,使div上的滚动条基于类可见。 The div needs to be overflow:auto , i want to control just the visibility of the scrollbar. div需要是overflow:auto ,我只想控制滚动条的可见性。

For that i am using this - 为此,我正在使用这个-

.not-scrolled::-webkit-scrollbar {
    display:none;
}

When the class not-scrolled is added to the div, the scrollbar gets hidden, when the class is removed, the scrollbar becomes visible. not-scrolled的类添加到div后,滚动条将隐藏,而删除该类后,滚动条将变为可见。

You can check the behaviour in the jsfiddle here - https://jsfiddle.net/naman_anand/39g0h1pk/16/ -你可以在这里检查中的jsfiddle行为https://jsfiddle.net/naman_anand/39g0h1pk/16/

This is working exactly as expected in Chrome. 完全可以在Chrome浏览器中正常运行。 But not working in Safari. 但不能在Safari中使用。

In Safari, the scrollbar takes the styles assigned to it at load time, and then does not change, even if a class is added/removed changing those styles. 在Safari中,滚动条采用加载时分配给它的样式,即使添加或删除了更改这些样式的类,该滚动条也不会更改。

Thats why if you open the above fiddle in safari, it will not work. 这就是为什么如果您在野生动物园中打开上述小提琴,它将无法正常工作的原因。 But if you hide the scrollbar in the beginning ( check this fiddle - https://jsfiddle.net/naman_anand/39g0h1pk/18/ ) and try to make it visible later by removing a class, it stays hidden. 但是,如果您在开始时隐藏了滚动条(请检查此小提琴-https: //jsfiddle.net/naman_anand/39g0h1pk/18/ ),然后尝试通过删除一个类使其可见,则它保持隐藏状态。

Any idea why is this happening in Safari, and any way of toggling the visibility of a scrollbar in Safari? 知道为什么在Safari中会发生这种情况,以及在Safari中切换滚动条的可见性的任何方式吗?

How css handles an overflow of contents is all handled by the overflow property. CSS如何处理内容溢出全部由overflow属性处理。 If you want the div to default to see the scroll bar, then set it to scroll . 如果您希望div默认显示滚动条,则将其设置为scroll (You can set it to auto, but then it won't show the scroll bar if the contents don't overflow.) (您可以将其设置为自动,但是如果内容没有溢出,则不会显示滚动条。)

Next, the .not-scrolled class can be added which should update the div to overflow: hidden . 接下来,可以添加.not-scrolled类,该类应将div更新为overflow: hidden (Assuming you don't want the contents to overflow. (假设您不希望内容溢出。

Something like: 就像是:

.default-class {
    overflow: scroll;

    &.not-scrolled {
        overflow: hidden;
    }
}

*Edit: Misinterpreted the original question, as this doesn't keep the ability to scroll while hiding the scroll bar. *编辑:误解了原始问题,因为在隐藏滚动条的同时并不能保持滚动的能力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM