Merge pull request #64 from jakubmal/flexible-scroll

Use vertical scroll for horizontal scrollbar if only horizontal scrollbar is present.
This commit is contained in:
Hyunje Alex Jun 2013-09-29 23:47:44 -07:00
commit 18c9517205
2 changed files with 35 additions and 4 deletions

View File

@ -66,6 +66,10 @@ Default: false
When set to an integer value, the thumb part of the scrollbar will not shrink below that number of pixels. When set to an integer value, the thumb part of the scrollbar will not shrink below that number of pixels.
Default: null 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
How to Use How to Use
---------- ----------

View File

@ -16,7 +16,8 @@
var defaultSettings = { var defaultSettings = {
wheelSpeed: 10, wheelSpeed: 10,
wheelPropagation: false, wheelPropagation: false,
minScrollbarLength: null minScrollbarLength: null,
useBothWheelAxes: false
}; };
$.fn.perfectScrollbar = function (suppliedSettings, option) { $.fn.perfectScrollbar = function (suppliedSettings, option) {
@ -62,6 +63,8 @@
var $scrollbarX = $("<div class='ps-scrollbar-x'></div>").appendTo($this), var $scrollbarX = $("<div class='ps-scrollbar-x'></div>").appendTo($this),
$scrollbarY = $("<div class='ps-scrollbar-y'></div>").appendTo($this), $scrollbarY = $("<div class='ps-scrollbar-y'></div>").appendTo($this),
scrollbarXActive,
scrollbarYActive,
containerWidth, containerWidth,
containerHeight, containerHeight,
contentWidth, contentWidth,
@ -104,20 +107,24 @@
contentHeight = $this.prop('scrollHeight'); contentHeight = $this.prop('scrollHeight');
if (containerWidth < contentWidth) { if (containerWidth < contentWidth) {
scrollbarXActive = true;
scrollbarXWidth = getSettingsAdjustedThumbSize(parseInt(containerWidth * containerWidth / contentWidth, 10)); scrollbarXWidth = getSettingsAdjustedThumbSize(parseInt(containerWidth * containerWidth / contentWidth, 10));
scrollbarXLeft = parseInt($this.scrollLeft() * (containerWidth - scrollbarXWidth) / (contentWidth - containerWidth), 10); scrollbarXLeft = parseInt($this.scrollLeft() * (containerWidth - scrollbarXWidth) / (contentWidth - containerWidth), 10);
} }
else { else {
scrollbarXActive = false;
scrollbarXWidth = 0; scrollbarXWidth = 0;
scrollbarXLeft = 0; scrollbarXLeft = 0;
$this.scrollLeft(0); $this.scrollLeft(0);
} }
if (containerHeight < contentHeight) { if (containerHeight < contentHeight) {
scrollbarYActive = true;
scrollbarYHeight = getSettingsAdjustedThumbSize(parseInt(containerHeight * containerHeight / contentHeight, 10)); scrollbarYHeight = getSettingsAdjustedThumbSize(parseInt(containerHeight * containerHeight / contentHeight, 10));
scrollbarYTop = parseInt($this.scrollTop() * (containerHeight - scrollbarYHeight) / (contentHeight - containerHeight), 10); scrollbarYTop = parseInt($this.scrollTop() * (containerHeight - scrollbarYHeight) / (contentHeight - containerHeight), 10);
} }
else { else {
scrollbarYActive = false;
scrollbarYHeight = 0; scrollbarYHeight = 0;
scrollbarYTop = 0; scrollbarYTop = 0;
$this.scrollTop(0); $this.scrollTop(0);
@ -250,8 +257,28 @@
var shouldPrevent = false; var shouldPrevent = false;
$this.bind('mousewheel.perfect-scroll', function (e, delta, deltaX, deltaY) { $this.bind('mousewheel.perfect-scroll', function (e, delta, deltaX, deltaY) {
if (!settings.useBothWheelAxes) {
// deltaX will only be used for horizontal scrolling and deltaY will
// only be used for vertical scrolling - this is the default
$this.scrollTop($this.scrollTop() - (deltaY * settings.wheelSpeed)); $this.scrollTop($this.scrollTop() - (deltaY * settings.wheelSpeed));
$this.scrollLeft($this.scrollLeft() + (deltaX * settings.wheelSpeed)); $this.scrollLeft($this.scrollLeft() + (deltaX * settings.wheelSpeed));
} else if (scrollbarYActive && !scrollbarXActive) {
// only vertical scrollbar is active and useBothWheelAxes option is
// active, so let's scroll vertical bar using both mouse wheel axes
if (deltaY) {
$this.scrollTop($this.scrollTop() - (deltaY * settings.wheelSpeed));
} else {
$this.scrollTop($this.scrollTop() + (deltaX * settings.wheelSpeed));
}
} else if (scrollbarXActive && !scrollbarYActive) {
// useBothWheelAxes and only horizontal bar is active, so use both
// wheel axes for horizontal bar
if (deltaX) {
$this.scrollLeft($this.scrollLeft() + (deltaX * settings.wheelSpeed));
} else {
$this.scrollLeft($this.scrollLeft() - (deltaY * settings.wheelSpeed));
}
}
// update bar position // update bar position
updateBarSizeAndPosition(); updateBarSizeAndPosition();