简体   繁体   English

如果不滚动则隐藏滚动条

[英]hide scrollbar if not scrolling

I would like to hide the scrollbar if the the user is not scrolling, meaning if the user scrolls, the scrollbar should appear (only the scrollbar not the scroll track), while if the user does not scroll it should disappear.如果用户没有滚动,我想隐藏滚动条,这意味着如果用户滚动,滚动条应该出现(只有滚动条而不是滚动轨道),而如果用户不滚动它应该消失。 I sort of had that setup for a long time, but than I made some changes to my page and now the page always shows the scrollbar (if there is more content than one page can cover).我有很长一段时间的设置,但是我对我的页面进行了一些更改,现在页面总是显示滚动条(如果内容多于一页可以覆盖)。 Unfortunately I don't know what I did to make this feature go away?不幸的是,我不知道我做了什么让这个功能消失? I played around with overflow in the css, but overflow: hidden just removes all scrolling possibilities.我在 css 中使用了溢出,但是溢出:隐藏只是删除了所有滚动的可能性。 Here is a fiddle which shows my current setup这是一个显示我当前设置的小提琴

https://jsfiddle.net/jsmnsLm7/ (please make the window big, so that you can see all of the features of my navbar setup) https://jsfiddle.net/jsmnsLm7/ (请将窗口调大,以便您可以看到我的导航栏设置的所有功能)

as you can see I use正如你所看到的,我使用

overflow: hidden 

in the body and在身体和

overflow: scroll

in the main.在主要。 thanks for your help carl谢谢你的帮助卡尔

try following css:尝试以下CSS:

overflow:auto;

It worked for me :)它对我有用:)

This will do what you're looking for http://rocha.la/jQuery-slimScroll这将做你正在寻找的http://rocha.la/jQuery-slimScroll

Or you could just show the scrollbar when you hover over the area using CSS only;或者,当您仅使用 CSS 将鼠标悬停在该区域上时,您可以仅显示滚动条; This worked for me;这对我有用;

<style>
#ID {
  overflow-y: hidden;
}
</style>
#ID:hover, #ID:active, #ID:focus {
  overflow-y: auto;
}
</style>

<div class="ID"></div>

There isn't a way to do this outside of a scripting languege as far as I know, but the JavaScript you use for this is super simple.据我所知,在脚本语言之外没有办法做到这一点,但您为此使用的 JavaScript 非常简单。

Start off with a CSS style of:从以下 CSS 样式开始:

#ID {
overflow: hidden
}

Then in your div in the HTML use this command然后在 HTML 中的 div 中使用此命令

<div id="ID" onmouseover="this.style.overflow='scroll'"
onmouseout="this.style.overflow='hidden'"

this will cause your scroll button to appear when the user hovers over the div, but then disappear again when the user hovers away from the div.这将导致您的滚动按钮在用户将鼠标悬停在 div 上时出现,但当用户将鼠标悬停在远离 div 的地方时再次消失。

If you are using bootstrap, it is pretty simple - There is a default Scroll class to which you can apply the style overflow: auto .如果您使用的是引导程序,则非常简单 - 有一个默认的 Scroll 类,您可以将样式overflow: auto应用到该类overflow: auto

<div class="Scroll" style="overflow: auto" >
    .......
</div>

Based on this https://stackoverflow.com/a/40857678/15992537 answer I made this:基于这个https://stackoverflow.com/a/40857678/15992537回答我做了这个:

.categorias::-webkit-scrollbar-thumb {
    height: 6px;
    background: #ff3d1d;
    border-radius: 10px;
    visibility: hidden;
}

.categorias:active::-webkit-scrollbar-thumb {
    visibility: visible;
}

this worked in my case because my div is draggable, so the thumb shows when i drag and move it, but with JS you probably can make it apear based on events like page scroll for example.这在我的情况下有效,因为我的 div 是可拖动的,所以当我拖动和移动它时,拇指会显示,但是使用 JS,您可能可以根据页面滚动等事件使其显示。

use overflow: auto使用overflow: auto

The overflow property has the following values:溢出属性具有以下值:

  • visible - Default.可见- 默认。 The overflow is not clipped.溢出不会被剪裁。 The content renders outside the element's box内容呈现在元素框外
  • hidden - The overflow is clipped, and the rest of the content will be invisible hidden - 溢出被剪裁,其余内容将不可见
  • scroll - The overflow is clipped, and a scrollbar is added to see the rest of the content scroll - 裁剪溢出,并添加滚动条以查看其余内容
  • auto - Similar to scroll, but it adds scrollbars only when necessary auto - 类似于滚动,但仅在必要时添加滚动条

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

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