Home > Web Front-end > CSS Tutorial > How Can I Hide Scrollbars While Keeping Scrolling Functionality?

How Can I Hide Scrollbars While Keeping Scrolling Functionality?

Barbara Streisand
Release: 2024-12-23 17:09:10
Original
424 people have browsed it

How Can I Hide Scrollbars While Keeping Scrolling Functionality?

Hiding Scrollbars While Maintaining Scrollability

In certain scenarios, you may want to conceal scrollbars from web elements or the entire webpage while still permitting scrolling functionality via the mouse wheel or arrow keys. Here's how you can achieve this:

Disable Scrollbars Using CSS

To eliminate scrollbars, apply the following CSS property:

overflow: hidden;
Copy after login

This will hide scrollbars for the targeted element, effectively preventing them from appearing. However, overflowed content will still be accessible.

Emulate Scrolling with JavaScript

To restore scrolling capabilities, you must bind event listeners to mouse wheel and keydown events.

Mouse Wheel Scrolling

Using JavaScript or the jQuery plugin shown below, you can emulate scrolling by modifying the element's scrollTop property:

$("#example").bind("mousewheel", function(ev, delta) {
  var scrollTop = $(this).scrollTop();
  $(this).scrollTop(scrollTop - Math.round(delta));
});
Copy after login

Arrow Key Scrolling

To support arrow key scrolling, bind an event listener to the keydown event:

$(document).keydown(function(event) {
  const{ scrollTop, scrollLeft } = $("#example");
  switch (event.keyCode) {
    case 37: // Left arrow
      $("#example").scrollLeft(scrollLeft - 10);
      break;
    case 38: // Up arrow
      $("#example").scrollTop(scrollTop - 10);
      break;
    case 39: // Right arrow
      $("#example").scrollLeft(scrollLeft + 10);
      break;
    case 40: // Down arrow
      $("#example").scrollTop(scrollTop + 10);
      break;
  }
});
Copy after login

By implementing these techniques, you can effectively disable scrollbars while preserving scrolling functionality using the mouse wheel or arrow keys.

The above is the detailed content of How Can I Hide Scrollbars While Keeping Scrolling Functionality?. 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