Implement opposite-sided scrollbars.

Now when you use `top` for the x-axis scrollbar or `left` for the y-axis
scrollbar, the scrollbars will be displayed on the opposite side.
This commit is contained in:
Hyunje Alex Jun 2014-04-27 11:01:11 +01:00
parent 61e1e6893d
commit 4a8e8066ab
2 changed files with 86 additions and 6 deletions

View File

@ -0,0 +1,39 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>perfect-scrollbar example</title>
<link href="../src/perfect-scrollbar.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="../src/jquery.mousewheel.js"></script>
<script src="../src/perfect-scrollbar.js"></script>
<style>
.contentHolder { position:relative; margin:0px auto; padding:0px; width: 600px; height: 400px; overflow: hidden; }
.contentHolder .content { background-image: url('./azusa.jpg'); width: 1280px; height: 720px; }
.spacer { text-align:center }
/* Change the alignment of scrollbars */
/* Recommended: You can just modify the CSS file directly. */
.ps-container .ps-scrollbar-x-rail {
top: 3px;
bottom: auto; /* If using `top`, there shouldn't be a `bottom`. */
}
.ps-container .ps-scrollbar-y-rail {
left: 3px;
right: auto; /* If using `left`, there shouldn't be a `right`. */
}
</style>
<script>
jQuery(document).ready(function ($) {
"use strict";
$('#Default').perfectScrollbar();
});
</script>
</head>
<body>
<div id="Default" class="contentHolder">
<div class="content">
</div>
</div>
</body>
</html>

View File

@ -94,9 +94,13 @@
scrollbarXWidth, scrollbarXWidth,
scrollbarXLeft, scrollbarXLeft,
scrollbarXBottom = parseInt($scrollbarXRail.css('bottom'), 10), scrollbarXBottom = parseInt($scrollbarXRail.css('bottom'), 10),
isScrollbarXUsingBottom = scrollbarXBottom === scrollbarXBottom, // !isNaN
scrollbarXTop = isScrollbarXUsingBottom ? null : parseInt($scrollbarXRail.css('top'), 10),
scrollbarYHeight, scrollbarYHeight,
scrollbarYTop, scrollbarYTop,
scrollbarYRight = parseInt($scrollbarYRail.css('right'), 10), scrollbarYRight = parseInt($scrollbarYRail.css('right'), 10),
isScrollbarYUsingRight = scrollbarYRight === scrollbarYRight, // !isNaN
scrollbarYLeft = isScrollbarYUsingRight ? null: parseInt($scrollbarYRail.css('left'), 10),
eventClassName = getEventClassName(); eventClassName = getEventClassName();
var updateContentScrollTop = function (currentTop, deltaY) { var updateContentScrollTop = function (currentTop, deltaY) {
@ -115,7 +119,12 @@
var scrollTop = parseInt(scrollbarYTop * (contentHeight - containerHeight) / (containerHeight - scrollbarYHeight), 10); var scrollTop = parseInt(scrollbarYTop * (contentHeight - containerHeight) / (containerHeight - scrollbarYHeight), 10);
$this.scrollTop(scrollTop); $this.scrollTop(scrollTop);
$scrollbarXRail.css({bottom: scrollbarXBottom - scrollTop});
if (isScrollbarXUsingBottom) {
$scrollbarXRail.css({bottom: scrollbarXBottom - scrollTop});
} else {
$scrollbarXRail.css({top: scrollbarXTop + scrollTop});
}
}; };
var updateContentScrollLeft = function (currentLeft, deltaX) { var updateContentScrollLeft = function (currentLeft, deltaX) {
@ -134,7 +143,12 @@
var scrollLeft = parseInt(scrollbarXLeft * (contentWidth - containerWidth) / (containerWidth - scrollbarXWidth), 10); var scrollLeft = parseInt(scrollbarXLeft * (contentWidth - containerWidth) / (containerWidth - scrollbarXWidth), 10);
$this.scrollLeft(scrollLeft); $this.scrollLeft(scrollLeft);
$scrollbarYRail.css({right: scrollbarYRight - scrollLeft});
if (isScrollbarYUsingRight) {
$scrollbarYRail.css({right: scrollbarYRight - scrollLeft});
} else {
$scrollbarYRail.css({left: scrollbarYLeft + scrollLeft});
}
}; };
var getSettingsAdjustedThumbSize = function (thumbSize) { var getSettingsAdjustedThumbSize = function (thumbSize) {
@ -145,8 +159,21 @@
}; };
var updateScrollbarCss = function () { var updateScrollbarCss = function () {
$scrollbarXRail.css({left: $this.scrollLeft(), bottom: scrollbarXBottom - $this.scrollTop(), width: containerWidth, display: scrollbarXActive ? "inherit": "none"}); var scrollbarXStyles = {left: $this.scrollLeft(), width: containerWidth, display: scrollbarXActive ? "inherit": "none"};
$scrollbarYRail.css({top: $this.scrollTop(), right: scrollbarYRight - $this.scrollLeft(), height: containerHeight, display: scrollbarYActive ? "inherit": "none"}); if (isScrollbarXUsingBottom) {
scrollbarXStyles.bottom = scrollbarXBottom - $this.scrollTop();
} else {
scrollbarXStyles.top = scrollbarXTop + $this.scrollTop();
}
$scrollbarXRail.css(scrollbarXStyles);
var scrollbarYStyles = {top: $this.scrollTop(), height: containerHeight, display: scrollbarYActive ? "inherit": "none"};
if (isScrollbarYUsingRight) {
scrollbarYStyles.right = scrollbarYRight - $this.scrollLeft();
} else {
scrollbarYStyles.left = scrollbarYLeft + $this.scrollLeft();
}
$scrollbarYRail.css(scrollbarYStyles);
$scrollbarX.css({left: scrollbarXLeft, width: scrollbarXWidth}); $scrollbarX.css({left: scrollbarXLeft, width: scrollbarXWidth});
$scrollbarY.css({top: scrollbarYTop, height: scrollbarYHeight}); $scrollbarY.css({top: scrollbarYTop, height: scrollbarYHeight});
}; };
@ -557,8 +584,22 @@
var fixIe6ScrollbarPosition = function () { var fixIe6ScrollbarPosition = function () {
updateScrollbarCss = function () { updateScrollbarCss = function () {
$scrollbarX.css({left: scrollbarXLeft + $this.scrollLeft(), bottom: scrollbarXBottom, width: scrollbarXWidth}); var scrollbarXStyles = {left: scrollbarXLeft + $this.scrollLeft(), width: scrollbarXWidth};
$scrollbarY.css({top: scrollbarYTop + $this.scrollTop(), right: scrollbarYRight, height: scrollbarYHeight}); if (isScrollbarXUsingBottom) {
scrollbarXStyles.bottom = scrollbarXBottom;
} else {
scrollbarXStyles.top = scrollbarXTop;
}
$scrollbarX.css(scrollbarXStyles);
var scrollbarYStyles = {top: scrollbarYTop + $this.scrollTop(), height: scrollbarYHeight};
if (isScrollbarYUsingRight) {
scrollbarYStyles.right = scrollbarYRight;
} else {
scrollbarYStyles.left = scrollbarYLeft;
}
$scrollbarY.css(scrollbarYStyles);
$scrollbarX.hide().show(); $scrollbarX.hide().show();
$scrollbarY.hide().show(); $scrollbarY.hide().show();
}; };