Git Product home page Git Product logo

antiscroll's Introduction

Antiscroll: cross-browser native OS X Lion scrollbars

Antiscroll fixes a fundamental problem JavaScript UI developers commonly face: how do I customize scrollbars so that they get out of the way (for example, for different form widgets), but retain their native scrolling properties (like OS widge scrolling velocity, or OS specific inertia)?

Antiscroll addresses this issue by providing a cross-browser implementation of the scrollbars popularized by OS X Lion that retains native properties.

Features

  • Supports mousewheels, trackpads, other input devices natively.
  • Total size is 1kb minified and gzipped.
  • Doesn't magically autowrap your elements with divs (manual wrapping is necessary, please see index.html demo)
  • Fade in/out controlled with CSS3 animations.
  • Shows scrollbars upon hovering.
  • Scrollbars are draggable.
  • Size of container can be dynamically adjusted and scrollbars will adapt.
  • Supports IE7+, Firefox 3+, Chrome, Safari

Demo

Please click here to see it in action.

Installation

  1. Wrap scrollable content with the class antiscroll-inner
  2. Wrap the above with the class antiscroll-wrap
  3. Include the following Javascript
   $(function () {
     $('.antiscroll-wrap').antiscroll();
   });

Configuration

You may remove automatic scrollbar hiding by passing in a key-value to the antiscroll() function like so:

   $(function () {
     $('.antiscroll-wrap').antiscroll({
       autoHide: false
     });
   });

What does it look like?

Firefox 8 overflow: scroll and antiscroll on OS X

IE 9 overflow: scroll and antiscroll

How does it work?

The idea behind Antiscroll is to leverage real scrollbars, but hide them from the view. The implementation consists of 3 steps.

1. Measure scrollbars width

In order to measure scrollbars width we use the following technique:

  1. Insert a div with a fixed width and measure the inner width
  2. Force overflow: scroll
  3. Measure the inner width. The difference is the scrollbar width

The caveat of this technique is precisely OS X Lion. Since the scrollbars float on top of the content, their width is always zero but they still overlay your content. To address this issue we add an aditional step which consists of declaring ::-webkit-scrollbar and ::scrollbar CSS pseudo-properties that set the width of the scrollbars to zero for modern browsers.

2. Adjust the width of the inner element

The parent element receives overflow: hidden and the desired width and height for the widget.

The inner .antiscroll-inner element receives the same width and height, but the script augments this two values with the size of the scrollbars, effectively hiding them.

The inner element is always overflow: scroll.

3. Listen on the scroll event

We attach the scroll event to the scrollable element, and we create our scrollbars as absolutely positioned divs. We update our scrollbars based on the detected scrollLeft and scrollTop of the element.

Credits

This technique was inspired by Facebook's chat sidebar/ticker, which also reproduces Lion's scrollbars, but relying on setting the width of the inner container to an arbitrarily large width, therefore allowing scrolling of a single axis (vertical).

Scrollbar size detection based on the work of Jonathan Sharp.

Contributors

Dependencies

  • jQuery
  • jquery-mousewheel: optional, only needed if you want to block further scrolling when you reach the boundaries of scrollable element.

TODO

  • Automatically leverage Joe Hewitt's scrollability as a replacement technique if a touch-enabled browser is detected.
  • IE6 support

License

(The MIT License)

Copyright (c) 2011 Guillermo Rauch <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

antiscroll's People

Contributors

2is10 avatar arlm avatar bpierre avatar davemckenna01 avatar devinrhode2 avatar fontaineshu avatar jumph4x avatar kapouer avatar logicalparadox avatar miketaylr avatar nulltask avatar nylen avatar pgherveou avatar pirxpilot avatar radagaisus avatar rauchg avatar retrofox avatar tristandunn 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

antiscroll's Issues

No track background?

In Lion when you mouseover the scrollbar,the track background will be show and you can click it to up or down scroll the page.
Sorry but i don't know whether it is called this name,do you know my words about the "track background"?

Click support for paging?

