Skip to content

Commit 736ff3c

Browse files
committed
Programatic scrolling animation
1 parent 4a91a39 commit 736ff3c

7 files changed

+35
-19
lines changed

README.textile

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ To use the plugin you obviously need jQuery (it should work in jQuery 1.4 and la
1818

1919
h2. Download
2020

21-
You can download the latest version "here":https://github.com/mzubala/jquery-custom-scrollbar/raw/master/build/jquery-custom-scrollbar-0.5.2.zip
21+
You can download the latest version "here":https://github.com/mzubala/jquery-custom-scrollbar/raw/master/build/jquery-custom-scrollbar-0.5.3.zip
2222

2323
h2. Demos
2424

25-
In <code>demos</code> folder of this repo, there are some example usages of custom scrollbar and its api. The demos are also
26-
available online "here":http://jquery-custom-scrollbar.rocketmind.pl/
25+
In <code>demos</code> folder of this repo, there are some example usages of custom scrollbar and its api. The demos are also available online "here":http://jquery-custom-scrollbar.rocketmind.pl/
2726

2827
h2. Usage
2928

@@ -54,8 +53,7 @@ Add a skin class to your container:
5453
</div>
5554
</pre></code>
5655

57-
In the example we use <code>default-skin</code>. Plugin comes with two other predefined skins: <code>gray-skin</code> and <code>modern-skin</code>.
58-
You are not limited to that and you can style scrollbar your own way.
56+
In the example we use <code>default-skin</code>. Plugin comes with two other predefined skins: <code>gray-skin</code> and <code>modern-skin</code>. You are not limited to that and you can style scrollbar your own way.
5957

6058
Finally call this js code:
6159

@@ -82,15 +80,20 @@ h2. Options
8280
There are some options you can pass when initializing scrollbar:
8381

8482
|_. Option |_. Type|_. Default value |_. Description |
85-
| <code>skin</code>|<code>String</code>|<code>undefined</code>|A css skin class that will be added to the scrolled container. You can define it in html as well as here in options. Note that skin has to be defined in one of those ways.|
83+
| <code>animationSpeed</code> | <code>Integer</code> | <code>300</code> | Speed of the animation of programatic scrolling.|
8684
| <code>hScroll</code> | <code>Boolean</code> | <code>true</code> | Indicates whether or not, horizontal scrollbar should be shown when it's necessary. |
87-
| <code>vScroll</code> | <code>Boolean</code> | <code>true</code> | Same as above but applies to vertical scrollbar. |
85+
| <code>skin</code>|<code>String</code>|<code>undefined</code>|A css skin class that will be added to the scrolled container. You can define it in html as well as here in options. Note that skin has to be defined in one of those ways.|
8886
| <code>updateOnWindowResize</code> | <code>Boolean</code> | <code>false</code> | Indicates whether scrollbar should recalculate thumb size when window is resized. See <code>demos/resize.html</code> for an example.|
87+
| <code>vScroll</code> | <code>Boolean</code> | <code>true</code> | Same as above but applies to vertical scrollbar. |
8988

9089
For example:
9190

9291
<pre><code>
93-
$("#my-container").customScrollbar({skin: "default-skin", hScroll: false, updateOnWindowResize: true})
92+
$("#my-container").customScrollbar({
93+
skin: "default-skin",
94+
hScroll: false,
95+
updateOnWindowResize: true
96+
})
9497
</pre></code>
9598

9699
h2. API
@@ -99,23 +102,23 @@ There are some methods of the plugin you may want to call.
99102

100103
h3. scrollTo(element)
101104

102-
Scrolls viewport to a given element inside scrolled content. An element might be jQuery object or a selector string. Example usage:
105+
Scrolls viewport to a given element inside scrolled content. An element might be jQuery object or a selector string. To controll animation speed use <code>animationSpeed</code> initialization option. Example usage:
103106

104107
<pre><code>
105108
$(".container").customScrollbar("scrollTo", "#some-element-inside-container")
106109
</pre></code>
107110

108111
h3. scrollToX(x)
109112

110-
Sets horizontal scrollbar position to <code>x</code> pixels. <code>x</code> should be in range from 0 to scrolled content width. If it's outside that range, content will be scrolled to the start or to the end.
113+
Sets horizontal scrollbar position to <code>x</code> pixels. <code>x</code> should be in range from 0 to scrolled content width. If it's outside that range, content will be scrolled to the start or to the end. To controll animation speed use <code>animationSpeed</code> initialization option.
111114

112115
<pre><code>
113116
$(".container").customScrollbar("scrollToX", 100)
114117
</pre></code>
115118

116119
h3. scrollToY(y)
117120

118-
Sets vertical scrollbar position to <code>y</code> pixels. <code>x</code> should be in range from 0 to scrolled content height. If it's outside that range, content will be scrolled to the start or to the end.
121+
Sets vertical scrollbar position to <code>y</code> pixels. <code>x</code> should be in range from 0 to scrolled content height. If it's outside that range, content will be scrolled to the start or to the end. To controll animation speed use <code>animationSpeed</code> initialization option.
119122

120123
<pre><code>
121124
$(".container").customScrollbar("scrollToY", 200)
-5.33 KB
Binary file not shown.
-5.33 KB
Binary file not shown.
-5.77 KB
Binary file not shown.
-7.54 KB
Binary file not shown.

custom-scrollbar.jquery.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"custom-scrollbar",
3-
"version":"0.5.2",
3+
"version":"0.5.3",
44
"title":"jQuery Custom Scrollbar",
55
"author":{
66
"name":"Maciej Zubala",

jquery.custom-scrollbar.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
hScroll:true,
88
vScroll:true,
99
updateOnWindowResize:false,
10+
animationSpeed: 300,
1011
onCustomScroll:undefined
1112
}
1213

@@ -121,12 +122,12 @@
121122

122123
scrollToX:function (x) {
123124
if (this.hScrollbar)
124-
this.hScrollbar.scrollTo(x);
125+
this.hScrollbar.scrollTo(x, true);
125126
},
126127

127128
scrollToY:function (y) {
128129
if (this.vScrollbar)
129-
this.vScrollbar.scrollTo(y);
130+
this.vScrollbar.scrollTo(y, true);
130131
},
131132

132133
remove: function() {
@@ -437,18 +438,21 @@
437438
scrollBy:function (delta) {
438439
var overviewPosition = -this.scrollable.$overview.position()[this.sizing.offsetComponent()];
439440
overviewPosition += delta;
440-
this.scrollTo(overviewPosition);
441+
this.scrollTo(overviewPosition, false);
441442
},
442443

443-
scrollTo:function (overviewPosition) {
444+
scrollTo:function (overviewPosition, animate) {
444445
if (overviewPosition < 0)
445446
overviewPosition = 0;
446447
if (overviewPosition > this.maxOverviewPosition)
447448
overviewPosition = this.maxOverviewPosition;
448449
var oldScrollPercent = this.scrollPercent;
449450
this.scrollPercent = overviewPosition / this.maxOverviewPosition;
450451
var thumbPosition = this.scrollPercent * this.maxThumbPosition;
451-
this.setScrollPosition(overviewPosition, thumbPosition);
452+
if(animate)
453+
this.setScrollPositionWithAnimation(overviewPosition, thumbPosition);
454+
else
455+
this.setScrollPosition(overviewPosition, thumbPosition);
452456
if (oldScrollPercent != this.scrollPercent)
453457
this.triggerCustomScroll(oldScrollPercent);
454458
},
@@ -470,7 +474,16 @@
470474

471475
setScrollPosition:function (overviewPosition, thumbPosition) {
472476
this.$thumb.css(this.sizing.offsetComponent(), thumbPosition + "px");
473-
this.scrollable.$overview.css(this.sizing.offsetComponent(), -overviewPosition + "px");
477+
this.scrollable.$overview.css(this.sizing.offsetComponent(), -overviewPosition + "px");
478+
},
479+
480+
setScrollPositionWithAnimation:function (overviewPosition, thumbPosition) {
481+
var thumbAnimationOpts = {};
482+
var overviewAnimationOpts = {};
483+
thumbAnimationOpts[this.sizing.offsetComponent()] = thumbPosition + "px";
484+
this.$thumb.animate(thumbAnimationOpts, this.scrollable.options.animationSpeed);
485+
overviewAnimationOpts[this.sizing.offsetComponent()] = -overviewPosition + "px";
486+
this.scrollable.$overview.animate(overviewAnimationOpts, this.scrollable.options.animationSpeed);
474487
},
475488

476489
calculateMaxThumbPosition:function () {
@@ -493,7 +506,7 @@
493506
var elementOffset = $element.offset();
494507
var overviewOffset = this.scrollable.$overview.offset();
495508
var viewPortOffset = this.scrollable.$viewPort.offset();
496-
this.scrollTo(elementOffset[this.sizing.offsetComponent()] - overviewOffset[this.sizing.offsetComponent()]);
509+
this.scrollTo(elementOffset[this.sizing.offsetComponent()] - overviewOffset[this.sizing.offsetComponent()], true);
497510
}
498511
},
499512

0 commit comments

Comments
 (0)