Skip to content

Commit 54f9913

Browse files
committed
#17 Fixed thumb height and width
1 parent 15e7387 commit 54f9913

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

README.textile

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ There are some options you can pass when initializing scrollbar:
8181

8282
|_. Option |_. Type|_. Default value |_. Description |
8383
| <code>animationSpeed</code> | <code>Number</code> | <code>300</code> | Speed of the animation of programmatic scrolling. It's possible to edit it with <code>setAnimationSpeed</code> method. Animation speed equal to <code>0</code> means no animation.|
84+
| <code>fixedThumbHeight</code> | <code>Number</code> | <code>undefined</code> | By default thumb height (in case of vertical scrollbar) is calculated automatically depending on viewport and overview height but you can fix thumb height to your chosen pixel value by setting this option. Make sure to not set <code>min-height</code> in css if you set <code>fixedThumbHeight</code> because <code>min-height</code> has priority.|
85+
| <code>fixedThumbWidth</code> | <code>Number</code> | <code>undefined</code> | Option analogical to <code>fixedThumbHeight</code> but applied to thumbs of horizontal scrollbar.|
86+
| <code>hScroll</code> | <code>Boolean</code> | <code>true</code> | Indicates whether or not, horizontal scrollbar should be shown when it's necessary. |
8487
| <code>hScroll</code> | <code>Boolean</code> | <code>true</code> | Indicates whether or not, horizontal scrollbar should be shown when it's necessary. |
8588
| <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.|
8689
| <code>swipeSpeed</code>|<code>Number</code>|<code>1</code>|Indicates how fast touch scroll should be. When you swipe your finger by <code>x</code> pixels the content will be scrolled by <code>swipeSpeed * x</code> pixels.|

demos/basic.html

+41
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@
6565
min-width: 300px;
6666
}
6767

68+
#fixed-thumb-size-demo {
69+
width: 400px;
70+
height: 200px;
71+
}
72+
73+
/* Vertical and horizontal scrollbar - one extra step required - add width of overview - overall width of scrolled content*/
74+
#fixed-thumb-size-demo .overview {
75+
width: 600px;
76+
}
77+
6878
</style>
6979
</head>
7080
<body>
@@ -162,9 +172,40 @@ <h2>Min thumb size</h2>
162172
interdum massa nibh nec erat.
163173
</div>
164174

175+
<h2>Fixed thumb size</h2>
176+
177+
<!-- Add a skin class to the scrolled div: default-skin in this case -->
178+
<div id="fixed-thumb-size-demo" class="default-skin">
179+
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Proin nibh augue, suscipit a, scelerisque sed,
180+
lacinia in, mi. Cras vel lorem. Etiam pellentesque aliquet tellus. Phasellus pharetra nulla ac diam.
181+
Quisque
182+
semper justo at risus. Donec venenatis, turpis vel hendrerit interdum, dui ligula ultricies purus, sed
183+
posuere libero dui id orci. Nam congue, pede vitae dapibus aliquet, elit magna vulputate arcu, vel
184+
tempus
185+
metus leo non est. Etiam sit amet lectus quis est congue mollis. Phasellus congue lacus eget neque.
186+
Phasellus ornare, ante vitae consectetuer consequat, purus sapien ultricies dolor, et mollis pede metus
187+
eget
188+
nisi. Praesent sodales velit quis augue. Cras suscipit, urna at aliquam rhoncus, urna quam viverra nisi,
189+
in
190+
interdum massa nibh nec erat.
191+
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Proin nibh augue, suscipit a, scelerisque sed,
192+
lacinia in, mi. Cras vel lorem. Etiam pellentesque aliquet tellus. Phasellus pharetra nulla ac diam.
193+
Quisque
194+
semper justo at risus. Donec venenatis, turpis vel hendrerit interdum, dui ligula ultricies purus, sed
195+
posuere libero dui id orci. Nam congue, pede vitae dapibus aliquet, elit magna vulputate arcu, vel
196+
tempus
197+
metus leo non est. Etiam sit amet lectus quis est congue mollis. Phasellus congue lacus eget neque.
198+
Phasellus ornare, ante vitae consectetuer consequat, purus sapien ultricies dolor, et mollis pede metus
199+
eget
200+
nisi. Praesent sodales velit quis augue. Cras suscipit, urna at aliquam rhoncus, urna quam viverra nisi,
201+
in
202+
interdum massa nibh nec erat.
203+
</div>
204+
165205
<script type="text/javascript">
166206
$(window).load(function () {
167207
$(".demo").customScrollbar();
208+
$("#fixed-thumb-size-demo").customScrollbar({fixedThumbHeight: 50, fixedThumbWidth: 60});
168209
});
169210
</script>
170211
</body>

jquery.custom-scrollbar.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
animationSpeed: 300,
1111
onCustomScroll: undefined,
1212
swipeSpeed: 1,
13-
wheelSpeed: 40
13+
wheelSpeed: 40,
14+
fixedThumbWidth: undefined,
15+
fixedThumbHeight: undefined
1416
}
1517

1618
var Scrollable = function (element, options) {
@@ -208,7 +210,7 @@
208210
this.overviewSize = this.sizing.size(this.scrollable.$overview);
209211
this.ratio = this.viewPortSize / this.overviewSize;
210212
this.sizing.size(this.$scrollBar, this.viewPortSize);
211-
this.thumbSize = Math.max(Math.round(this.ratio * this.viewPortSize), this.sizing.minSize(this.$thumb));
213+
this.thumbSize = this.calculateThumbSize();
212214
this.sizing.size(this.$thumb, this.thumbSize);
213215
this.maxThumbPosition = this.calculateMaxThumbPosition();
214216
this.maxOverviewPosition = this.calculateMaxOverviewPosition();
@@ -222,6 +224,16 @@
222224
this.$scrollBar.toggle(this.enabled);
223225
},
224226

227+
calculateThumbSize: function () {
228+
var fixedSize = this.sizing.fixedThumbSize(this.scrollable.options)
229+
var size;
230+
if (fixedSize)
231+
size = fixedSize;
232+
else
233+
size = this.ratio * this.viewPortSize
234+
return Math.max(size, this.sizing.minSize(this.$thumb));
235+
},
236+
225237
initMouseMoveScrolling: function () {
226238
var _this = this;
227239
this.$thumb.mousedown(function (event) {
@@ -543,6 +555,10 @@
543555
return parseInt($el.css("min-width")) || 0;
544556
},
545557

558+
fixedThumbSize: function (options) {
559+
return options.fixedThumbWidth;
560+
},
561+
546562
scrollBar: function ($el) {
547563
return $el.find(".scroll-bar.horizontal");
548564
},
@@ -603,6 +619,10 @@
603619
return parseInt($el.css("min-height")) || 0;
604620
},
605621

622+
fixedThumbSize: function (options) {
623+
return options.fixedThumbHeight;
624+
},
625+
606626
scrollBar: function ($el) {
607627
return $el.find(".scroll-bar.vertical");
608628
},

0 commit comments

Comments
 (0)