Is there a way to implement a floating horizontal scrollbar?

One of the challenges I face with my website is a large table that extends far below the fold, making it inconvenient to access the horizontal scroll bar. Is there a way to keep the horizontal scroll bar floating at the bottom of the window for easier access without having to scroll all the way down?

Answer №1

Unfortunately, I am unable to post the answer due to my low reputation.

You might find the jquery.ba-floatingscrollbar.js plugin helpful in your situation.

Here is a useful GitHub link along with a JSFiddle example for the JavaScript code provided.

/*!
 * jQuery Floating Scrollbar - v0.4 - 02/28/2011
 * http://benalman.com/
 * 
 * Copyright (c) 2011 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */

(function($){
  // Some reused jQuery objects.
  var win = $(this),
      html = $('html'),
      elems = $([]),
      current,
      previous,
      scroller = $('<div id="floating-scrollbar"><div/></div>'),
      scrollerInner = scroller.children();

  scroller
    .hide()
    .css({
      position: 'fixed',
      bottom: 0,
      height: '30px',
      overflowX: 'auto',
      overflowY: 'hidden'
    })
    .scroll(function() {
      current && current.scrollLeft(scroller.scrollLeft())
    });

  scrollerInner.css({
    border: '1px solid #fff',
    opacity: 0.01
  });

  $.fn.floatingScrollbar = function( state ) {
    if ( state === false ) {
      elems = elems.not(this);
      this.unbind('scroll', scrollCurrent);
      if ( !elems.length ) {
        scroller.detach();
        win.unbind('resize scroll', update);
      }
    } else if ( this.length ) {
      if ( !elems.length ) {
        win.resize(update).scroll(update);
      }
      elems = elems.add(this);
    }

    update();
    return this;
  };

  $.floatingScrollbarUpdate = update;

  function setState( state ) {
    scroller.toggle(!!state);
  }

  function scrollCurrent() {
    current && scroller.scrollLeft(current.scrollLeft())
  }

  function update() {
    previous = current;
    current = null;

    elems.each(function(){
      var elem = $(this),
          top = elem.offset().top,
          bottom = top + elem.height(),
          viewportBottom = win.scrollTop() + win.height(),
          topOffset = 30;

      if ( top + topOffset < viewportBottom && bottom > viewportBottom ) {
        current = elem;
        return false;
      }
    });

    if ( !current ) { setState(); return; }

    var scroll = current.scrollLeft(),
        scrollMax = current.scrollLeft(90019001).scrollLeft(),
        widthOuter = current.innerWidth(),
        widthInner = widthOuter + scrollMax;

    current.scrollLeft(scroll);

    if ( widthInner <= widthOuter ) { setState(); return; }

    setState(true);

    if ( !previous || previous[0] !== current[0] ) {
      previous && previous.unbind('scroll', scrollCurrent);
      current.scroll(scrollCurrent).after(scroller);
    }

    scroller
      .css({
        left: current.offset().left - win.scrollLeft(),
        width: widthOuter
      })
      .scrollLeft(scroll);

    scrollerInner.width(widthInner);
  }

})(jQuery);

$(function() {
  $('.sh .highlight, .sample').floatingScrollbar();
});

Answer №2

If you're looking for a solution to floating scroll in jQuery, check out the floatingscroll plugin here.

Here's an example of how to use it:

$(document).ready(function () {
    $(".spacious-container").floatingScroll();
});

You can also see a demo of the plugin here.

The same developer has also created a version of the plugin called handy-scroll here, which does not have a dependency on jQuery.

Answer №3

S. Thompson's response is truly exceptional. However, being a beginner, it took me 4 hours to get it to work properly. In order to help others save time, remember to add the following style to your div:

.wide-wrapper {
    overflow: auto;
    width: 100%;
}

Then simply execute the following code:

$(document).ready(function () {
    $(".wide-wrapper").floatingScroll();
});

Important: The attachScroll() function is outdated and will be removed in upcoming versions.

You can find more information about the plugin here (floating-scroll)

Answer №4

Place the table inside a div. Adjust the height of the div, and apply overflow: auto to it.

Example: http://jsfiddle.net/NtQ2M/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing jQuery Ajax to dynamically update PHP session variables

Although this question may have been answered before, I'm struggling to find a solution that fits my specific issue. I am currently developing a website where users can sell their eBay items using the eBay API, which has proven to be quite challenging ...

Instructions for placing the navigation links below the navigation brand

Could you please advise me on how to center the navigation links below the brand logo? I am new to coding and just starting out. Here is a link to a page that demonstrates what I am trying to achieve: In this example, the brand name is "a fork and a pe ...

