Home > Web Front-end > CSS Tutorial > How to Hide Scrollbars While Still Allowing Mouse and Keyboard Scrolling?

How to Hide Scrollbars While Still Allowing Mouse and Keyboard Scrolling?

Susan Sarandon
Release: 2024-11-09 10:22:02
Original
705 people have browsed it

How to Hide Scrollbars While Still Allowing Mouse and Keyboard Scrolling?

Hiding Scrollbars while Maintaining Mouse/Keyboard Scrolling

This question has been marked as a duplicate, but the original thread did not adequately address the specific issue of hiding the scrollbar while still enabling scrolling with the mouse or keyboard.

Original Question:

Can I hide the scrollbar while still allowing scrolling with the mouse or keyboard?

CSS Overflow: Hidden Limitation:

The CSS property overflow: hidden can be used to hide the scrollbar, but it also disables scrolling functionality altogether.

jQuery Solution (Original):

The original thread proposed a jQuery solution that dynamically measures the textarea's width without the scrollbar and sets the wrapper div's width accordingly. This creates the illusion of a scrollable div without a visible scrollbar.

// get the width of the textarea minus scrollbar
var textareaWidth = document.getElementById("textarea").scrollWidth;

// width of our wrapper equals width of the inner part of the textarea
document.getElementById("wrapper").style.width = textareaWidth + "px";
Copy after login

JavaScript Solution (Without jQuery):

Alternatively, the same principle can be applied without jQuery:

document.getElementById("wrapper").style.overflow = "hidden";

// get the width of the textarea minus scrollbar
var textareaWidth = document.getElementById("textarea").scrollWidth;

// width of our wrapper equals width of the inner part of the textarea
document.getElementById("wrapper").style.width = textareaWidth + "px";
Copy after login

Update:

The same principle can be used to create scrollable divs without scrollbars using CSS and JavaScript.

The above is the detailed content of How to Hide Scrollbars While Still Allowing Mouse and Keyboard Scrolling?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template