64 lines
2.1 KiB
JavaScript
64 lines
2.1 KiB
JavaScript
/* Copyright (c) 2015 Hyunje Alex Jun and other contributors
|
|
* Licensed under the MIT License
|
|
*/
|
|
'use strict';
|
|
|
|
var h = require('../../lib/helper')
|
|
, instances = require('../instances')
|
|
, updateGeometry = require('../update-geometry')
|
|
, updateScroll = require('../update-scroll');
|
|
|
|
function bindClickRailHandler(element, i) {
|
|
function pageOffset(el) {
|
|
return el.getBoundingClientRect();
|
|
}
|
|
var stopPropagation = window.Event.prototype.stopPropagation.bind;
|
|
|
|
if (i.settings.stopPropagationOnClick) {
|
|
i.event.bind(i.scrollbarY, 'click', stopPropagation);
|
|
}
|
|
i.event.bind(i.scrollbarYRail, 'click', function (e) {
|
|
var halfOfScrollbarLength = h.toInt(i.scrollbarYHeight / 2);
|
|
var positionTop = i.railYRatio * (e.pageY - window.scrollY - pageOffset(i.scrollbarYRail).top - halfOfScrollbarLength);
|
|
var maxPositionTop = i.railYRatio * (i.railYHeight - i.scrollbarYHeight);
|
|
var positionRatio = positionTop / maxPositionTop;
|
|
|
|
if (positionRatio < 0) {
|
|
positionRatio = 0;
|
|
} else if (positionRatio > 1) {
|
|
positionRatio = 1;
|
|
}
|
|
|
|
updateScroll(element, 'top', (i.contentHeight - i.containerHeight) * positionRatio);
|
|
updateGeometry(element);
|
|
|
|
e.stopPropagation();
|
|
});
|
|
|
|
if (i.settings.stopPropagationOnClick) {
|
|
i.event.bind(i.scrollbarX, 'click', stopPropagation);
|
|
}
|
|
i.event.bind(i.scrollbarXRail, 'click', function (e) {
|
|
var halfOfScrollbarLength = h.toInt(i.scrollbarXWidth / 2);
|
|
var positionLeft = i.railXRatio * (e.pageX - window.scrollX - pageOffset(i.scrollbarXRail).left - halfOfScrollbarLength);
|
|
var maxPositionLeft = i.railXRatio * (i.railXWidth - i.scrollbarXWidth);
|
|
var positionRatio = positionLeft / maxPositionLeft;
|
|
|
|
if (positionRatio < 0) {
|
|
positionRatio = 0;
|
|
} else if (positionRatio > 1) {
|
|
positionRatio = 1;
|
|
}
|
|
|
|
updateScroll(element, 'left', ((i.contentWidth - i.containerWidth) * positionRatio) - i.negativeScrollAdjustment);
|
|
updateGeometry(element);
|
|
|
|
e.stopPropagation();
|
|
});
|
|
}
|
|
|
|
module.exports = function (element) {
|
|
var i = instances.get(element);
|
|
bindClickRailHandler(element, i);
|
|
};
|