perfect RTL support

This commit is contained in:
ahspw 2015-05-08 14:06:55 +03:00 committed by Hyunje Alex Jun
parent db855ceca1
commit 3dbf20a5d0
6 changed files with 19 additions and 6 deletions

View File

@ -46,6 +46,7 @@ I hope you love it!
to update the size or position of the scrollbar, just update. to update the size or position of the scrollbar, just update.
* Additionally, perfect-scrollbar uses 'scrollTop' and 'scrollLeft', * Additionally, perfect-scrollbar uses 'scrollTop' and 'scrollLeft',
not absolute positioning or something messy. not absolute positioning or something messy.
* perfect-scrollbar supports RTL perfectly on both WebKit and Gecko based browsers.
It's cool, isn't it? It's cool, isn't it?

View File

@ -45,7 +45,7 @@ function bindClickRailHandler(element, i) {
positionRatio = 1; positionRatio = 1;
} }
element.scrollLeft = (i.contentWidth - i.containerWidth) * positionRatio; element.scrollLeft = ((i.contentWidth - i.containerWidth) * positionRatio) - i.negativeScrollAdjustment;
updateGeometry(element); updateGeometry(element);
e.stopPropagation(); e.stopPropagation();

View File

@ -24,7 +24,7 @@ function bindMouseScrollXHandler(element, i) {
i.scrollbarXLeft = newLeft; i.scrollbarXLeft = newLeft;
} }
var scrollLeft = h.toInt(i.scrollbarXLeft * (i.contentWidth - i.containerWidth) / (i.containerWidth - (i.railXRatio * i.scrollbarXWidth))); var scrollLeft = h.toInt(i.scrollbarXLeft * (i.contentWidth - i.containerWidth) / (i.containerWidth - (i.railXRatio * i.scrollbarXWidth))) - i.negativeScrollAdjustment;
element.scrollLeft = scrollLeft; element.scrollLeft = scrollLeft;
} }

View File

@ -21,6 +21,15 @@ function Instance(element) {
i.contentHeight = null; i.contentHeight = null;
i.isRtl = d.css(element, 'direction') === "rtl"; i.isRtl = d.css(element, 'direction') === "rtl";
i.isNegativeScroll = (function () {
var originalScrollLeft = element.scrollLeft;
var result = null;
element.scrollLeft = -1;
result = element.scrollLeft < 0;
element.scrollLeft = originalScrollLeft;
return result;
})();
i.negativeScrollAdjustment = i.isNegativeScroll ? element.scrollWidth - element.clientWidth : 0;
i.event = new EventManager(); i.event = new EventManager();
i.ownerDocument = element.ownerDocument || document; i.ownerDocument = element.ownerDocument || document;

View File

@ -21,7 +21,7 @@ function getThumbSize(i, thumbSize) {
function updateCss(element, i) { function updateCss(element, i) {
var xRailOffset = {width: i.railXWidth}; var xRailOffset = {width: i.railXWidth};
if (i.isRtl) { if (i.isRtl) {
xRailOffset.left = element.scrollLeft + i.containerWidth - i.contentWidth; xRailOffset.left = i.negativeScrollAdjustment + element.scrollLeft + i.containerWidth - i.contentWidth;
} else { } else {
xRailOffset.left = element.scrollLeft; xRailOffset.left = element.scrollLeft;
} }
@ -35,13 +35,13 @@ function updateCss(element, i) {
var yRailOffset = {top: element.scrollTop, height: i.railYHeight}; var yRailOffset = {top: element.scrollTop, height: i.railYHeight};
if (i.isScrollbarYUsingRight) { if (i.isScrollbarYUsingRight) {
if (i.isRtl) { if (i.isRtl) {
yRailOffset.right = i.contentWidth - element.scrollLeft - i.scrollbarYRight - i.scrollbarYOuterWidth; yRailOffset.right = i.contentWidth - (i.negativeScrollAdjustment + element.scrollLeft) - i.scrollbarYRight - i.scrollbarYOuterWidth;
} else { } else {
yRailOffset.right = i.scrollbarYRight - element.scrollLeft; yRailOffset.right = i.scrollbarYRight - element.scrollLeft;
} }
} else { } else {
if (i.isRtl) { if (i.isRtl) {
yRailOffset.left = element.scrollLeft + i.containerWidth * 2 - i.contentWidth - i.scrollbarYLeft - i.scrollbarYOuterWidth; yRailOffset.left = i.negativeScrollAdjustment + element.scrollLeft + i.containerWidth * 2 - i.contentWidth - i.scrollbarYLeft - i.scrollbarYOuterWidth;
} else { } else {
yRailOffset.left = i.scrollbarYLeft + element.scrollLeft; yRailOffset.left = i.scrollbarYLeft + element.scrollLeft;
} }
@ -72,7 +72,7 @@ module.exports = function (element) {
i.railXWidth = i.containerWidth - i.railXMarginWidth; i.railXWidth = i.containerWidth - i.railXMarginWidth;
i.railXRatio = i.containerWidth / i.railXWidth; i.railXRatio = i.containerWidth / i.railXWidth;
i.scrollbarXWidth = getThumbSize(i, h.toInt(i.railXWidth * i.containerWidth / i.contentWidth)); i.scrollbarXWidth = getThumbSize(i, h.toInt(i.railXWidth * i.containerWidth / i.contentWidth));
i.scrollbarXLeft = h.toInt(element.scrollLeft * (i.railXWidth - i.scrollbarXWidth) / (i.contentWidth - i.containerWidth)); i.scrollbarXLeft = h.toInt((i.negativeScrollAdjustment + element.scrollLeft) * (i.railXWidth - i.scrollbarXWidth) / (i.contentWidth - i.containerWidth));
} else { } else {
i.scrollbarXActive = false; i.scrollbarXActive = false;
i.scrollbarXWidth = 0; i.scrollbarXWidth = 0;

View File

@ -11,6 +11,9 @@ var d = require('../lib/dom')
module.exports = function (element) { module.exports = function (element) {
var i = instances.get(element); var i = instances.get(element);
// Recalcuate negative scrollLeft adjustment
i.negativeScrollAdjustment = i.isNegativeScroll ? element.scrollWidth - element.clientWidth : 0;
// Recalculate rail margins // Recalculate rail margins
d.css(i.scrollbarXRail, 'display', 'block'); d.css(i.scrollbarXRail, 'display', 'block');
d.css(i.scrollbarYRail, 'display', 'block'); d.css(i.scrollbarYRail, 'display', 'block');