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
Like the previous answers, you would use
overflow:hidden
to disable the scrollbars on the body/div.Then you'd bind the
mousewheel
event to a function that would change thescrollTop
of the div to emulate scrolling.For arrow keys, you would bind the
keydown
event to recognize an arrow key, and then changescrollTop
andscrollLeft
of the div as appropriate to emulate scrolling. (Note: you usekeydown
instead ofkeypress
since IE doesn't recognizekeypress
for arrow keys.)Edit: I couldn't get FF/Chrome to recognize
keydown
on a div, but it works in IE8. Depending on what you needed this for, you can set akeydown
listener on thedocument
to scroll the div. (Check out the keyCode reference as an example.)For example, scrolling with the mouse wheel (using jQuery and a mousewheel plugin):
(This is a quick mockup, you'd have to adjust the numbers since for me, this scrolls a bit slowly.)
keyCode reference
mousewheel plugin
keydown, keypress @ quirksmode
Update 12/19/2012:
The updated location of the mousewheel plugin is at: https://github.com/brandonaaron/jquery-mousewheel