Would adding click support on scrollbar areas (outside the scroll handles, but inside the invisible scroll axes) be considered for paging? Having this feature would make the replacement scrollbars on par with the native scrollbars.

I'm just wondering if the lack of click support is by design, or something that can be considered if someone is willing to do it.

Dynamically appearing elements

Hi,

This is a small fix for elements that appear on run-time (ie. onclick of a button) and their content changes. Since they have display hidden initially I had to call .antiscroll() every time I show an element, in order to reset the scroll bars (I don't know if there is a better way of doing this...).

Now this causes constant size expansion each time the element appears (due to the css width += scrollbarSize() in the initialization function). My solution was to remove this extra width/height in the destroy():

  Antiscroll.prototype.destroy = function () {

    this.inner.css({
        'width':  '-=' + (this.y ? scrollbarSize() : 0)
      , 'height': '-=' + (this.x ? scrollbarSize() : 0)
    });

    if (this.horizontal) {
      this.horizontal.destroy();
      this.horizontal = null
    }
    if (this.vertical) {
      this.vertical.destroy();
      this.vertical = null
    }
    return this;
  };

Please let me know if I am doing something wrong, but seems reasonable to me to add this code in destroy, just to leave the element in its original state. This will also solve problems when antiscroll() is called multiple times.

Andreas

Rebuild/refresh methods on window resize

Hi,

My container is 100% height. I am handling the window resize to update the scroller. All three potential methods I've tried have issues.

Here is a gist that demonstrates the issue.

https://gist.github.com/2522897

The methods I've tried to get this to work

  • reapply the plugin_ - This works but the scroller range is not updated.
  • rebuild - Updates the size of the inner div but removes the scroll bars
  • refresh - Updates the scroller but doesn't change the size of the inner div.

I'm not sure why the plugin has both a rebuild and a refresh method? From reading the previous issues it seems rebuild was meant to solve the issue of flexible containers. I have an alternative fix which doesn't require the rebuild method.

function Antiscroll (el, opts) {
  this.el = $(el);
  this.options = opts || {};

  this.x = false !== this.options.x;
  this.y = false !== this.options.y;
  this.padding = undefined == this.options.padding ? 2 : this.options.padding;

  this.inner = this.el.find('.antiscroll-inner');
  this.refresh();
};

Antiscroll.prototype.refresh = function() {
  var width = this.el.width(),
      height = this.el.height(),
      needHScroll, needVScroll;

  this.inner.css({
      'width': (width + scrollbarSize()) + 'px'
    , 'height': (height + scrollbarSize()) + 'px'
  });

  needHScroll = this.inner.get(0).scrollWidth > width;
  needVScroll = this.inner.get(0).scrollHeight > height;

  if (!this.horizontal && needHScroll && this.x) {
    this.horizontal = new Scrollbar.Horizontal(this);
  } else if (this.horizontal && !needHScroll)  {
    this.horizontal.destroy();
    this.horizontal = null
  }

  if (!this.vertical && needVScroll && this.y) {
    this.vertical = new Scrollbar.Vertical(this);
  } else if (this.vertical && !needVScroll)  {
    this.vertical.destroy();
    this.vertical = null
  }
};

I simply update the inner size in the refresh method. This step can then be removed from the constructor. Works great for me. Maybe I am missing the reason for the rebuild method.

Thanks

Native scrollbar not being hidden in latest Chrome on Lion

Looks like the latest Chrome update has something funny going on with hiding native scrollbars. See screenshot - you get the custom antiscroll scrollbar overlaid on top of the native one. Screenshot is from Chrome 25.0.1364.99.

A co-worker has 24.0.1312.56 and this problem doesn't occur.

Related to issue #40 and the fix proposed in that commit, but the fix doesn't work on Lion.

antiscroll-chrome

scrollbarSize() calculation issue for scaled page (FireFox)

During the scroll bar size calculation the test div element with 50x50px is temporary created. But from some page scale (50%) this isn't enough for scrollbars (or scroll buttons, I'm not sure) and this function returns size 0.
Suppose that larger div should used (in my case it was 200x200).

