I want to hide any scrollbars from my div
elements and my whole body
, but still let the user scroll with the mouse wheel or arrow keys. How can this be achieved with raw JavaScript or jQuery? Any ideas?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
- jQuery add and remove delay
What about a purely CSS solution? I've tested this and it works fine. However, I would recommend Solution 3 as isn't hacky, it's supported by all browsers with JS and it's very simple.
Solution 1 (cross browser but more hacky)
Solution 2 (IE and Chrome only)
Just add the
nobars
class to any element you want to hide the scrollbars on.The Firefox
overflow: -moz-scrollbars-none
is, according to MDN, obsolete. It used to work but now hides the overflow and disables scrolling if used.Solution 3 (cross browser javascript)
Perfect Scrollbar doesn't require jQuery (although it can utilise jQuery if installed) and has a demo available here. The components can be styled with css such as in the following example:
Here is a complete example including the implementation of Perfect Scrollbar:
You dont have to use jquery or js to make this. Its more performant with simple webkit.
Just add the code below to your css file.
Caution ! This will disable all the scrollbar so be sure to put it in a specific class or id if you just want one to be hidden.
To get this working for me, I used this CSS:
But I had problems using
$(this).scrollTop()
, so I bound to my #id, but adjusted the scrollTop of window. Also, my smooth scrolling mouse would fire lots of 1 or -1 deltas, so I multiplied that by 20.As Baldráni said above
Or you can do
(posted for other people that stumble on this from google search!)
Well, perhaps not the most intuitive in my opinion, but I can imagine you being able to make it a decent experience, give this a try.
make sure the parent object has a
height
andwidth
, and displays asblock
I much prefer SamGoody's answer provided to a duplicate of this question. It leaves native scrolling effects intact, instead of trying to manually re-implement for a few particular input devices:
See the original comment for a fleshed-out example. You may want to use JavaScript to determine the actual size of scrollbars rather than assuming they are always 8px wide as his example does.