2012-11-06 21:00:37 +08:00
|
|
|
/* Copyright (c) 2012 HyeonJe Jun (http://github.com/noraesae)
|
|
|
|
* Licensed under the MIT License
|
|
|
|
*/
|
2013-08-31 01:24:43 +08:00
|
|
|
'use strict';
|
2013-09-20 17:04:29 +08:00
|
|
|
(function (factory) {
|
|
|
|
if (typeof define === 'function' && define.amd) {
|
|
|
|
// AMD. Register as an anonymous module.
|
|
|
|
define(['jquery'], factory);
|
|
|
|
} else {
|
|
|
|
// Browser globals
|
|
|
|
factory(jQuery);
|
|
|
|
}
|
|
|
|
}(function ($) {
|
2013-05-18 11:41:15 +08:00
|
|
|
|
|
|
|
// The default settings for the plugin
|
|
|
|
var defaultSettings = {
|
|
|
|
wheelSpeed: 10,
|
2013-07-18 00:53:02 +08:00
|
|
|
wheelPropagation: false,
|
2013-09-28 06:53:57 +08:00
|
|
|
minScrollbarLength: null,
|
2013-09-30 16:24:12 +08:00
|
|
|
useBothWheelAxes: false,
|
2013-11-07 04:20:30 +08:00
|
|
|
useKeyboard: true,
|
|
|
|
suppressScrollX: false,
|
|
|
|
suppressScrollY: false,
|
|
|
|
scrollXMarginOffset: 0,
|
|
|
|
scrollYMarginOffset: 0
|
2013-05-18 11:41:15 +08:00
|
|
|
};
|
|
|
|
|
2014-01-21 11:22:04 +08:00
|
|
|
var getEventClassName = (function () {
|
|
|
|
var incrementingId = 0;
|
|
|
|
return function () {
|
|
|
|
var id = incrementingId;
|
|
|
|
incrementingId += 1;
|
|
|
|
return '.perfect-scrollbar-' + id;
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
2013-05-18 11:41:15 +08:00
|
|
|
$.fn.perfectScrollbar = function (suppliedSettings, option) {
|
|
|
|
|
2013-06-14 20:38:33 +08:00
|
|
|
return this.each(function () {
|
2013-05-22 17:48:51 +08:00
|
|
|
// Use the default settings
|
2013-08-05 22:59:17 +08:00
|
|
|
var settings = $.extend(true, {}, defaultSettings),
|
|
|
|
$this = $(this);
|
|
|
|
|
2013-05-22 17:48:51 +08:00
|
|
|
if (typeof suppliedSettings === "object") {
|
|
|
|
// But over-ride any supplied
|
|
|
|
$.extend(true, settings, suppliedSettings);
|
|
|
|
} else {
|
|
|
|
// If no settings were supplied, then the first param must be the option
|
|
|
|
option = suppliedSettings;
|
2013-05-18 11:41:15 +08:00
|
|
|
}
|
|
|
|
|
2013-08-05 22:59:17 +08:00
|
|
|
// Catch options
|
|
|
|
|
2013-05-22 17:48:51 +08:00
|
|
|
if (option === 'update') {
|
2013-08-05 22:59:17 +08:00
|
|
|
if ($this.data('perfect-scrollbar-update')) {
|
|
|
|
$this.data('perfect-scrollbar-update')();
|
2013-05-22 17:48:51 +08:00
|
|
|
}
|
2013-08-05 22:59:17 +08:00
|
|
|
return $this;
|
2013-05-18 11:41:15 +08:00
|
|
|
}
|
2013-05-22 17:48:51 +08:00
|
|
|
else if (option === 'destroy') {
|
2013-08-05 22:59:17 +08:00
|
|
|
if ($this.data('perfect-scrollbar-destroy')) {
|
|
|
|
$this.data('perfect-scrollbar-destroy')();
|
2013-05-22 17:48:51 +08:00
|
|
|
}
|
2013-08-05 22:59:17 +08:00
|
|
|
return $this;
|
2013-05-18 11:41:15 +08:00
|
|
|
}
|
|
|
|
|
2013-08-05 22:59:17 +08:00
|
|
|
if ($this.data('perfect-scrollbar')) {
|
2013-05-22 17:48:51 +08:00
|
|
|
// if there's already perfect-scrollbar
|
2013-08-05 22:59:17 +08:00
|
|
|
return $this.data('perfect-scrollbar');
|
2013-05-18 11:41:15 +08:00
|
|
|
}
|
|
|
|
|
2013-08-05 22:59:17 +08:00
|
|
|
|
|
|
|
// Or generate new perfectScrollbar
|
|
|
|
|
|
|
|
// Set class to the container
|
|
|
|
$this.addClass('ps-container');
|
|
|
|
|
2013-09-30 20:51:07 +08:00
|
|
|
var $scrollbarXRail = $("<div class='ps-scrollbar-x-rail'></div>").appendTo($this),
|
|
|
|
$scrollbarYRail = $("<div class='ps-scrollbar-y-rail'></div>").appendTo($this),
|
|
|
|
$scrollbarX = $("<div class='ps-scrollbar-x'></div>").appendTo($scrollbarXRail),
|
|
|
|
$scrollbarY = $("<div class='ps-scrollbar-y'></div>").appendTo($scrollbarYRail),
|
2013-09-28 06:53:57 +08:00
|
|
|
scrollbarXActive,
|
|
|
|
scrollbarYActive,
|
2013-05-22 17:48:51 +08:00
|
|
|
containerWidth,
|
|
|
|
containerHeight,
|
|
|
|
contentWidth,
|
|
|
|
contentHeight,
|
|
|
|
scrollbarXWidth,
|
|
|
|
scrollbarXLeft,
|
2013-09-30 20:51:07 +08:00
|
|
|
scrollbarXBottom = parseInt($scrollbarXRail.css('bottom'), 10),
|
2013-05-22 17:48:51 +08:00
|
|
|
scrollbarYHeight,
|
|
|
|
scrollbarYTop,
|
2014-01-21 11:22:04 +08:00
|
|
|
scrollbarYRight = parseInt($scrollbarYRail.css('right'), 10),
|
|
|
|
eventClassName = getEventClassName();
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-01-14 23:23:21 +08:00
|
|
|
var updateContentScrollTop = function (currentTop, deltaY) {
|
|
|
|
var newTop = currentTop + deltaY,
|
|
|
|
maxTop = containerHeight - scrollbarYHeight;
|
|
|
|
|
|
|
|
if (newTop < 0) {
|
|
|
|
scrollbarYTop = 0;
|
|
|
|
}
|
|
|
|
else if (newTop > maxTop) {
|
|
|
|
scrollbarYTop = maxTop;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
scrollbarYTop = newTop;
|
|
|
|
}
|
|
|
|
|
2013-07-31 23:59:23 +08:00
|
|
|
var scrollTop = parseInt(scrollbarYTop * (contentHeight - containerHeight) / (containerHeight - scrollbarYHeight), 10);
|
2013-05-22 17:48:51 +08:00
|
|
|
$this.scrollTop(scrollTop);
|
2013-09-30 20:51:07 +08:00
|
|
|
$scrollbarXRail.css({bottom: scrollbarXBottom - scrollTop});
|
2013-05-22 17:48:51 +08:00
|
|
|
};
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2014-01-14 23:23:21 +08:00
|
|
|
var updateContentScrollLeft = function (currentLeft, deltaX) {
|
|
|
|
var newLeft = currentLeft + deltaX,
|
|
|
|
maxLeft = containerWidth - scrollbarXWidth;
|
|
|
|
|
|
|
|
if (newLeft < 0) {
|
|
|
|
scrollbarXLeft = 0;
|
|
|
|
}
|
|
|
|
else if (newLeft > maxLeft) {
|
|
|
|
scrollbarXLeft = maxLeft;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
scrollbarXLeft = newLeft;
|
|
|
|
}
|
|
|
|
|
2013-07-31 23:59:23 +08:00
|
|
|
var scrollLeft = parseInt(scrollbarXLeft * (contentWidth - containerWidth) / (containerWidth - scrollbarXWidth), 10);
|
2013-05-22 17:48:51 +08:00
|
|
|
$this.scrollLeft(scrollLeft);
|
2013-09-30 20:51:07 +08:00
|
|
|
$scrollbarYRail.css({right: scrollbarYRight - scrollLeft});
|
2013-05-22 17:48:51 +08:00
|
|
|
};
|
|
|
|
|
2013-07-18 00:53:02 +08:00
|
|
|
var getSettingsAdjustedThumbSize = function (thumbSize) {
|
|
|
|
if (settings.minScrollbarLength) {
|
|
|
|
thumbSize = Math.max(thumbSize, settings.minScrollbarLength);
|
|
|
|
}
|
|
|
|
return thumbSize;
|
|
|
|
};
|
|
|
|
|
2013-06-16 16:45:37 +08:00
|
|
|
var updateScrollbarCss = function () {
|
2014-01-22 10:14:01 +08:00
|
|
|
$scrollbarXRail.css({left: $this.scrollLeft(), bottom: scrollbarXBottom - $this.scrollTop(), width: containerWidth, display: scrollbarXActive ? "inherit": "none"});
|
|
|
|
$scrollbarYRail.css({top: $this.scrollTop(), right: scrollbarYRight - $this.scrollLeft(), height: containerHeight, display: scrollbarYActive ? "inherit": "none"});
|
2013-09-30 20:51:07 +08:00
|
|
|
$scrollbarX.css({left: scrollbarXLeft, width: scrollbarXWidth});
|
|
|
|
$scrollbarY.css({top: scrollbarYTop, height: scrollbarYHeight});
|
2013-06-16 16:45:37 +08:00
|
|
|
};
|
|
|
|
|
2013-05-22 17:48:51 +08:00
|
|
|
var updateBarSizeAndPosition = function () {
|
|
|
|
containerWidth = $this.width();
|
|
|
|
containerHeight = $this.height();
|
2013-06-14 00:08:22 +08:00
|
|
|
contentWidth = $this.prop('scrollWidth');
|
|
|
|
contentHeight = $this.prop('scrollHeight');
|
2013-08-05 22:59:17 +08:00
|
|
|
|
2013-11-07 04:20:30 +08:00
|
|
|
if (!settings.suppressScrollX && containerWidth + settings.scrollXMarginOffset < contentWidth) {
|
2013-09-28 06:53:57 +08:00
|
|
|
scrollbarXActive = true;
|
2013-07-18 00:53:02 +08:00
|
|
|
scrollbarXWidth = getSettingsAdjustedThumbSize(parseInt(containerWidth * containerWidth / contentWidth, 10));
|
2013-07-31 23:59:23 +08:00
|
|
|
scrollbarXLeft = parseInt($this.scrollLeft() * (containerWidth - scrollbarXWidth) / (contentWidth - containerWidth), 10);
|
2013-05-22 17:48:51 +08:00
|
|
|
}
|
|
|
|
else {
|
2013-09-28 06:53:57 +08:00
|
|
|
scrollbarXActive = false;
|
2013-05-22 17:48:51 +08:00
|
|
|
scrollbarXWidth = 0;
|
|
|
|
scrollbarXLeft = 0;
|
|
|
|
$this.scrollLeft(0);
|
|
|
|
}
|
2013-08-05 22:59:17 +08:00
|
|
|
|
2013-11-07 04:20:30 +08:00
|
|
|
if (!settings.suppressScrollY && containerHeight + settings.scrollYMarginOffset < contentHeight) {
|
2013-09-28 06:53:57 +08:00
|
|
|
scrollbarYActive = true;
|
2013-07-18 00:53:02 +08:00
|
|
|
scrollbarYHeight = getSettingsAdjustedThumbSize(parseInt(containerHeight * containerHeight / contentHeight, 10));
|
2013-07-31 23:59:23 +08:00
|
|
|
scrollbarYTop = parseInt($this.scrollTop() * (containerHeight - scrollbarYHeight) / (contentHeight - containerHeight), 10);
|
2013-05-22 17:48:51 +08:00
|
|
|
}
|
|
|
|
else {
|
2013-09-28 06:53:57 +08:00
|
|
|
scrollbarYActive = false;
|
2013-05-22 17:48:51 +08:00
|
|
|
scrollbarYHeight = 0;
|
|
|
|
scrollbarYTop = 0;
|
|
|
|
$this.scrollTop(0);
|
2013-02-26 09:32:19 +08:00
|
|
|
}
|
|
|
|
|
2013-05-22 17:48:51 +08:00
|
|
|
if (scrollbarYTop >= containerHeight - scrollbarYHeight) {
|
|
|
|
scrollbarYTop = containerHeight - scrollbarYHeight;
|
2012-11-10 02:07:47 +08:00
|
|
|
}
|
2013-05-22 17:48:51 +08:00
|
|
|
if (scrollbarXLeft >= containerWidth - scrollbarXWidth) {
|
|
|
|
scrollbarXLeft = containerWidth - scrollbarXWidth;
|
2012-11-10 02:07:47 +08:00
|
|
|
}
|
|
|
|
|
2013-06-16 16:45:37 +08:00
|
|
|
updateScrollbarCss();
|
2013-05-22 17:48:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
var bindMouseScrollXHandler = function () {
|
|
|
|
var currentLeft,
|
|
|
|
currentPageX;
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2014-01-21 11:22:04 +08:00
|
|
|
$scrollbarX.bind('mousedown' + eventClassName, function (e) {
|
2013-05-22 17:48:51 +08:00
|
|
|
currentPageX = e.pageX;
|
|
|
|
currentLeft = $scrollbarX.position().left;
|
2013-09-30 20:51:07 +08:00
|
|
|
$scrollbarXRail.addClass('in-scrolling');
|
2013-05-22 17:48:51 +08:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
2014-01-21 11:22:04 +08:00
|
|
|
$(document).bind('mousemove' + eventClassName, function (e) {
|
2013-09-30 20:51:07 +08:00
|
|
|
if ($scrollbarXRail.hasClass('in-scrolling')) {
|
2014-01-14 23:23:21 +08:00
|
|
|
updateContentScrollLeft(currentLeft, e.pageX - currentPageX);
|
2013-05-22 17:48:51 +08:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-01-21 11:22:04 +08:00
|
|
|
$(document).bind('mouseup' + eventClassName, function (e) {
|
2013-09-30 20:51:07 +08:00
|
|
|
if ($scrollbarXRail.hasClass('in-scrolling')) {
|
|
|
|
$scrollbarXRail.removeClass('in-scrolling');
|
2013-05-22 17:48:51 +08:00
|
|
|
}
|
|
|
|
});
|
2013-08-05 22:59:17 +08:00
|
|
|
|
|
|
|
currentLeft =
|
|
|
|
currentPageX = null;
|
2013-05-22 17:48:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
var bindMouseScrollYHandler = function () {
|
|
|
|
var currentTop,
|
|
|
|
currentPageY;
|
2012-11-10 02:07:47 +08:00
|
|
|
|
2014-01-21 11:22:04 +08:00
|
|
|
$scrollbarY.bind('mousedown' + eventClassName, function (e) {
|
2013-05-22 17:48:51 +08:00
|
|
|
currentPageY = e.pageY;
|
|
|
|
currentTop = $scrollbarY.position().top;
|
2013-09-30 20:51:07 +08:00
|
|
|
$scrollbarYRail.addClass('in-scrolling');
|
2013-05-22 17:48:51 +08:00
|
|
|
e.stopPropagation();
|
2013-05-18 11:41:15 +08:00
|
|
|
e.preventDefault();
|
2013-05-22 17:48:51 +08:00
|
|
|
});
|
|
|
|
|
2014-01-21 11:22:04 +08:00
|
|
|
$(document).bind('mousemove' + eventClassName, function (e) {
|
2013-09-30 20:51:07 +08:00
|
|
|
if ($scrollbarYRail.hasClass('in-scrolling')) {
|
2014-01-14 23:23:21 +08:00
|
|
|
updateContentScrollTop(currentTop, e.pageY - currentPageY);
|
2013-05-22 17:48:51 +08:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2014-01-21 11:22:04 +08:00
|
|
|
$(document).bind('mouseup' + eventClassName, function (e) {
|
2013-09-30 20:51:07 +08:00
|
|
|
if ($scrollbarYRail.hasClass('in-scrolling')) {
|
|
|
|
$scrollbarYRail.removeClass('in-scrolling');
|
2013-05-22 17:48:51 +08:00
|
|
|
}
|
|
|
|
});
|
2013-08-05 22:59:17 +08:00
|
|
|
|
|
|
|
currentTop =
|
|
|
|
currentPageY = null;
|
2013-05-22 17:48:51 +08:00
|
|
|
};
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2013-12-14 15:25:36 +08:00
|
|
|
// check if the default scrolling should be prevented.
|
|
|
|
var shouldPreventDefault = function (deltaX, deltaY) {
|
|
|
|
var scrollTop = $this.scrollTop();
|
|
|
|
if (deltaX === 0) {
|
|
|
|
if (!scrollbarYActive) {
|
|
|
|
return false;
|
2013-05-22 17:48:51 +08:00
|
|
|
}
|
2013-12-14 15:25:36 +08:00
|
|
|
if ((scrollTop === 0 && deltaY > 0) || (scrollTop >= contentHeight - containerHeight && deltaY < 0)) {
|
2013-05-22 17:48:51 +08:00
|
|
|
return !settings.wheelPropagation;
|
|
|
|
}
|
2013-12-14 15:25:36 +08:00
|
|
|
}
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2013-12-14 15:25:36 +08:00
|
|
|
var scrollLeft = $this.scrollLeft();
|
|
|
|
if (deltaY === 0) {
|
|
|
|
if (!scrollbarXActive) {
|
|
|
|
return false;
|
2013-05-22 17:48:51 +08:00
|
|
|
}
|
2013-12-14 15:25:36 +08:00
|
|
|
if ((scrollLeft === 0 && deltaX < 0) || (scrollLeft >= contentWidth - containerWidth && deltaX > 0)) {
|
2013-05-22 17:48:51 +08:00
|
|
|
return !settings.wheelPropagation;
|
|
|
|
}
|
2013-12-14 15:25:36 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2013-12-14 15:25:36 +08:00
|
|
|
// bind handlers
|
|
|
|
var bindMouseWheelHandler = function () {
|
2013-08-06 14:14:55 +08:00
|
|
|
var shouldPrevent = false;
|
2014-01-21 11:22:04 +08:00
|
|
|
$this.bind('mousewheel' + eventClassName, function (e, delta, deltaX, deltaY) {
|
2013-09-28 06:53:57 +08:00
|
|
|
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.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));
|
|
|
|
}
|
|
|
|
}
|
2013-05-22 17:48:51 +08:00
|
|
|
|
|
|
|
// update bar position
|
|
|
|
updateBarSizeAndPosition();
|
|
|
|
|
2013-08-06 14:14:55 +08:00
|
|
|
shouldPrevent = shouldPreventDefault(deltaX, deltaY);
|
|
|
|
if (shouldPrevent) {
|
2013-05-22 17:48:51 +08:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2013-08-01 01:01:42 +08:00
|
|
|
|
|
|
|
// fix Firefox scroll problem
|
2014-01-21 11:22:04 +08:00
|
|
|
$this.bind('MozMousePixelScroll' + eventClassName, function (e) {
|
2013-08-06 14:14:55 +08:00
|
|
|
if (shouldPrevent) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2013-05-18 11:41:15 +08:00
|
|
|
};
|
|
|
|
|
2013-09-30 16:24:12 +08:00
|
|
|
var bindKeyboardHandler = function () {
|
|
|
|
var hovered = false;
|
2014-01-21 11:22:04 +08:00
|
|
|
$this.bind('mouseenter' + eventClassName, function (e) {
|
2013-09-30 16:24:12 +08:00
|
|
|
hovered = true;
|
|
|
|
});
|
2014-01-21 11:22:04 +08:00
|
|
|
$this.bind('mouseleave' + eventClassName, function (e) {
|
2013-09-30 16:24:12 +08:00
|
|
|
hovered = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
var shouldPrevent = false;
|
2014-01-21 11:22:04 +08:00
|
|
|
$(document).bind('keydown' + eventClassName, function (e) {
|
2013-09-30 16:24:12 +08:00
|
|
|
if (!hovered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var deltaX = 0,
|
|
|
|
deltaY = 0;
|
|
|
|
|
|
|
|
switch (e.which) {
|
|
|
|
case 37: // left
|
|
|
|
deltaX = -3;
|
|
|
|
break;
|
|
|
|
case 38: // up
|
|
|
|
deltaY = 3;
|
|
|
|
break;
|
|
|
|
case 39: // right
|
|
|
|
deltaX = 3;
|
|
|
|
break;
|
|
|
|
case 40: // down
|
|
|
|
deltaY = -3;
|
|
|
|
break;
|
2014-01-21 06:11:42 +08:00
|
|
|
case 33: // page up
|
|
|
|
deltaY = 9;
|
|
|
|
break;
|
|
|
|
case 32: // space bar
|
|
|
|
case 34: // page down
|
|
|
|
deltaY = -9;
|
|
|
|
break;
|
|
|
|
case 35: // end
|
|
|
|
deltaY = -containerHeight;
|
|
|
|
break;
|
|
|
|
case 36: // home
|
|
|
|
deltaY = containerHeight;
|
|
|
|
break;
|
2013-09-30 16:24:12 +08:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this.scrollTop($this.scrollTop() - (deltaY * settings.wheelSpeed));
|
|
|
|
$this.scrollLeft($this.scrollLeft() + (deltaX * settings.wheelSpeed));
|
|
|
|
|
|
|
|
shouldPrevent = shouldPreventDefault(deltaX, deltaY);
|
|
|
|
if (shouldPrevent) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-09-30 22:32:15 +08:00
|
|
|
var bindRailClickHandler = function () {
|
|
|
|
var stopPropagation = function (e) { e.stopPropagation(); };
|
|
|
|
|
2014-01-21 11:22:04 +08:00
|
|
|
$scrollbarY.bind('click' + eventClassName, stopPropagation);
|
|
|
|
$scrollbarYRail.bind('click' + eventClassName, function (e) {
|
2013-09-30 22:32:15 +08:00
|
|
|
var halfOfScrollbarLength = parseInt(scrollbarYHeight / 2, 10),
|
|
|
|
positionTop = e.pageY - $scrollbarYRail.offset().top - halfOfScrollbarLength,
|
|
|
|
maxPositionTop = containerHeight - scrollbarYHeight,
|
|
|
|
positionRatio = positionTop / maxPositionTop;
|
|
|
|
|
|
|
|
if (positionRatio < 0) {
|
|
|
|
positionRatio = 0;
|
|
|
|
} else if (positionRatio > 1) {
|
|
|
|
positionRatio = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this.scrollTop((contentHeight - containerHeight) * positionRatio);
|
|
|
|
});
|
|
|
|
|
2014-01-21 11:22:04 +08:00
|
|
|
$scrollbarX.bind('click' + eventClassName, stopPropagation);
|
|
|
|
$scrollbarXRail.bind('click' + eventClassName, function (e) {
|
2013-09-30 22:32:15 +08:00
|
|
|
var halfOfScrollbarLength = parseInt(scrollbarXWidth / 2, 10),
|
|
|
|
positionLeft = e.pageX - $scrollbarXRail.offset().left - halfOfScrollbarLength,
|
|
|
|
maxPositionLeft = containerWidth - scrollbarXWidth,
|
|
|
|
positionRatio = positionLeft / maxPositionLeft;
|
|
|
|
|
|
|
|
if (positionRatio < 0) {
|
|
|
|
positionRatio = 0;
|
|
|
|
} else if (positionRatio > 1) {
|
|
|
|
positionRatio = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this.scrollLeft((contentWidth - containerWidth) * positionRatio);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-05-22 17:48:51 +08:00
|
|
|
// bind mobile touch handler
|
|
|
|
var bindMobileTouchHandler = function () {
|
|
|
|
var applyTouchMove = function (differenceX, differenceY) {
|
|
|
|
$this.scrollTop($this.scrollTop() - differenceY);
|
|
|
|
$this.scrollLeft($this.scrollLeft() - differenceX);
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2013-05-22 17:48:51 +08:00
|
|
|
// update bar position
|
|
|
|
updateBarSizeAndPosition();
|
|
|
|
};
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2013-05-22 17:48:51 +08:00
|
|
|
var startCoords = {},
|
|
|
|
startTime = 0,
|
|
|
|
speed = {},
|
2013-06-14 20:57:24 +08:00
|
|
|
breakingProcess = null,
|
|
|
|
inGlobalTouch = false;
|
|
|
|
|
2014-01-21 11:22:04 +08:00
|
|
|
$(window).bind("touchstart" + eventClassName, function (e) {
|
2013-06-14 20:57:24 +08:00
|
|
|
inGlobalTouch = true;
|
|
|
|
});
|
2014-01-21 11:22:04 +08:00
|
|
|
$(window).bind("touchend" + eventClassName, function (e) {
|
2013-06-14 20:57:24 +08:00
|
|
|
inGlobalTouch = false;
|
|
|
|
});
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2014-01-21 11:22:04 +08:00
|
|
|
$this.bind("touchstart" + eventClassName, function (e) {
|
2013-05-22 17:48:51 +08:00
|
|
|
var touch = e.originalEvent.targetTouches[0];
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2013-05-22 17:48:51 +08:00
|
|
|
startCoords.pageX = touch.pageX;
|
|
|
|
startCoords.pageY = touch.pageY;
|
|
|
|
|
|
|
|
startTime = (new Date()).getTime();
|
|
|
|
|
|
|
|
if (breakingProcess !== null) {
|
2013-05-18 11:41:15 +08:00
|
|
|
clearInterval(breakingProcess);
|
|
|
|
}
|
2013-06-14 20:57:24 +08:00
|
|
|
|
|
|
|
e.stopPropagation();
|
2013-05-22 17:48:51 +08:00
|
|
|
});
|
2014-01-21 11:22:04 +08:00
|
|
|
$this.bind("touchmove" + eventClassName, function (e) {
|
2013-06-14 20:57:24 +08:00
|
|
|
if (!inGlobalTouch && e.originalEvent.targetTouches.length === 1) {
|
|
|
|
var touch = e.originalEvent.targetTouches[0];
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2013-06-14 20:57:24 +08:00
|
|
|
var currentCoords = {};
|
|
|
|
currentCoords.pageX = touch.pageX;
|
|
|
|
currentCoords.pageY = touch.pageY;
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2013-06-14 20:57:24 +08:00
|
|
|
var differenceX = currentCoords.pageX - startCoords.pageX,
|
|
|
|
differenceY = currentCoords.pageY - startCoords.pageY;
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2013-06-14 20:57:24 +08:00
|
|
|
applyTouchMove(differenceX, differenceY);
|
|
|
|
startCoords = currentCoords;
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2013-06-14 20:57:24 +08:00
|
|
|
var currentTime = (new Date()).getTime();
|
|
|
|
speed.x = differenceX / (currentTime - startTime);
|
|
|
|
speed.y = differenceY / (currentTime - startTime);
|
|
|
|
startTime = currentTime;
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2013-06-14 20:57:24 +08:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
2013-05-22 17:48:51 +08:00
|
|
|
});
|
2014-01-21 11:22:04 +08:00
|
|
|
$this.bind("touchend" + eventClassName, function (e) {
|
2013-08-05 22:59:17 +08:00
|
|
|
clearInterval(breakingProcess);
|
2013-05-22 17:48:51 +08:00
|
|
|
breakingProcess = setInterval(function () {
|
|
|
|
if (Math.abs(speed.x) < 0.01 && Math.abs(speed.y) < 0.01) {
|
|
|
|
clearInterval(breakingProcess);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
applyTouchMove(speed.x * 30, speed.y * 30);
|
|
|
|
|
|
|
|
speed.x *= 0.8;
|
|
|
|
speed.y *= 0.8;
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-01-14 23:23:21 +08:00
|
|
|
var bindScrollHandler = function () {
|
2014-01-21 11:22:04 +08:00
|
|
|
$this.bind('scroll' + eventClassName, function (e) {
|
2014-01-14 23:23:21 +08:00
|
|
|
updateBarSizeAndPosition();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-05-22 17:48:51 +08:00
|
|
|
var destroy = function () {
|
2014-01-21 11:22:04 +08:00
|
|
|
$this.unbind(eventClassName);
|
|
|
|
$(window).unbind(eventClassName);
|
|
|
|
$(document).unbind(eventClassName);
|
2013-05-22 17:48:51 +08:00
|
|
|
$this.data('perfect-scrollbar', null);
|
|
|
|
$this.data('perfect-scrollbar-update', null);
|
|
|
|
$this.data('perfect-scrollbar-destroy', null);
|
2013-08-05 22:59:17 +08:00
|
|
|
$scrollbarX.remove();
|
|
|
|
$scrollbarY.remove();
|
2013-09-30 20:51:07 +08:00
|
|
|
$scrollbarXRail.remove();
|
|
|
|
$scrollbarYRail.remove();
|
2013-08-05 22:59:17 +08:00
|
|
|
|
|
|
|
// clean all variables
|
|
|
|
$scrollbarX =
|
|
|
|
$scrollbarY =
|
|
|
|
containerWidth =
|
|
|
|
containerHeight =
|
|
|
|
contentWidth =
|
|
|
|
contentHeight =
|
|
|
|
scrollbarXWidth =
|
|
|
|
scrollbarXLeft =
|
|
|
|
scrollbarXBottom =
|
|
|
|
scrollbarYHeight =
|
|
|
|
scrollbarYTop =
|
|
|
|
scrollbarYRight = null;
|
2013-05-22 17:48:51 +08:00
|
|
|
};
|
|
|
|
|
2013-06-11 23:44:36 +08:00
|
|
|
var ieSupport = function (version) {
|
|
|
|
$this.addClass('ie').addClass('ie' + version);
|
2013-06-11 23:44:36 +08:00
|
|
|
|
|
|
|
var bindHoverHandlers = function () {
|
|
|
|
var mouseenter = function () {
|
|
|
|
$(this).addClass('hover');
|
|
|
|
};
|
|
|
|
var mouseleave = function () {
|
|
|
|
$(this).removeClass('hover');
|
|
|
|
};
|
2014-01-21 11:22:04 +08:00
|
|
|
$this.bind('mouseenter' + eventClassName, mouseenter).bind('mouseleave' + eventClassName, mouseleave);
|
|
|
|
$scrollbarXRail.bind('mouseenter' + eventClassName, mouseenter).bind('mouseleave' + eventClassName, mouseleave);
|
|
|
|
$scrollbarYRail.bind('mouseenter' + eventClassName, mouseenter).bind('mouseleave' + eventClassName, mouseleave);
|
|
|
|
$scrollbarX.bind('mouseenter' + eventClassName, mouseenter).bind('mouseleave' + eventClassName, mouseleave);
|
|
|
|
$scrollbarY.bind('mouseenter' + eventClassName, mouseenter).bind('mouseleave' + eventClassName, mouseleave);
|
2013-06-11 23:44:36 +08:00
|
|
|
};
|
|
|
|
|
2013-06-16 16:56:40 +08:00
|
|
|
var fixIe6ScrollbarPosition = function () {
|
|
|
|
updateScrollbarCss = function () {
|
|
|
|
$scrollbarX.css({left: scrollbarXLeft + $this.scrollLeft(), bottom: scrollbarXBottom, width: scrollbarXWidth});
|
|
|
|
$scrollbarY.css({top: scrollbarYTop + $this.scrollTop(), right: scrollbarYRight, height: scrollbarYHeight});
|
|
|
|
$scrollbarX.hide().show();
|
|
|
|
$scrollbarY.hide().show();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-06-11 23:44:36 +08:00
|
|
|
if (version === 6) {
|
|
|
|
bindHoverHandlers();
|
2013-06-16 16:56:40 +08:00
|
|
|
fixIe6ScrollbarPosition();
|
2013-06-11 23:44:36 +08:00
|
|
|
}
|
2013-06-11 23:44:36 +08:00
|
|
|
};
|
|
|
|
|
2013-06-29 01:14:40 +08:00
|
|
|
var supportsTouch = (('ontouchstart' in window) || window.DocumentTouch && document instanceof window.DocumentTouch);
|
2013-05-22 17:48:51 +08:00
|
|
|
|
|
|
|
var initialize = function () {
|
2013-06-11 23:44:36 +08:00
|
|
|
var ieMatch = navigator.userAgent.toLowerCase().match(/(msie) ([\w.]+)/);
|
|
|
|
if (ieMatch && ieMatch[1] === 'msie') {
|
|
|
|
// must be executed at first, because 'ieSupport' may addClass to the container
|
|
|
|
ieSupport(parseInt(ieMatch[2], 10));
|
|
|
|
}
|
|
|
|
|
2013-05-22 17:48:51 +08:00
|
|
|
updateBarSizeAndPosition();
|
2014-01-14 23:23:21 +08:00
|
|
|
bindScrollHandler();
|
2013-05-22 17:48:51 +08:00
|
|
|
bindMouseScrollXHandler();
|
|
|
|
bindMouseScrollYHandler();
|
2013-09-30 22:32:15 +08:00
|
|
|
bindRailClickHandler();
|
2013-06-29 01:14:40 +08:00
|
|
|
if (supportsTouch) {
|
2013-05-22 17:48:51 +08:00
|
|
|
bindMobileTouchHandler();
|
|
|
|
}
|
|
|
|
if ($this.mousewheel) {
|
|
|
|
bindMouseWheelHandler();
|
|
|
|
}
|
2013-09-30 16:24:12 +08:00
|
|
|
if (settings.useKeyboard) {
|
|
|
|
bindKeyboardHandler();
|
|
|
|
}
|
2013-05-22 17:48:51 +08:00
|
|
|
$this.data('perfect-scrollbar', $this);
|
|
|
|
$this.data('perfect-scrollbar-update', updateBarSizeAndPosition);
|
|
|
|
$this.data('perfect-scrollbar-destroy', destroy);
|
|
|
|
};
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2013-05-22 17:48:51 +08:00
|
|
|
// initialize
|
|
|
|
initialize();
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2013-05-22 17:48:51 +08:00
|
|
|
return $this;
|
|
|
|
});
|
2013-05-18 11:41:15 +08:00
|
|
|
};
|
2013-09-20 17:04:29 +08:00
|
|
|
}));
|