display the table without providing a preview

Hey everyone, I am currently utilizing vue.js to develop a table and I am looking for a way to add a button that can print the data in the table without displaying a preview dialog. What modifications should I make to my javascript code in vue? Here is an ...

Challenges integrating Jquery with Flask

When attempting to add an HTML element using jQuery, I encountered an exception from Flask: jinja2.exceptions.TemplateSyntaxError: expected token ',', got 'static' The string seems correct, but I'm unsure about what the issue ...

Slow loading time when switching between items in the drop-down menu from the Main Menu

Visiting my website at europebathroom.com, you will notice a horizontal main menu with dropdown functionality. When hovering over a menu item, the corresponding dropdown appears seamlessly. Yet, sometimes quickly touching another menu item unintentionally ...

Tips for aligning content at the bottom center of a design

Could you assist me in positioning the buttons at the bottom center of the horizontal layout as depicted in the image below? Ps: the vertical layout at the top has a dynamic height. My HTML code is as follows: <html> <body> <div class="c ...

Investigating and solving issues in Javascript and JQuery

Let me walk you through the JavaScript process I am working on: When a text is entered in the input bar and submitted, the script dynamically creates a new "div" element and places the text inside it. This new div is then appended to an existing div calle ...

Is it possible to target the sibling of the currently selected or focused element with CSS?

I'm attempting to add button functionality to show and hide errors using CSS. By clicking the CSS button or link, I can target the siblings of the focused element as shown below. This method is only effective in IE8+ browsers. #ShowErrors:focus + a ...

Double firing issue with AJAX Product Filtering upon button click

I am in the process of creating a WordPress online store using an Ajax Filtering Plugin that allows users to filter products by category and other criteria. However, I have noticed that when I click on a category button, the URL is being requested twice, ...

Can you suggest a more effective method for changing the color of buttons?

While the title provides some context, allow me to elaborate further. I've been working on a website for quite some time now. Each section of the website features 3 buttons, each highlighted in a unique color. My question is, is there a more streamlin ...

Tips for accessing the descendants and grandchildren of a list item

Is there a way to display the sub-categories under the sub-categories of SALE using the JSFiddle provided here? Specifically, I'm looking to show additional categories that fall under SALE > BELTS and SALE > ACCESSORIES. The code snippet below c ...

Trouble with Bootstrap popover displaying content

I've been working on implementing the bootstrap popover feature on a website and have had some success. I managed to get the popover to appear when the link is clicked, but unfortunately, I'm not seeing any content in the box other than the title ...

When a condition fails, the default style in the CSS media query does not apply

Whenever the user resizes the browser, my media query for max-width kicks in to make the design responsive. But I'm facing an issue where when the user returns back to the original browser size, the design is still a little messed up and doesn't ...

Ways to eliminate a css class with jquery based on a specific screen dimension

There is a div element that includes a specific class. My goal is to remove this class when the window is resized to fit a mobile view-port. <div class="box"></div> ...

I'm currently facing an issue with toggling the visibility of a div using JavaScript

In my attempt to create a unique custom select menu using html, css, and javascript, I encountered an issue with toggling the display style of the div containing the options when clicking on the button (in this case, an arrow). HTML: <ul class="de ...

Attempting to dynamically add an image to a template snippet

Struggling with inserting images dynamically from database paths in templates. How can I display them when a user clicks the Show More button on an expense page? New to templates and unsure about syntax. Show More Button displayed in the HTML template sho ...

Position the cells in the middle of the bottom row within the table

Indeed, this layout is based on tables. It's a legacy design, so removing the table structure may not be feasible. Nevertheless, I am curious to learn about table alignment, hence posing this somewhat unconventional question. Imagine a 3x3 table with ...

Tips for accessing data from dynamically named inputs within an ng-repeat loop

My objective is to facilitate the process of transferring data from one table row to another. https://i.sstatic.net/2XjSG.png If the information from 2015 remains unchanged in 2016, users should have an efficient method for copying these values into the c ...

Enhance your Vue.js app with seamless page transitions using the

I am facing an issue and need some help. My goal is to create a web application-like interface. However, when I use vue.js transitions, it displays like this: https://i.sstatic.net/U1g6M.gif The transition automatically resizes, repeats, and changes si ...

How to Efficiently Organize OpenAI AI Responses Using TypeScript/JavaScript and CSS

I'm using a Next.js framework to connect to the OpenAI API, and I've integrated it seamlessly with an AI npm package. The functionality is incredible, but I've encountered an issue regarding line breaks in the responses. You can find the AI ...