Giter Club home page Giter Club logo

floating-scroll's Introduction

floating-scroll

The Crux of the Matter

The general purpose of the plugin is to provide some lengthy containers on the page with a separate horizontal (or vertical) scrollbar, which does not vanish from sight when the entire page is scrolled. So, the user will always be able to scroll the container even if its own scrollbar is outside the viewport.

Tip

  • floating-scroll is a jQuery plugin. If you are rather looking for a standalone dependency-free module with the similar functionality, please check out the sibling project handy-scroll instead.
  • Developing a Vue-based app? Consider using the vue-handy-scroll component.

Details & API

There is the only public method used to instantiate and control a floating scrollbar — .floatingScroll(). Unless otherwise indicated, the plugin method .floatingScroll() should be called in context of a scrollable container. It takes an optional argument method. The currently supported methods are

  • init (default value) — used to initialize a floating scrollbar widget;
  • update — used t force update of the floating scrollbar parameters (size and position);
  • destroy — removes a scrollbar and all related event handlers;
  • destroyDetached — destroys floating scrollbar instances whose containers were removed from the document (requires a different context, see below).

You may also trigger events update.fscroll and destroy.fscroll on containers with attached floating scrollbar: this does the same as invoking the corresponding methods.

Usage

Inclusion of plugin files

The plugin requires the CSS file jquery.floatingscroll.css to be included on the page (you may also import it in your CSS/LESS files). The plugin’s script file jquery.floatingscroll.min.js may be added on the web page either via a separate <script> element, or it may be loaded by any AMD/CommonJS compatible module loader. This is a jQuery plugin so be sure that this library is added and accessible.

💡 Tip: If you don’t care about supporting old browsers, feel free to use the file jquery.floatingscroll.es6.min.js, which is de facto the same as jquery.floatingscroll.min.js but is written in ES6, and is a bit smaller.

Initialisation

The only thing required to apply floatingScroll to a static container (whose sizes will never change during the session) is a single call of the .floatingScroll() method on the DOM ready event:

$(() => {
    $(".spacious-container").floatingScroll();
});

Starting with v3.1.0, the method init supports passing options. Currently, the only supported parameter is orientation. Default scrollbar orientation is "horizontal" but you can also make a vertical floating scrollbar using options:

$(() => {
    $(".spacious-container").floatingScroll("init", {
        orientation: "vertical"
    });
});

Auto-initialisation

There is another way of initialising the floatingScroll widget without writing a single line of JavaScript code. Just add an attribute data-fl-scrolls to the desired container. As the DOM is ready the plugin will detect all such elements and will do initialisation automatically.

<div class="spacious-container" data-fl-scrolls>
    <!-- Horizontally wide contents -->
</div>

<div class="spacious-container" data-fl-scrolls='{"orientation": "vertical"}'>
    <!-- Horizontally wide contents -->
</div>

Updating scrollbar

If you attach a floating scrollbar to a container whose size and/or content may dynamically change, then you need a way to update the scrollbar each time the container’s sizes change. This can be done by invoking the method update as in the example below.

$(".spacious-container").floatingScroll("init");
$("#refresh-button").click(() => {
    // ... some actions which change the total scroll width of the container ...
    $(".spacious-container").floatingScroll("update");
});

Destroying floating scrollbar

To detach a scrollbar and remove all related event handlers, use the method destroy as follows:

$(".spacious-container").floatingScroll("destroy");

Destroying detached widgets

If your app completely re-renders a large portion of DOM where floating scrollbar widgets were mounted, actual container references are lost, and therefore you cannot destroy the widgets and perform related cleanup using the destroy method. In this case, you may just call the destroyDetached method, and the pluign will find all “zombie” instances and will destroy them for you. Unlike the other methods, this one needs to be called in context of $(window):

$(".main-view .spacious-container").floatingScroll("init");
// ... the app re-renders the main view ...
$(".main-view").html("...");
// destroy floating scrollbar widgets whose containers are not in the document anymore
$(window).floatingScroll("destroyDetached");

Special cases

If you want to attach a floating scrollbar to a container living in a positioned box (e.g. a modal popup with position: fixed) then you need to apply two special indicating class names in the markup. The plugin detects these indicating class names (they are prefixed with fl-scrolls-) and switches to a special functioning mode.

<div class="fl-scrolls-viewport"><!-- (1) -->
    <div class="fl-scrolls-body"><!-- (2) -->
        <div class="spacious-container">
            <!-- Horizontally wide contents -->
        </div>
    </div>
</div>

The .fl-scrolls-viewport element (1) is a positioned block (with any type of positioning except static) which serves for correct positioning of the floating scrollbar. Note that this element itself should not be scrollable. The .fl-scrolls-body element (2) is a vertically scrollable block (with overflow: auto) which encloses the target block the plugin is applied to. After applying these special class names, you may initialise the plugin as usual:

$(".spacious-container").floatingScroll();

The plugin’s CSS file provides some basic styles for elements with classes .fl-scrolls-viewport and .fl-scrolls-body. Feel free to adjust their styles in your stylesheets as needed.

“Unobtrusive” mode