Any chance of auto-showing the scrollbar?

Hi

I've tried setting initialDisplay, and also tried manually triggering scroll(), mouseenter(), mousewheel() and a bunch of other hacks, but I just can't seem to make the scrollbar appear immediately. I can make the scrollbar appear on mouseover of the element (a popup menu - see the "Choose Your Daddy-O" menu here in bottom left of screen http://campaigns.myer.com.au/FathersDay). But I can't seem to trigger it to auto-show as soon as the menu appears.

Any help appreciated. Cheers
Jason

[feature request] bounces

would it be plausible to do the bounce at the end of the content? for example how Safari bounces if you reach the edge of the page and keep trying to scroll via trackpad/mousewheel.

Releases? Tags?

Some tags would be very welcome as it would ease locking dependencies.

Shouldn't the scrollbar buttons be present on windows?

On Mac, we have very nice.. no, we have world-class trackpads and inertial scrolling.
On Windows, scrolling isn't too hot. So I think it'd be best to maintain scroll up/down buttons for them. They are butt-ugly, so it'd be good to have something that looked better.

Something like Spotify scrollbars could be done:
Spotify scrollbars

Please create an initial version

Hi there,

I like your plugin pretty much but it would be nice if you create an intial version (v1.0) on GitHub, so that I can use your plugin with a specific version, so that my application doesn't break if you change your plugin.

Your plugin is also listed on http://bower.io/search/ so it would be nice if you add a bower.json configuration file. I can help you with that! :)

Cheers from Berlin,

Benny

update on resize

i think the scrollbars need to be updated when the window resizes and when the inner content grows.

Any luck with iFrames?

I've been trying to get OSX Lion styled scrollbars to replace the default OS scrollbars for an iframe. I couldn't get this plugin to work with the iframe though. Has anyone else successfully done this?

Mousewheel Support is Miscalculated

Hey,

On Mac OS X with Chrome there's a rounding error when trying to stop the window from scrolling when reaching the end of the antiscroll box.

> log y, this.innerEl.scrollTop, this.pane.el.height(), this.innerEl.scrollHeight
-0.025 585 299.77777767181396 885 

The mousewheel end detection is this:

if ((y > 0 && 0 == this.innerEl.scrollTop) ||
        (y < 0 && (this.innerEl.scrollTop + this.pane.el.height()
          == this.innerEl.scrollHeight))) {
      ev.preventDefault();
      return false;
    }

A simple fix for the rounding error:

((y > 0 && 0 == this.innerEl.scrollTop) ||
        (y < 0 && (1+this.innerEl.scrollTop + this.pane.el.height()
          >= this.innerEl.scrollHeight)))

or:

((y > 0 && 0 == this.innerEl.scrollTop) ||
        (y < 0 && (this.innerEl.scrollTop + Math.round(this.pane.el.height())
          >= this.innerEl.scrollHeight)))

This applies to both horizontal and vertical scroll.

Compatibility with jQuery 1.10.2

HI,

The plugin doesn't seem to work with jQuery 1.10.2. I get a 'Uncaught TypeError: Object # has no method 'createDocumentFragment' error.

I am using the script in Wordpress 3.6 Any ideas what could be the problem?

no scroll bar after content has been appended

This is a great scrolling plugin! However I have stumbled across a small issue.

Say I am appending content like chat and there isn't quite enough content in the div yet when it reaches the point there is enough content the scroll bar doesn't appear but however the scroll mechanisms are fine! Its just the UI isn't retained.

http://jsfiddle.net/DY9CT/5/

Yosemite Scrollbar Bug

It looks as though the following has no more affect in OSX Yosemite - the default scrollbar shimmers below the custom bar.

.antiscroll-inner::-webkit-scrollbar,
.antiscroll-inner::scrollbar
  width: 0
  height: 0
  display: none

[enhancement] Add missing bower.json.

Hey, maintainer(s) of LearnBoost/antiscroll!

We at VersionEye are working hard to keep up the quality of the bower's registry.

We just finished our initial analysis of the quality of the Bower.io registry:

7530 - registered packages, 224 of them doesnt exists anymore;

We analysed 7306 existing packages and 1070 of them don't have bower.json on the master branch ( that's where a Bower client pulls a data ).

