Baron — a small, fast and crossbrowser custom scrollbar with native system scroll mechanic.
Demo
http://diokuz.github.io/baron/
Features
- Doesn't replace native system scroll mechanic.
- Customizable scrollbar design with full CSS support.
- No strong dependencies on jQuery.
- Plugin system (fixable headers, sticky footer, autotests and more)
- (new) Can be inited on hidden blocks
Baron just hides the system scrollbar, without removing it. This guarantees scrolling will work on any system where the CSS property 'overflow: scroll' is applied.
Simple usage
If you want only to hide system scrollbar:
- Include either the development or minified version of baron.js:
<script src="baron.js"></script>
<div class="scroller">
Your scrollable content here
</div>
.scroller {
overflow-y: scroll;
}
.scroller::-webkit-scrollbar {
width: 0;
}
Note: you can choose any class names, and style them as you want.
$('.scroller').baron();
Advanced usage
<div class="scroller">
Your scrollable content here
<div class="scroller__track">
<div class="scroller__bar"></div>
</div>
</div>
.scroller {
overflow-y: scroll;
}
.scroller::-webkit-scrollbar {
width: 0;
}
.scroller__track {
display: none;
position: absolute;
right: 4px;
top: 10px;
bottom: 4px;
width: 10px;
background: rgba(0, 0, 0, .1);
}
.baron .scroller__track {
display: block;
}
.scroller__bar {
position: absolute;
z-index: 1;
right: 0;
width: 10px;
background: #999;
}
You can specify some parameters on baron initialization:
var scroll = $('.scroller').baron(params);
var scroll = baron(params);
and store baron scrollbar object to scroll
variable.
where:
var params = {
scroller: '.scroller',
root: $('.my_scroller'),
bar: '.scroller__bar',
track: '.scroller__track',
barOnCls: 'baron',
direction: 'h',
freeze: true,
pause: .2,
$: function(selector, context) {
return bonzo(qwery(selector, context));
},
event: function(elem, event, func, mode) {
if (mode == 'trigger') {
mode = 'fire';
}
bean[mode || 'on'](elem, event, func);
}
};
All parameters are optional (except scroller or root, if you are not using baron as jQuery plugin).
scroll
methods:
scroll.update();
scroll.dispose();
Note: baron returns the baron object, even in jQuery mode. That can break jQuery chaining. For example, you can't do this:
$('.scroller').baron().css().animate();
but you can:
$('.scroller').css().animate().baron();
and even more:
var scroll = $('.scroller').css().animate().baron().fix();
scroll.baron({direction: 'h'}).test().anotherBaronPlugin();
Every baron plugin sould return baron object (this);
Horizontal and bidirectional scroll
To switch default vertical direction to horizontal direction just set 'direction' param to 'h' value:
baron({
...
direction: 'h'
});
If you want to scroll in both directions (vertical and horizontal) you must initialize baron two times: one per direction. In than case you can do chaining:
baron(vParams).baron(hParams);
Note: think about horizontal baron as about second independent baron instance, or as about plugin for 'baron', which simply calls 'baron' with redefined default params - both statements are true, actually. Unfortunately, in case above you only can manage last baron instance in chain (to update or dispose it). To manage both you have to initialize them independently:
vScroll = baron(vParams);
hScroll = baron(hParams);
...
vScroll.dispose();
hScroll.dispose();
##Updating baron
When container size changed (for example: you load additional data to the container using ajax), you should call update() method:
scroll.update();
or fire custom event 'sizeChange' to wrapper:
$('.scroller').trigger('sizeChange');
##Disposing baron
If you removed html-nodes, which used baron, from DOM, dont forget dispose related baron instance manually. Use 'dispose' method for that.
##noConflict mode
If you need window.baron for another purposes you can restore original value:
...
var graf = baron.noConflict();
Custom build (Grunt)
If you want exclude plugins functionality, type
grunt core
in your console, and you will get dist/baron.js and dist/baron.min.js only with core functionality.
Type
grunt full
to build full version of baron (including all available plugins).
Output files you can find in /dist/ folder.
Browsers support
Full support: Chrome 1+, Firefox 3.6+, Safari 5+, Opera 9+ on Windows, OS X and iOS. Also, the best ever browser downloader - Internet Explorer - supported since version 7.
Partial (core) support: IE6.
Not supported: Opera mini, old versions of Android browser, and other browsers which does not implemented the overflow: scroll
CSS property.
fix plugin
To use fixable headers you should initialize fix plugin after baron:
<div class="scroller">
<div class="header__title-wrapper">
<div class="header__title">First element</div>
</div>
...content...
<div class="header__title-wrapper">
<div class="header__title">Second element</div>
</div>
...content...
</div>
baron(baronParams).fix(params);
where:
var params = {
elements: '.header__title',
outside: 'header__title_state_fixed',
inside: 'header__title_state_unfixed',
before: 'header__title_position_top',
after: 'header__title_position_bottom',
limiter: true,
radius: 10,
grad: 'header__title_state_grad',
clickable: false,
scroll: function(data) {}
};
Controls plugin
baron().controls(params);
var params = {
track: '.visual-track',
forward: '.forward-btn',
backward: '.backward-btn',
screen: .5
delta: 40
};
test plugin
If you have some troubles with baron, use test plugin:
baron(...).test(params);
And read results in browser console.
There is no params for test() right now.
License
MIT.