Home > Web Front-end > CSS Tutorial > How to Create Synchronized Top and Bottom Horizontal Scrollbars for a Table Using Only HTML and CSS?

How to Create Synchronized Top and Bottom Horizontal Scrollbars for a Table Using Only HTML and CSS?

Mary-Kate Olsen
Release: 2024-12-19 10:39:12
Original
132 people have browsed it

How to Create Synchronized Top and Bottom Horizontal Scrollbars for a Table Using Only HTML and CSS?

How to Create a Horizontal Scrollbar on Both the Top and Bottom of a Table with Only HTML and CSS

Problem:

A user wants to add horizontal scrollbars to the top and bottom of a large table without using any JavaScript.

Solution:

It is possible to simulate a second horizontal scrollbar on top of the table by using CSS to create a "dummy" div above the table that has horizontal scrolling and a height just high enough for a scrollbar. Event handlers for the "scroll" event can then be attached to the dummy element and the actual table, synchronizing the scrolling of both elements when either scrollbar is moved.

Code Snippet:

HTML:

<div>
Copy after login

CSS:

.wrapper1, .wrapper2 {
  width: 300px;
  overflow-x: scroll;
  overflow-y:hidden;
}

.wrapper1 {height: 20px; }
.wrapper2 {height: 200px; }

.div1 {
  width:1000px;
  height: 20px;
}

.div2 {
  width:1000px;
  height: 200px;
  background-color: #88FF88;
  overflow: auto;
}
Copy after login

JavaScript:

$(function(){
  $(".wrapper1").scroll(function(){
    $(".wrapper2").scrollLeft($(".wrapper1").scrollLeft());
  });
  $(".wrapper2").scroll(function(){
    $(".wrapper1").scrollLeft($(".wrapper2").scrollLeft());
  });
});
Copy after login

The dummy div (.div1) will appear as a second horizontal scrollbar above the actual table element (.div2). By syncing the scrolling of both elements, the user can scroll the table content from either the top or bottom scrollbar.

The above is the detailed content of How to Create Synchronized Top and Bottom Horizontal Scrollbars for a Table Using Only HTML and CSS?. 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