GithubHelp home page GithubHelp logo

willcode2surf / perfect-scrollbar Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mdbootstrap/tw-elements

0.0 2.0 0.0 715 KB

Minimalistic but perfect custom scrollbar plugin

Home Page: http://noraesae.github.io/perfect-scrollbar

HTML 34.32% JavaScript 61.60% CSS 4.08%

perfect-scrollbar's Introduction

perfect-scrollbar

Minimalistic but perfect custom scrollbar plugin

Travis CI

If you want information of old versions<0.6.0, please refer to an old documentation.

Why perfect-scrollbar?

I was once working on a personal project, and trying to find the jQuery scrollbar plugin that's perfect. But there was no perfect one. That's why I decided to make one.

perfect-scrollbar is minimalistic but perfect (for me, and maybe for most developers) scrollbar plugin working with jQuery or vanilla JavaScript as well.

I hope you love it!

Demo

It's on the GitHub Pages.

What does perfect mean?

perfect means...

  • There should be no css change on any original element.
  • The scrollbar should not affect the original design layout.
  • The design of the scrollbar should be (nearly) fully customizable.
  • If the size of the container or the content changes, the scrollbar size and position should be able to change.
  • New! It should work with vanilla JavaScript and major tools like NPM or Browserify.

Then perfect-scrollbar is really perfect?

  • perfect-scrollbar has some requirements, but doesn't change or add any style on original elements.
  • perfect-scrollbar is designed not to have width or height. It's fixed on the right and bottom side of the container.
  • You can change nearly all css styles of the scrollbar. The scrollbar design has no dependency on scripts.
  • perfect-scrollbar supports an 'update' function. Whenever you need to update the size or position of the scrollbar, just update.
  • Additionally, perfect-scrollbar uses 'scrollTop' and 'scrollLeft', not absolute positioning or something messy.

It's cool, isn't it?

Install

The best way to install and use perfect-scrollbar is with NPM. It's registered on npm as perfect-scrollbar.

$ npm install perfect-scrollbar

You can download the latest stable version with download links here. You also can find all releases on Releases.

If you want to use the development version of the plugin, use the source files which are not minified. They're in the src directory. The development version may be unstable, but some known bugs may have been fixed.

$ git clone https://github.com/noraesae/perfect-scrollbar.git
$ cd perfect-scrollbar/src
$ npm install
$ gulp # will lint and build the source code.

There is a Bower package for perfect-scrollbar as well. It is managed under the perfect-scrollbar-bower repository. The plugin is registered as perfect-scrollbar.

$ bower install perfect-scrollbar

You can also load it from cdnjs. It is registered as jquery.perfect-scrollbar.

Requirements

To make this plugin perfect, some requirements were unavoidable. But, they're all very trivial and there is nothing to worry about.

The following requirements should meet.

  • the container must have a 'position' css style.

The following requirements are included in the basic CSS, but please keep in mind when you'd like to change the CSS files.

  • the container must have an 'overflow:hidden' css style.
  • the scrollbar's position must be 'absolute'.
  • the scrollbar-x must have a 'bottom' css style, and the scrollbar-y must have a 'right' css style.

How to use

First of all, please check if the container element meets the requirements.

<link rel='stylesheet' href='dist/css/perfect-scrollbar.css' />
<style>
  #container {
    position: relative;
    height: 100%; /* Or whatever you want (eg. 400px) */
  }
</style>

I would recommend using the plugin with NPM and CommonJS module system like Browserify.

var Ps = require('perfect-scrollbar');

Or you can just load the script file as usual.

<script src='dist/js/perfect-scrollbar.js'></script>

To initialise the plugin, Ps.initialize is used.

var container = document.getElementById('container');
Ps.initialize(container);

It can be initialised with optional parameters.

Ps.initialize(container, {
  wheelSpeed: 2,
  wheelPropagation: true,
  minScrollbarLength: 20
});

If the size of your container or content changes, call update.

Ps.update(container);