You can make a floating scrollbar more “unobtrusive” so that it will appear only when the mouse pointer hovers over the scrollable container. To do so just apply the class fl-scrolls-hoverable to the desired scrollable container owning the floating scrollbar.

Live demos

Check out some usage demos here.

floating-scroll's People

Contributors

amphiluke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

floating-scroll's Issues

Floating scroll bars do not work on Safari

Issue by @wesley-kiyohara
Originally opened on 24 Sep 2015 as Amphiluke/jquery-plugins#7


Currently on Safari, I am unable to see the horizontal scrollbar until the bottom the div is reached. This is after I've changed my Mac settings to always display scrollbars, and can be seen on each of your demos. Although I cannot see the .fl-scrolls div, I'm still able to hover over where it should be and scroll horizontally.
scroll

Publish to Bower

You have done a very nice work, wondering you could publish to bower.. Thanks again.

Import warning

I have installed floatingScroll via npm. v3.0.2

I then attempt to import into my vue application:
import floatingScroll from "floating-scroll";

I then get this warning, not an error:

./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./test.vue 156:20-34 "export 'default' (imported as 'floatingScroll') was not found in 'floating-scroll'

I am still achieving the floating scroll i am looking for, but i would like to tackle this warning and make sure that isn't a precursor to something breaking in the fiture.

Help Wanted - Implementation in Angular

Hi first of all fantastic plugin! I have an angular 6 project and would like to implement this. I do/can install JQUERY into my project but am struggling to implement this into my project. Have you had any experience using this in Angular? If so would it be possible to provide a working guide on how to implement it in an Angular project? Many thanks in advanced.

Requirejs optimizer compatibility

Requirejs optimizer throws Error: Mismatched anonymous define() module.

Replacing line global.define(["jquery"], factory); with define(["jquery"], factory); seems to resolve the problem

Wrong work within containers with position: fixed

Hey guys, this is really awasome plugin, thank you so mush for it!
But unfortunatelly I got issue when scroll added to a block within scrollable container with position: fixed. Looking through the code if found next method:

var wnd = window,
getMaxVisibleY = function () {return wnd.pageYOffset + wnd.innerHeight;};

It's used in checkVisibility() . As you see this max visible Y is calculated according to page's offset but actually it was overriden by fixed block's position (including its own scroll).
The single solution I can see it's for parrent container with position fixed instead of window here + listening to scrolling of it.

Html will be looking like this: https://jsfiddle.net/12x4t50h/

Not working in modals that rely on CSS transform property

Hey, loving this plugin!

The following addresses an issue with a recent feature that fixes floating scroll bars in modals. Some modals will use the CSS property transform:translate(x,y). This is so animation can be applied to the modal. A great example of this is the Bootstrap Modal.

Without going into too much detail (just google "css transform position fixed"), transform:translate and position:fixed don't act as expected. In this situation, when the user scrolls down, the floating scrollbar will not fix itself in place as it did before. This is happening on Chrome and FF but not Safari, IE or Edge.

The only way I have found a way around this is to place the floating scrollbar outside the element that is transformed with CSS.

This is a simple plunk showing how to replicate the issue: http://plnkr.co/edit/nIdRvY7uprxWXAaNrskS?p=preview

I've finished developing a solution to this but need to do some solid cross-browser compatibility testing first before I create a pull request. You'll hear from me soon 😄

floating scroll bar and actual scroll bar not in sync after couple of clicks

Steps to reproduce on example#1 on demo page https://amphiluke.github.io/floating-scroll/

  1. Scroll down just enough for floating scroll bar to surface
  2. Click on right side of scroll bar to shift it to the right
  3. Scroll down so that actual scroll bar surfaces
  4. Click on left side of scroll bar to shift it to the left
  5. Scroll up so that floating scroll bar surfaces
  6. Click on right side of scroll bar to shift it to the right
  7. Scroll down so that actual scroll bar surfaces

You should now see actual scroll bar still sitting on the left edge.

Floating scroll not updating when container is hidden

Hi there!

I have been using your plugin and find it to be very useful! But I noticed an issue where:

  • I have two tabs, where each tab has a table that contains a floating scroll instance
  • When user loads, they view the first tab with a table, but there is no visible floating scroll because the table is not very wide.
  • If the user goes to the second tab and sees a very wide table, the floating scroll shows up.
  • If the user goes back to the first tab, the floating scroll from the second tab is still visible.
  • Only when I resize the floating scroll disappears.

The problem is that I cannot detect when an element visibility changes and then ask the floating scroll to update. The way I fixed this was to modify your plugin from this:

        var flscroll = $("<div class='fl-scrolls'></div>");
        $("<div></div>").appendTo(flscroll).css({width: this.cont.block.scrollWidth + "px"});
        return flscroll.appendTo('body');

to this:

        var flscroll = $("<div class='fl-scrolls'></div>");
        $("<div></div>").appendTo(flscroll).css({width: this.cont.block.scrollWidth + "px"});
        return flscroll.appendTo($(this.cont.block));

Do you think this is a better approach since now the floating scroll instances are attached to the container and not the body?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.