Sadly, your library LearnBoost/antiscroll is one of them.

Can you spare 15 minutes to help us to make Bower better?

Just add a new file bower.json and change attributes.

{
  "name": "LearnBoost/antiscroll",
  "version": "1.0.0",
  "main": "path/to/main.css",
  "description": "please add it",
  "license": "Eclipse",
  "ignore": [
    ".jshintrc",
    "**/*.txt"
  ],
  "dependencies": {
    "<dependency_name>": "<semantic_version>",
    "<dependency_name>": "<Local_folder>",
    "<dependency_name>": "<package>"
  },
  "devDependencies": {
    "<test-framework-name>": "<version>"
  }
}

Read more about bower.json on the official spefication and nodejs semver library has great examples of proper versioning.

NB! Please validate your bower.json with jsonlint before commiting your updates.

Thank you!

Timo,
twitter: @versioneye
email: [email protected]
VersionEye - no more legacy software!

Can't scroll up

Hi, I'm trying the lib (look great) but I can't scroll up! Only down. I'm on Chrome 22 beta for Mac OS X. Also, I'm using it like it's on the demo page.
Please let me know if you need further info.
Cheers.

Scrollbar measurement doesn't work on Chrome for Mac OSX 10.7.5

The scrollbar estimation function doesn't work for me (Chrome for OSX 10.7.5)

Here's the fix:

      var w1 = $('div', div).innerWidth();
      div.css('overflow-y', 'scroll');

      // huyl: fix this function (doesn't work on chrome for mac at least)
      // as per http://stackoverflow.com/a/10692895/161972
      //var w2 = $('div', div).innerWidth();
      var w2 = $('<div>').appendTo($('div', div)).width();

Chrome MacOSX Bug

Hi there,

On Chrome Version 27.0.1453.110 MAC OS X the plugin is not working correctly.
I have attached a screenshot of the demo.

screen shot 2013-06-06 at 09 28 40

Hope you'll have a fix for it,
Bogdan Soos

Not working with 100% width/height elements

I was trying to set it up with 100% width & height elements but it's not working, so I tried to dynamically set the width and height through javascript, it works this way but the div native scrollbars still appear, see image attached
antiscroll

HTML behind is:

  <body>
    <div class="box-wrap antiscroll-wrap">
      <div class="box">
        <div class="antiscroll-inner">
          <div class="box-inner">
            ...
          </div>
        </div>
      </div>
    </div>
 </body>

The CSS:

body {
        padding: 0;
        margin: 0;
        overflow: hidden;
      }

The JS:

        $(window).on('load resize', function() {
          $('.antiscroll-inner').css({
            height: $(window).height(),
            width: $(window).width(),
          });
        }).resize();

IE bug

hello
I was testing antiscroll in IE and found it's not even working in IE 9
also in IE 7 it make a box for it
IE 7 screenshot
Capture

Demo is broken

Throws a JS error:

Uncaught TypeError: Object [object Object] has no method 'antiscroll'

inner-bottom display

hi ,
antiscroll can not display all content,
example:
the last row of tr did not show all in the demo.
QQ20130401-1

Add a Minimum scroller height/width

See this screenshot. That tiny little black thing at the right side is antiscroll.

A simple fix is to add a minimal height, and adjust the top position based on it:

//  Scrollbar.Vertical.prototype.update

height = trackHeight * paneHeight / innerEl.scrollHeight
top = trackHeight * innerEl.scrollTop / innerEl.scrollHeight

if height < 30
  top = top - (30 - height)
  height = 30

this.el
      .css('height', height)
      .css('top', top)

Minimum height should probably be an option, as well.

scrollbar on top of table

Is it possible for the scroll bar to appear on the top (or both top & bottom) of the rendered table? It might be handy if it's a large table and the user needs to be clued in that there are more columns to the right.

Bryan

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.