If you want to destory the scrollbar, use destroy.

Ps.destroy(container);

If you want to scroll to somewhere, just use a scrollTop property and update.

container.scrollTop = 0;
Ps.update(container);

You can also get information about how to use the plugin from code in the examples directory of the source tree.

jQuery

As you may already know, perfect-scrollbar was a jQuery plugin. And it is as well. There's a jQuery adaptor and the plugin can be used in the same way it used to be used before.

I also recommend using NPM and CommonJS here, but it's not mandatory.

var $ = require('jquery');
require('perfect-scrollbar/jquery')($);

For sure, you can just import a built script.

<script src='dist/js/perfect-scrollbar.jquery.js'></script>

After importing it, you can use the plugin in the usual way.

$('#container').perfectScrollbar();          // Initialize
$('#container').perfectScrollbar({ ... });   // with options
$('#container').perfectScrollbar('update');  // Update
$('#container').perfectScrollbar('destroy'); // Destroy

Optional parameters

perfect-scrollbar supports optional parameters.

wheelSpeed

The scroll speed applied to mousewheel event.
Default: 1

wheelPropagation

If this option is true, when the scroll reaches the end of the side, mousewheel event will be propagated to parent element.
Default: false

swipePropagation

If this option is true, when the scroll reaches the end of the side, touch scrolling will be propagated to parent element.
Default: true

minScrollbarLength

When set to an integer value, the thumb part of the scrollbar will not shrink below that number of pixels.
Default: null

maxScrollbarLength

When set to an integer value, the thumb part of the scrollbar will not expand over that number of pixels.
Default: null

useBothWheelAxes

When set to true, and only one (vertical or horizontal) scrollbar is visible then both vertical and horizontal scrolling will affect the scrollbar.
Default: false

useKeyboard

When set to true, the scroll works with arrow keys on the keyboard. The element is scrolled only when the mouse cursor hovers the element.
Default: true

suppressScrollX

When set to true, the scroll bar in X axis will not be available, regardless of the content width.
Default: false

suppressScrollY

When set to true, the scroll bar in Y axis will not be available, regardless of the content height.
Default: false

scrollXMarginOffset

The number of pixels the content width can surpass the container width without enabling the X axis scroll bar. Allows some "wiggle room" or "offset break", so that X axis scroll bar is not enabled just because of a few pixels.
Default: 0

scrollYMarginOffset

The number of pixels the content height can surpass the container height without enabling the Y axis scroll bar. Allows some "wiggle room" or "offset break", so that Y axis scroll bar is not enabled just because of a few pixels.
Default: 0

Contribution

Please read Contributing in the wiki before making any contribution.

I really welcome contributions! Please feel free to fork and issue pull requests when...

  • You have a very nice idea to improve this plugin!
  • You found a bug!
  • You're good at English and can help my bad English!

For IE problems, please refer to IE Support.

IE Support

The plugin would work in IEs >= IE9 (not well, though).

The patches to fix problems in IE<=8 won't be accepted.

When old IEs should be supported, please fork the project and make patches personally.

Helpdesk

If you have any idea to improve this project or any problem using this, please feel free to upload an issue.

For common problems there is a FAQ wiki page. Please check the page before uploading an issue.

License

The MIT License (MIT) Copyright (c) 2015 Hyunje Alex Jun and other contributors.

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.

perfect-scrollbar's People

Contributors

adrianonantua avatar asiviero avatar d42f avatar danielapt avatar davidkethel avatar di-john avatar dimitrilarue avatar feasul avatar frozeman avatar genediazjr avatar gregdthomas avatar itsdrewmiller avatar ivan1986 avatar joshuaballoch avatar jtmalinowski avatar julien avatar kelion avatar le717 avatar malroc avatar maoziliang avatar mathieulorber avatar mnico avatar modified avatar rangermeier avatar smithl avatar strichter avatar suxxes avatar tatarinov avatar tkhyn avatar zeevl avatar

Watchers

 avatar  avatar

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.