2014-09-25 02:35:43 +08:00
|
|
|
/* Copyright (c) 2012, 2014 Hyunje Alex Jun and other contributors
|
2012-11-06 21:00:37 +08:00
|
|
|
* Licensed under the MIT License
|
|
|
|
*/
|
2013-09-20 17:04:29 +08:00
|
|
|
(function (factory) {
|
2014-03-11 15:08:07 +08:00
|
|
|
'use strict';
|
|
|
|
|
2013-09-20 17:04:29 +08:00
|
|
|
if (typeof define === 'function' && define.amd) {
|
|
|
|
// AMD. Register as an anonymous module.
|
|
|
|
define(['jquery'], factory);
|
2014-02-21 02:06:30 +08:00
|
|
|
} else if (typeof exports === 'object') {
|
|
|
|
// Node/CommonJS
|
|
|
|
factory(require('jquery'));
|
2013-09-20 17:04:29 +08:00
|
|
|
} else {
|
|
|
|
// Browser globals
|
|
|
|
factory(jQuery);
|
|
|
|
}
|
2014-10-14 06:21:08 +08:00
|
|
|
})(function ($) {
|
2014-03-11 15:08:07 +08:00
|
|
|
'use strict';
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2014-11-14 14:00:59 +08:00
|
|
|
function getInt(x) {
|
2014-10-14 05:27:35 +08:00
|
|
|
if (typeof x === 'string') {
|
|
|
|
return parseInt(x, 10);
|
|
|
|
} else {
|
|
|
|
return ~~x;
|
|
|
|
}
|
2014-11-14 14:25:19 +08:00
|
|
|
}
|
2014-10-14 05:27:35 +08:00
|
|
|
|
2013-05-18 11:41:15 +08:00
|
|
|
var defaultSettings = {
|
2014-08-25 07:35:51 +08:00
|
|
|
wheelSpeed: 1,
|
2013-07-18 00:53:02 +08:00
|
|
|
wheelPropagation: false,
|
2014-11-05 00:58:24 +08:00
|
|
|
swipePropagation: true,
|
2013-09-28 06:53:57 +08:00
|
|
|
minScrollbarLength: null,
|
2014-07-10 01:16:16 +08:00
|
|
|
maxScrollbarLength: 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,
|
2014-01-29 17:06:27 +08:00
|
|
|
scrollYMarginOffset: 0,
|
|
|
|
includePadding: false
|
2013-05-18 11:41:15 +08:00
|
|
|
};
|
|
|
|
|
2014-10-29 00:41:36 +08:00
|
|
|
var incrementingId = 0;
|
|
|
|
var eventClassFactory = function () {
|
|
|
|
var id = incrementingId++;
|
|
|
|
return function (eventName) {
|
|
|
|
var className = '.perfect-scrollbar-' + id;
|
|
|
|
if (typeof eventName === 'undefined') {
|
|
|
|
return className;
|
|
|
|
} else {
|
|
|
|
return eventName + className;
|
|
|
|
}
|
2014-01-21 11:22:04 +08:00
|
|
|
};
|
2014-10-29 00:41:36 +08:00
|
|
|
};
|
2014-01-21 11:22:04 +08:00
|
|
|
|
2014-11-11 21:29:09 +08:00
|
|
|
var isWebkit = 'WebkitAppearance' in document.documentElement.style;
|
|
|
|
|
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 () {
|
2014-10-14 05:04:16 +08:00
|
|
|
var settings = $.extend(true, {}, defaultSettings);
|
|
|
|
var $this = $(this);
|
2014-11-01 03:17:38 +08:00
|
|
|
var isPluginAlive = function () { return !!$this; };
|
2013-08-05 22:59:17 +08:00
|
|
|
|
2013-05-22 17:48:51 +08:00
|
|
|
if (typeof suppliedSettings === "object") {
|
2014-10-14 06:21:08 +08:00
|
|
|
// Override default settings with any supplied
|
2013-05-22 17:48:51 +08:00
|
|
|
$.extend(true, settings, suppliedSettings);
|
|
|
|
} else {
|
2014-10-14 05:04:16 +08:00
|
|
|
// If no setting was supplied, then the first param must be the option
|
2013-05-22 17:48:51 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
$this.addClass('ps-container');
|
|
|
|
|
2014-10-14 05:04:16 +08:00
|
|
|
var containerWidth;
|
|
|
|
var containerHeight;
|
|
|
|
var contentWidth;
|
|
|
|
var contentHeight;
|
2014-10-14 06:21:08 +08:00
|
|
|
|
|
|
|
var isRtl = $this.css('direction') === "rtl";
|
2014-10-29 00:41:36 +08:00
|
|
|
var eventClass = eventClassFactory();
|
2014-09-11 05:06:05 +08:00
|
|
|
var ownerDocument = this.ownerDocument || document;
|
2014-10-14 06:21:08 +08:00
|
|
|
|
|
|
|
var $scrollbarXRail = $("<div class='ps-scrollbar-x-rail'>").appendTo($this);
|
|
|
|
var $scrollbarX = $("<div class='ps-scrollbar-x'>").appendTo($scrollbarXRail);
|
|
|
|
var scrollbarXActive;
|
2014-10-14 05:04:16 +08:00
|
|
|
var scrollbarXWidth;
|
|
|
|
var scrollbarXLeft;
|
2014-11-14 14:00:59 +08:00
|
|
|
var scrollbarXBottom = getInt($scrollbarXRail.css('bottom'));
|
2014-10-14 05:04:16 +08:00
|
|
|
var isScrollbarXUsingBottom = scrollbarXBottom === scrollbarXBottom; // !isNaN
|
2014-11-14 14:00:59 +08:00
|
|
|
var scrollbarXTop = isScrollbarXUsingBottom ? null : getInt($scrollbarXRail.css('top'));
|
|
|
|
var railBorderXWidth = getInt($scrollbarXRail.css('borderLeftWidth')) + getInt($scrollbarXRail.css('borderRightWidth'));
|
2014-10-14 06:21:08 +08:00
|
|
|
|
|
|
|
var $scrollbarYRail = $("<div class='ps-scrollbar-y-rail'>").appendTo($this);
|
|
|
|
var $scrollbarY = $("<div class='ps-scrollbar-y'>").appendTo($scrollbarYRail);
|
|
|
|
var scrollbarYActive;
|
2014-10-14 05:04:16 +08:00
|
|
|
var scrollbarYHeight;
|
|
|
|
var scrollbarYTop;
|
2014-11-14 14:00:59 +08:00
|
|
|
var scrollbarYRight = getInt($scrollbarYRail.css('right'));
|
2014-10-14 05:04:16 +08:00
|
|
|
var isScrollbarYUsingRight = scrollbarYRight === scrollbarYRight; // !isNaN
|
2014-11-14 14:00:59 +08:00
|
|
|
var scrollbarYLeft = isScrollbarYUsingRight ? null : getInt($scrollbarYRail.css('left'));
|
|
|
|
var railBorderYWidth = getInt($scrollbarYRail.css('borderTopWidth')) + getInt($scrollbarYRail.css('borderBottomWidth'));
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
function updateScrollTop(currentTop, deltaY) {
|
2014-10-14 05:04:16 +08:00
|
|
|
var newTop = currentTop + deltaY;
|
|
|
|
var maxTop = containerHeight - scrollbarYHeight;
|
2014-01-14 23:23:21 +08:00
|
|
|
|
|
|
|
if (newTop < 0) {
|
|
|
|
scrollbarYTop = 0;
|
2014-10-14 06:21:08 +08:00
|
|
|
} else if (newTop > maxTop) {
|
2014-01-14 23:23:21 +08:00
|
|
|
scrollbarYTop = maxTop;
|
2014-10-14 06:21:08 +08:00
|
|
|
} else {
|
2014-01-14 23:23:21 +08:00
|
|
|
scrollbarYTop = newTop;
|
|
|
|
}
|
|
|
|
|
2014-11-14 14:00:59 +08:00
|
|
|
var scrollTop = getInt(scrollbarYTop * (contentHeight - containerHeight) / (containerHeight - scrollbarYHeight));
|
2013-05-22 17:48:51 +08:00
|
|
|
$this.scrollTop(scrollTop);
|
2014-10-14 06:21:08 +08:00
|
|
|
}
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
function updateScrollLeft(currentLeft, deltaX) {
|
2014-10-14 05:04:16 +08:00
|
|
|
var newLeft = currentLeft + deltaX;
|
|
|
|
var maxLeft = containerWidth - scrollbarXWidth;
|
2014-01-14 23:23:21 +08:00
|
|
|
|
|
|
|
if (newLeft < 0) {
|
|
|
|
scrollbarXLeft = 0;
|
2014-10-14 06:21:08 +08:00
|
|
|
} else if (newLeft > maxLeft) {
|
2014-01-14 23:23:21 +08:00
|
|
|
scrollbarXLeft = maxLeft;
|
2014-10-14 06:21:08 +08:00
|
|
|
} else {
|
2014-01-14 23:23:21 +08:00
|
|
|
scrollbarXLeft = newLeft;
|
|
|
|
}
|
|
|
|
|
2014-11-14 14:00:59 +08:00
|
|
|
var scrollLeft = getInt(scrollbarXLeft * (contentWidth - containerWidth) / (containerWidth - scrollbarXWidth));
|
2013-05-22 17:48:51 +08:00
|
|
|
$this.scrollLeft(scrollLeft);
|
2014-10-14 06:21:08 +08:00
|
|
|
}
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
function getThumbSize(thumbSize) {
|
2013-07-18 00:53:02 +08:00
|
|
|
if (settings.minScrollbarLength) {
|
|
|
|
thumbSize = Math.max(thumbSize, settings.minScrollbarLength);
|
|
|
|
}
|
2014-07-10 01:16:16 +08:00
|
|
|
if (settings.maxScrollbarLength) {
|
|
|
|
thumbSize = Math.min(thumbSize, settings.maxScrollbarLength);
|
|
|
|
}
|
2013-07-18 00:53:02 +08:00
|
|
|
return thumbSize;
|
2014-10-14 06:21:08 +08:00
|
|
|
}
|
2013-07-18 00:53:02 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
function updateCss() {
|
2014-11-06 02:51:52 +08:00
|
|
|
var xRailOffset = {width: containerWidth};
|
2014-04-17 13:31:57 +08:00
|
|
|
if (isRtl) {
|
2014-10-14 06:21:08 +08:00
|
|
|
xRailOffset.left = $this.scrollLeft() + containerWidth - contentWidth;
|
2014-04-17 13:31:57 +08:00
|
|
|
} else {
|
2014-10-14 06:21:08 +08:00
|
|
|
xRailOffset.left = $this.scrollLeft();
|
2014-04-17 13:31:57 +08:00
|
|
|
}
|
2014-04-27 18:01:11 +08:00
|
|
|
if (isScrollbarXUsingBottom) {
|
2014-10-14 06:21:08 +08:00
|
|
|
xRailOffset.bottom = scrollbarXBottom - $this.scrollTop();
|
2014-04-27 18:01:11 +08:00
|
|
|
} else {
|
2014-10-14 06:21:08 +08:00
|
|
|
xRailOffset.top = scrollbarXTop + $this.scrollTop();
|
2014-04-27 18:01:11 +08:00
|
|
|
}
|
2014-10-14 06:21:08 +08:00
|
|
|
$scrollbarXRail.css(xRailOffset);
|
2014-04-27 18:01:11 +08:00
|
|
|
|
2014-11-06 02:51:52 +08:00
|
|
|
var railYOffset = {top: $this.scrollTop(), height: containerHeight};
|
2014-04-17 13:31:57 +08:00
|
|
|
|
2014-04-27 18:01:11 +08:00
|
|
|
if (isScrollbarYUsingRight) {
|
2014-04-17 13:31:57 +08:00
|
|
|
if (isRtl) {
|
2014-10-14 06:21:08 +08:00
|
|
|
railYOffset.right = contentWidth - $this.scrollLeft() - scrollbarYRight - $scrollbarY.outerWidth();
|
2014-04-17 13:31:57 +08:00
|
|
|
} else {
|
2014-10-14 06:21:08 +08:00
|
|
|
railYOffset.right = scrollbarYRight - $this.scrollLeft();
|
2014-04-17 13:31:57 +08:00
|
|
|
}
|
2014-04-27 18:01:11 +08:00
|
|
|
} else {
|
2014-04-17 13:31:57 +08:00
|
|
|
if (isRtl) {
|
2014-10-14 06:21:08 +08:00
|
|
|
railYOffset.left = $this.scrollLeft() + containerWidth * 2 - contentWidth - scrollbarYLeft - $scrollbarY.outerWidth();
|
2014-04-17 13:31:57 +08:00
|
|
|
} else {
|
2014-10-14 06:21:08 +08:00
|
|
|
railYOffset.left = scrollbarYLeft + $this.scrollLeft();
|
2014-04-17 13:31:57 +08:00
|
|
|
}
|
2014-04-27 18:01:11 +08:00
|
|
|
}
|
2014-10-14 06:21:08 +08:00
|
|
|
$scrollbarYRail.css(railYOffset);
|
2014-04-17 13:31:57 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
$scrollbarX.css({left: scrollbarXLeft, width: scrollbarXWidth - railBorderXWidth});
|
|
|
|
$scrollbarY.css({top: scrollbarYTop, height: scrollbarYHeight - railBorderYWidth});
|
|
|
|
}
|
2013-06-16 16:45:37 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
function updateGeometry() {
|
2014-09-16 04:45:27 +08:00
|
|
|
// Hide scrollbars not to affect scrollWidth and scrollHeight
|
2014-10-29 02:01:23 +08:00
|
|
|
$this.removeClass('ps-active-x');
|
|
|
|
$this.removeClass('ps-active-y');
|
2014-09-16 04:45:27 +08:00
|
|
|
|
2014-04-01 16:44:45 +08:00
|
|
|
containerWidth = settings.includePadding ? $this.innerWidth() : $this.width();
|
|
|
|
containerHeight = settings.includePadding ? $this.innerHeight() : $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;
|
2014-11-14 14:00:59 +08:00
|
|
|
scrollbarXWidth = getThumbSize(getInt(containerWidth * containerWidth / contentWidth));
|
|
|
|
scrollbarXLeft = getInt($this.scrollLeft() * (containerWidth - scrollbarXWidth) / (contentWidth - containerWidth));
|
2014-10-14 06:21:08 +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;
|
2014-11-14 14:00:59 +08:00
|
|
|
scrollbarYHeight = getThumbSize(getInt(containerHeight * containerHeight / contentHeight));
|
|
|
|
scrollbarYTop = getInt($this.scrollTop() * (containerHeight - scrollbarYHeight) / (contentHeight - containerHeight));
|
2014-10-14 06:21:08 +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
|
|
|
}
|
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
if (scrollbarXLeft >= containerWidth - scrollbarXWidth) {
|
|
|
|
scrollbarXLeft = containerWidth - scrollbarXWidth;
|
|
|
|
}
|
2013-05-22 17:48:51 +08:00
|
|
|
if (scrollbarYTop >= containerHeight - scrollbarYHeight) {
|
|
|
|
scrollbarYTop = containerHeight - scrollbarYHeight;
|
2012-11-10 02:07:47 +08:00
|
|
|
}
|
2014-10-14 06:21:08 +08:00
|
|
|
|
|
|
|
updateCss();
|
|
|
|
|
|
|
|
if (scrollbarXActive) {
|
|
|
|
$this.addClass('ps-active-x');
|
2012-11-10 02:07:47 +08:00
|
|
|
}
|
2014-10-14 06:21:08 +08:00
|
|
|
if (scrollbarYActive) {
|
|
|
|
$this.addClass('ps-active-y');
|
2014-09-28 08:24:09 +08:00
|
|
|
}
|
2014-10-14 06:21:08 +08:00
|
|
|
}
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
function bindMouseScrollXHandler() {
|
2014-10-14 05:04:16 +08:00
|
|
|
var currentLeft;
|
|
|
|
var currentPageX;
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2014-10-22 19:40:12 +08:00
|
|
|
var mouseMoveHandler = function (e) {
|
|
|
|
updateScrollLeft(currentLeft, e.pageX - currentPageX);
|
|
|
|
updateGeometry();
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
};
|
|
|
|
|
|
|
|
var mouseUpHandler = function (e) {
|
|
|
|
$scrollbarXRail.removeClass('in-scrolling');
|
|
|
|
$(ownerDocument).unbind(eventClass('mousemove'), mouseMoveHandler);
|
|
|
|
};
|
|
|
|
|
2014-10-29 00:41:36 +08:00
|
|
|
$scrollbarX.bind(eventClass('mousedown'), 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
|
|
|
|
2014-10-22 19:40:12 +08:00
|
|
|
$(ownerDocument).bind(eventClass('mousemove'), mouseMoveHandler);
|
|
|
|
$(ownerDocument).one(eventClass('mouseup'), mouseUpHandler);
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-10-22 19:40:12 +08:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2013-05-22 17:48:51 +08:00
|
|
|
});
|
2013-08-05 22:59:17 +08:00
|
|
|
|
|
|
|
currentLeft =
|
|
|
|
currentPageX = null;
|
2014-10-14 06:21:08 +08:00
|
|
|
}
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
function bindMouseScrollYHandler() {
|
2014-10-14 05:04:16 +08:00
|
|
|
var currentTop;
|
|
|
|
var currentPageY;
|
2012-11-10 02:07:47 +08:00
|
|
|
|
2014-10-22 19:40:12 +08:00
|
|
|
var mouseMoveHandler = function (e) {
|
|
|
|
updateScrollTop(currentTop, e.pageY - currentPageY);
|
|
|
|
updateGeometry();
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
};
|
|
|
|
|
|
|
|
var mouseUpHandler = function (e) {
|
|
|
|
$scrollbarYRail.removeClass('in-scrolling');
|
|
|
|
$(ownerDocument).unbind(eventClass('mousemove'), mouseMoveHandler);
|
|
|
|
};
|
|
|
|
|
2014-10-29 00:41:36 +08:00
|
|
|
$scrollbarY.bind(eventClass('mousedown'), 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
|
|
|
|
2014-10-22 19:40:12 +08:00
|
|
|
$(ownerDocument).bind(eventClass('mousemove'), mouseMoveHandler);
|
|
|
|
$(ownerDocument).one(eventClass('mouseup'), mouseUpHandler);
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2014-10-22 19:40:12 +08:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2013-05-22 17:48:51 +08:00
|
|
|
});
|
2013-08-05 22:59:17 +08:00
|
|
|
|
|
|
|
currentTop =
|
|
|
|
currentPageY = null;
|
2014-10-14 06:21:08 +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.
|
2014-10-14 06:21:08 +08:00
|
|
|
function shouldPreventDefault(deltaX, deltaY) {
|
2013-12-14 15:25:36 +08:00
|
|
|
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;
|
2014-10-14 06:21:08 +08:00
|
|
|
}
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-11-05 00:58:24 +08:00
|
|
|
// check if default swipe should be prevented
|
|
|
|
function shouldPreventDefaultSwipe(deltaX, deltaY) {
|
|
|
|
var
|
|
|
|
scrollTop = $this.scrollTop(),
|
|
|
|
scrollLeft = $this.scrollLeft(),
|
|
|
|
magnitudeX = Math.abs(deltaX),
|
|
|
|
magnitudeY = Math.abs(deltaY);
|
|
|
|
|
|
|
|
if (magnitudeY > magnitudeX) {
|
|
|
|
// user is perhaps trying to swipe up/down the page
|
|
|
|
|
|
|
|
if (((deltaY < 0) && (scrollTop === contentHeight - containerHeight)) ||
|
|
|
|
((deltaY > 0) && (scrollTop === 0))) {
|
|
|
|
return !settings.swipePropagation;
|
|
|
|
}
|
|
|
|
} else if (magnitudeX > magnitudeY) {
|
|
|
|
// user is perhaps trying to swipe left/right across the page
|
|
|
|
|
|
|
|
if (((deltaX < 0) && (scrollLeft === contentWidth - containerWidth)) ||
|
|
|
|
((deltaX > 0) && (scrollLeft === 0))) {
|
|
|
|
return !settings.swipePropagation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
function bindMouseWheelHandler() {
|
2013-08-06 14:14:55 +08:00
|
|
|
var shouldPrevent = false;
|
2014-08-25 07:35:51 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
function getDeltaFromEvent(e) {
|
2014-10-14 05:04:16 +08:00
|
|
|
var deltaX = e.originalEvent.deltaX;
|
|
|
|
var deltaY = -1 * e.originalEvent.deltaY;
|
2014-08-25 07:35:51 +08:00
|
|
|
|
|
|
|
if (typeof deltaX === "undefined" || typeof deltaY === "undefined") {
|
|
|
|
// OS X Safari
|
|
|
|
deltaX = -1 * e.originalEvent.wheelDeltaX / 6;
|
|
|
|
deltaY = e.originalEvent.wheelDeltaY / 6;
|
|
|
|
}
|
|
|
|
|
2014-09-16 05:53:40 +08:00
|
|
|
if (e.originalEvent.deltaMode && e.originalEvent.deltaMode === 1) {
|
|
|
|
// Firefox in deltaMode 1: Line scrolling
|
|
|
|
deltaX *= 10;
|
|
|
|
deltaY *= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deltaX !== deltaX && deltaY !== deltaY/* NaN checks */) {
|
|
|
|
// IE in some mouse drivers
|
|
|
|
deltaX = 0;
|
|
|
|
deltaY = e.originalEvent.wheelDelta;
|
|
|
|
}
|
|
|
|
|
2014-08-25 07:35:51 +08:00
|
|
|
return [deltaX, deltaY];
|
2014-10-14 06:21:08 +08:00
|
|
|
}
|
2014-08-25 07:35:51 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
function mousewheelHandler(e) {
|
2014-11-11 21:29:09 +08:00
|
|
|
// FIXME: this is a quick fix for the select problem in FF and IE.
|
|
|
|
// If there comes an effective way to deal with the problem,
|
|
|
|
// this lines should be removed.
|
|
|
|
if (!isWebkit && $this.find('select:focus').length > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-25 07:35:51 +08:00
|
|
|
var delta = getDeltaFromEvent(e);
|
|
|
|
|
2014-10-14 05:04:16 +08:00
|
|
|
var deltaX = delta[0];
|
|
|
|
var deltaY = delta[1];
|
2014-01-26 00:33:52 +08:00
|
|
|
|
2014-04-01 23:42:00 +08:00
|
|
|
shouldPrevent = false;
|
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));
|
|
|
|
}
|
2014-04-01 23:42:00 +08:00
|
|
|
shouldPrevent = true;
|
2013-09-28 06:53:57 +08:00
|
|
|
} 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));
|
|
|
|
}
|
2014-04-01 23:42:00 +08:00
|
|
|
shouldPrevent = true;
|
2013-09-28 06:53:57 +08:00
|
|
|
}
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
updateGeometry();
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-04-01 23:42:00 +08:00
|
|
|
shouldPrevent = (shouldPrevent || shouldPreventDefault(deltaX, deltaY));
|
2013-08-06 14:14:55 +08:00
|
|
|
if (shouldPrevent) {
|
2014-03-22 23:29:07 +08:00
|
|
|
e.stopPropagation();
|
2013-05-22 17:48:51 +08:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
2014-10-14 06:21:08 +08:00
|
|
|
}
|
2014-08-25 07:35:51 +08:00
|
|
|
|
|
|
|
if (typeof window.onwheel !== "undefined") {
|
2014-10-29 00:41:36 +08:00
|
|
|
$this.bind(eventClass('wheel'), mousewheelHandler);
|
2014-08-25 07:35:51 +08:00
|
|
|
} else if (typeof window.onmousewheel !== "undefined") {
|
2014-10-29 00:41:36 +08:00
|
|
|
$this.bind(eventClass('mousewheel'), mousewheelHandler);
|
2014-08-25 07:35:51 +08:00
|
|
|
}
|
2014-10-14 06:21:08 +08:00
|
|
|
}
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
function bindKeyboardHandler() {
|
2013-09-30 16:24:12 +08:00
|
|
|
var hovered = false;
|
2014-10-29 00:41:36 +08:00
|
|
|
$this.bind(eventClass('mouseenter'), function (e) {
|
2013-09-30 16:24:12 +08:00
|
|
|
hovered = true;
|
|
|
|
});
|
2014-10-29 00:41:36 +08:00
|
|
|
$this.bind(eventClass('mouseleave'), function (e) {
|
2013-09-30 16:24:12 +08:00
|
|
|
hovered = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
var shouldPrevent = false;
|
2014-10-29 00:41:36 +08:00
|
|
|
$(ownerDocument).bind(eventClass('keydown'), function (e) {
|
2014-07-15 22:41:01 +08:00
|
|
|
if (e.isDefaultPrevented && e.isDefaultPrevented()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-01 17:49:16 +08:00
|
|
|
if (!hovered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-11 05:06:05 +08:00
|
|
|
var activeElement = document.activeElement ? document.activeElement : ownerDocument.activeElement;
|
2014-10-01 17:49:16 +08:00
|
|
|
// go deeper if element is a webcomponent
|
|
|
|
while (activeElement.shadowRoot) {
|
|
|
|
activeElement = activeElement.shadowRoot.activeElement;
|
|
|
|
}
|
|
|
|
if ($(activeElement).is(":input,[contenteditable]")) {
|
2013-09-30 16:24:12 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-14 05:04:16 +08:00
|
|
|
var deltaX = 0;
|
|
|
|
var deltaY = 0;
|
2013-09-30 16:24:12 +08:00
|
|
|
|
|
|
|
switch (e.which) {
|
|
|
|
case 37: // left
|
2014-03-23 04:51:27 +08:00
|
|
|
deltaX = -30;
|
2013-09-30 16:24:12 +08:00
|
|
|
break;
|
|
|
|
case 38: // up
|
2014-03-23 04:51:27 +08:00
|
|
|
deltaY = 30;
|
2013-09-30 16:24:12 +08:00
|
|
|
break;
|
|
|
|
case 39: // right
|
2014-03-23 04:51:27 +08:00
|
|
|
deltaX = 30;
|
2013-09-30 16:24:12 +08:00
|
|
|
break;
|
|
|
|
case 40: // down
|
2014-03-23 04:51:27 +08:00
|
|
|
deltaY = -30;
|
2013-09-30 16:24:12 +08:00
|
|
|
break;
|
2014-01-21 06:11:42 +08:00
|
|
|
case 33: // page up
|
2014-03-23 04:51:27 +08:00
|
|
|
deltaY = 90;
|
2014-01-21 06:11:42 +08:00
|
|
|
break;
|
|
|
|
case 32: // space bar
|
|
|
|
case 34: // page down
|
2014-03-23 04:51:27 +08:00
|
|
|
deltaY = -90;
|
2014-01-21 06:11:42 +08:00
|
|
|
break;
|
|
|
|
case 35: // end
|
2014-10-14 07:29:51 +08:00
|
|
|
if (e.ctrlKey) {
|
|
|
|
deltaY = -contentHeight;
|
|
|
|
} else {
|
|
|
|
deltaY = -containerHeight;
|
|
|
|
}
|
2014-01-21 06:11:42 +08:00
|
|
|
break;
|
|
|
|
case 36: // home
|
2014-10-14 07:29:51 +08:00
|
|
|
if (e.ctrlKey) {
|
|
|
|
deltaY = $this.scrollTop();
|
|
|
|
} else {
|
|
|
|
deltaY = containerHeight;
|
|
|
|
}
|
2014-01-21 06:11:42 +08:00
|
|
|
break;
|
2013-09-30 16:24:12 +08:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-23 04:51:27 +08:00
|
|
|
$this.scrollTop($this.scrollTop() - deltaY);
|
|
|
|
$this.scrollLeft($this.scrollLeft() + deltaX);
|
2013-09-30 16:24:12 +08:00
|
|
|
|
|
|
|
shouldPrevent = shouldPreventDefault(deltaX, deltaY);
|
|
|
|
if (shouldPrevent) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2014-10-14 06:21:08 +08:00
|
|
|
}
|
2013-09-30 16:24:12 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
function bindRailClickHandler() {
|
|
|
|
function stopPropagation(e) { e.stopPropagation(); }
|
2013-09-30 22:32:15 +08:00
|
|
|
|
2014-10-29 00:41:36 +08:00
|
|
|
$scrollbarY.bind(eventClass('click'), stopPropagation);
|
|
|
|
$scrollbarYRail.bind(eventClass('click'), function (e) {
|
2014-11-14 14:00:59 +08:00
|
|
|
var halfOfScrollbarLength = getInt(scrollbarYHeight / 2);
|
2014-10-14 05:04:16 +08:00
|
|
|
var positionTop = e.pageY - $scrollbarYRail.offset().top - halfOfScrollbarLength;
|
|
|
|
var maxPositionTop = containerHeight - scrollbarYHeight;
|
|
|
|
var positionRatio = positionTop / maxPositionTop;
|
2013-09-30 22:32:15 +08:00
|
|
|
|
|
|
|
if (positionRatio < 0) {
|
|
|
|
positionRatio = 0;
|
|
|
|
} else if (positionRatio > 1) {
|
|
|
|
positionRatio = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this.scrollTop((contentHeight - containerHeight) * positionRatio);
|
|
|
|
});
|
|
|
|
|
2014-10-29 00:41:36 +08:00
|
|
|
$scrollbarX.bind(eventClass('click'), stopPropagation);
|
|
|
|
$scrollbarXRail.bind(eventClass('click'), function (e) {
|
2014-11-14 14:00:59 +08:00
|
|
|
var halfOfScrollbarLength = getInt(scrollbarXWidth / 2);
|
2014-10-14 05:04:16 +08:00
|
|
|
var positionLeft = e.pageX - $scrollbarXRail.offset().left - halfOfScrollbarLength;
|
|
|
|
var maxPositionLeft = containerWidth - scrollbarXWidth;
|
|
|
|
var positionRatio = positionLeft / maxPositionLeft;
|
2013-09-30 22:32:15 +08:00
|
|
|
|
|
|
|
if (positionRatio < 0) {
|
|
|
|
positionRatio = 0;
|
|
|
|
} else if (positionRatio > 1) {
|
|
|
|
positionRatio = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this.scrollLeft((contentWidth - containerWidth) * positionRatio);
|
|
|
|
});
|
2014-10-14 06:21:08 +08:00
|
|
|
}
|
2013-09-30 22:32:15 +08:00
|
|
|
|
2014-10-29 01:49:16 +08:00
|
|
|
function bindSelectionHandler() {
|
|
|
|
function getRangeNode() {
|
|
|
|
var selection = window.getSelection ? window.getSelection() :
|
|
|
|
document.getSlection ? document.getSlection() : {rangeCount: 0};
|
|
|
|
if (selection.rangeCount === 0) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return selection.getRangeAt(0).commonAncestorContainer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var scrollingLoop = null;
|
|
|
|
var scrollDiff = {top: 0, left: 0};
|
|
|
|
function startScrolling() {
|
|
|
|
if (!scrollingLoop) {
|
|
|
|
scrollingLoop = setInterval(function () {
|
2014-11-01 03:17:38 +08:00
|
|
|
if (!isPluginAlive()) {
|
|
|
|
clearInterval(scrollingLoop);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:49:16 +08:00
|
|
|
$this.scrollTop($this.scrollTop() + scrollDiff.top);
|
|
|
|
$this.scrollLeft($this.scrollLeft() + scrollDiff.left);
|
|
|
|
updateGeometry();
|
|
|
|
}, 50); // every .1 sec
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function stopScrolling() {
|
|
|
|
if (scrollingLoop) {
|
|
|
|
clearInterval(scrollingLoop);
|
|
|
|
scrollingLoop = null;
|
|
|
|
}
|
|
|
|
$scrollbarXRail.removeClass('in-scrolling');
|
|
|
|
$scrollbarYRail.removeClass('in-scrolling');
|
|
|
|
}
|
|
|
|
|
|
|
|
var isSelected = false;
|
|
|
|
$(ownerDocument).bind(eventClass('selectionchange'), function (e) {
|
|
|
|
if ($.contains($this[0], getRangeNode())) {
|
|
|
|
isSelected = true;
|
|
|
|
} else {
|
|
|
|
isSelected = false;
|
|
|
|
stopScrolling();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$(window).bind(eventClass('mouseup'), function (e) {
|
|
|
|
if (isSelected) {
|
|
|
|
isSelected = false;
|
|
|
|
stopScrolling();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$(window).bind(eventClass('mousemove'), function (e) {
|
|
|
|
if (isSelected) {
|
|
|
|
var mousePosition = {x: e.pageX, y: e.pageY};
|
|
|
|
var containerOffset = $this.offset();
|
|
|
|
var containerGeometry = {
|
|
|
|
left: containerOffset.left,
|
|
|
|
right: containerOffset.left + $this.outerWidth(),
|
|
|
|
top: containerOffset.top,
|
|
|
|
bottom: containerOffset.top + $this.outerHeight()
|
|
|
|
};
|
|
|
|
|
|
|
|
if (mousePosition.x < containerGeometry.left + 3) {
|
|
|
|
scrollDiff.left = -5;
|
|
|
|
$scrollbarXRail.addClass('in-scrolling');
|
|
|
|
} else if (mousePosition.x > containerGeometry.right - 3) {
|
|
|
|
scrollDiff.left = 5;
|
|
|
|
$scrollbarXRail.addClass('in-scrolling');
|
|
|
|
} else {
|
|
|
|
scrollDiff.left = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mousePosition.y < containerGeometry.top + 3) {
|
|
|
|
if (containerGeometry.top + 3 - mousePosition.y < 5) {
|
|
|
|
scrollDiff.top = -5;
|
|
|
|
} else {
|
|
|
|
scrollDiff.top = -20;
|
|
|
|
}
|
|
|
|
$scrollbarYRail.addClass('in-scrolling');
|
|
|
|
} else if (mousePosition.y > containerGeometry.bottom - 3) {
|
|
|
|
if (mousePosition.y - containerGeometry.bottom + 3 < 5) {
|
|
|
|
scrollDiff.top = 5;
|
|
|
|
} else {
|
|
|
|
scrollDiff.top = 20;
|
|
|
|
}
|
|
|
|
$scrollbarYRail.addClass('in-scrolling');
|
|
|
|
} else {
|
|
|
|
scrollDiff.top = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scrollDiff.top === 0 && scrollDiff.left === 0) {
|
|
|
|
stopScrolling();
|
|
|
|
} else {
|
|
|
|
startScrolling();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-22 04:06:16 +08:00
|
|
|
function bindTouchHandler(supportsTouch, supportsIePointer) {
|
2014-10-14 06:21:08 +08:00
|
|
|
function applyTouchMove(differenceX, differenceY) {
|
2013-05-22 17:48:51 +08:00
|
|
|
$this.scrollTop($this.scrollTop() - differenceY);
|
|
|
|
$this.scrollLeft($this.scrollLeft() - differenceX);
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
updateGeometry();
|
|
|
|
}
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
var startOffset = {};
|
2014-10-14 05:04:16 +08:00
|
|
|
var startTime = 0;
|
|
|
|
var speed = {};
|
2014-11-01 03:20:23 +08:00
|
|
|
var easingLoop = null;
|
2014-10-14 05:04:16 +08:00
|
|
|
var inGlobalTouch = false;
|
2014-10-24 18:26:54 +08:00
|
|
|
var inLocalTouch = false;
|
2013-06-14 20:57:24 +08:00
|
|
|
|
2014-10-13 11:48:48 +08:00
|
|
|
function globalTouchStart(e) {
|
2013-06-14 20:57:24 +08:00
|
|
|
inGlobalTouch = true;
|
2014-10-13 11:48:48 +08:00
|
|
|
}
|
|
|
|
function globalTouchEnd(e) {
|
2013-06-14 20:57:24 +08:00
|
|
|
inGlobalTouch = false;
|
2014-10-13 11:48:48 +08:00
|
|
|
}
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2014-10-13 11:48:48 +08:00
|
|
|
function getTouch(e) {
|
|
|
|
if (e.originalEvent.targetTouches) {
|
|
|
|
return e.originalEvent.targetTouches[0];
|
|
|
|
} else {
|
|
|
|
// Maybe IE pointer
|
|
|
|
return e.originalEvent;
|
|
|
|
}
|
|
|
|
}
|
2014-10-24 18:26:54 +08:00
|
|
|
function shouldHandle(e) {
|
|
|
|
var event = e.originalEvent;
|
|
|
|
if (event.targetTouches && event.targetTouches.length === 1) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-10-31 00:04:20 +08:00
|
|
|
if (event.pointerType && event.pointerType !== 'mouse' && event.pointerType !== event.MSPOINTER_TYPE_MOUSE) {
|
2014-10-24 18:26:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-13 11:48:48 +08:00
|
|
|
function touchStart(e) {
|
2014-10-24 18:26:54 +08:00
|
|
|
if (shouldHandle(e)) {
|
|
|
|
inLocalTouch = true;
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2014-10-24 18:26:54 +08:00
|
|
|
var touch = getTouch(e);
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-10-24 18:26:54 +08:00
|
|
|
startOffset.pageX = touch.pageX;
|
|
|
|
startOffset.pageY = touch.pageY;
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-10-24 18:26:54 +08:00
|
|
|
startTime = (new Date()).getTime();
|
2013-06-14 20:57:24 +08:00
|
|
|
|
2014-11-01 03:20:23 +08:00
|
|
|
if (easingLoop !== null) {
|
|
|
|
clearInterval(easingLoop);
|
2014-10-24 18:26:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
2014-10-13 11:48:48 +08:00
|
|
|
}
|
|
|
|
function touchMove(e) {
|
2014-10-24 18:26:54 +08:00
|
|
|
if (!inGlobalTouch && inLocalTouch && shouldHandle(e)) {
|
2014-10-13 11:48:48 +08:00
|
|
|
var touch = getTouch(e);
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
var currentOffset = {pageX: touch.pageX, pageY: touch.pageY};
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
var differenceX = currentOffset.pageX - startOffset.pageX;
|
|
|
|
var differenceY = currentOffset.pageY - startOffset.pageY;
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2013-06-14 20:57:24 +08:00
|
|
|
applyTouchMove(differenceX, differenceY);
|
2014-10-14 06:21:08 +08:00
|
|
|
startOffset = currentOffset;
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2013-06-14 20:57:24 +08:00
|
|
|
var currentTime = (new Date()).getTime();
|
2014-01-28 13:51:53 +08:00
|
|
|
|
|
|
|
var timeGap = currentTime - startTime;
|
|
|
|
if (timeGap > 0) {
|
|
|
|
speed.x = differenceX / timeGap;
|
|
|
|
speed.y = differenceY / timeGap;
|
|
|
|
startTime = currentTime;
|
|
|
|
}
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-11-05 00:58:24 +08:00
|
|
|
if (shouldPreventDefaultSwipe(differenceX, differenceY)) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2013-06-14 20:57:24 +08:00
|
|
|
}
|
2014-10-13 11:48:48 +08:00
|
|
|
}
|
|
|
|
function touchEnd(e) {
|
2014-10-24 18:26:54 +08:00
|
|
|
if (!inGlobalTouch && inLocalTouch) {
|
|
|
|
inLocalTouch = false;
|
|
|
|
|
2014-11-01 03:20:23 +08:00
|
|
|
clearInterval(easingLoop);
|
|
|
|
easingLoop = setInterval(function () {
|
2014-11-01 03:17:38 +08:00
|
|
|
if (!isPluginAlive()) {
|
2014-11-01 03:20:23 +08:00
|
|
|
clearInterval(easingLoop);
|
2014-11-01 03:17:38 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-24 18:26:54 +08:00
|
|
|
if (Math.abs(speed.x) < 0.01 && Math.abs(speed.y) < 0.01) {
|
2014-11-01 03:20:23 +08:00
|
|
|
clearInterval(easingLoop);
|
2014-10-24 18:26:54 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-10-24 18:26:54 +08:00
|
|
|
applyTouchMove(speed.x * 30, speed.y * 30);
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-10-24 18:26:54 +08:00
|
|
|
speed.x *= 0.8;
|
|
|
|
speed.y *= 0.8;
|
|
|
|
}, 10);
|
|
|
|
}
|
2014-10-13 11:48:48 +08:00
|
|
|
}
|
|
|
|
|
2014-10-22 04:06:16 +08:00
|
|
|
if (supportsTouch) {
|
2014-10-29 00:41:36 +08:00
|
|
|
$(window).bind(eventClass("touchstart"), globalTouchStart);
|
|
|
|
$(window).bind(eventClass("touchend"), globalTouchEnd);
|
|
|
|
$this.bind(eventClass("touchstart"), touchStart);
|
|
|
|
$this.bind(eventClass("touchmove"), touchMove);
|
|
|
|
$this.bind(eventClass("touchend"), touchEnd);
|
2014-10-22 04:06:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (supportsIePointer) {
|
|
|
|
if (window.PointerEvent) {
|
2014-10-29 00:41:36 +08:00
|
|
|
$(window).bind(eventClass("pointerdown"), globalTouchStart);
|
|
|
|
$(window).bind(eventClass("pointerup"), globalTouchEnd);
|
|
|
|
$this.bind(eventClass("pointerdown"), touchStart);
|
|
|
|
$this.bind(eventClass("pointermove"), touchMove);
|
|
|
|
$this.bind(eventClass("pointerup"), touchEnd);
|
2014-10-22 04:06:16 +08:00
|
|
|
} else if (window.MSPointerEvent) {
|
2014-10-29 00:41:36 +08:00
|
|
|
$(window).bind(eventClass("MSPointerDown"), globalTouchStart);
|
|
|
|
$(window).bind(eventClass("MSPointerUp"), globalTouchEnd);
|
|
|
|
$this.bind(eventClass("MSPointerDown"), touchStart);
|
|
|
|
$this.bind(eventClass("MSPointerMove"), touchMove);
|
|
|
|
$this.bind(eventClass("MSPointerUp"), touchEnd);
|
2014-10-22 04:06:16 +08:00
|
|
|
}
|
2014-10-13 11:48:48 +08:00
|
|
|
}
|
2014-10-14 06:21:08 +08:00
|
|
|
}
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
function bindScrollHandler() {
|
2014-10-29 00:41:36 +08:00
|
|
|
$this.bind(eventClass('scroll'), function (e) {
|
2014-10-14 06:21:08 +08:00
|
|
|
updateGeometry();
|
2014-01-14 23:23:21 +08:00
|
|
|
});
|
2014-10-14 06:21:08 +08:00
|
|
|
}
|
2014-01-14 23:23:21 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
function destroy() {
|
2014-10-29 00:41:36 +08:00
|
|
|
$this.unbind(eventClass());
|
|
|
|
$(window).unbind(eventClass());
|
|
|
|
$(ownerDocument).unbind(eventClass());
|
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
|
2014-11-01 03:17:38 +08:00
|
|
|
$this =
|
2014-04-27 18:43:21 +08:00
|
|
|
$scrollbarXRail =
|
|
|
|
$scrollbarYRail =
|
2013-08-05 22:59:17 +08:00
|
|
|
$scrollbarX =
|
|
|
|
$scrollbarY =
|
2014-04-27 18:43:21 +08:00
|
|
|
scrollbarXActive =
|
|
|
|
scrollbarYActive =
|
2013-08-05 22:59:17 +08:00
|
|
|
containerWidth =
|
|
|
|
containerHeight =
|
|
|
|
contentWidth =
|
|
|
|
contentHeight =
|
|
|
|
scrollbarXWidth =
|
|
|
|
scrollbarXLeft =
|
|
|
|
scrollbarXBottom =
|
2014-04-27 18:43:21 +08:00
|
|
|
isScrollbarXUsingBottom =
|
|
|
|
scrollbarXTop =
|
2013-08-05 22:59:17 +08:00
|
|
|
scrollbarYHeight =
|
|
|
|
scrollbarYTop =
|
2014-04-27 18:43:21 +08:00
|
|
|
scrollbarYRight =
|
|
|
|
isScrollbarYUsingRight =
|
|
|
|
scrollbarYLeft =
|
|
|
|
isRtl =
|
2014-10-29 00:41:36 +08:00
|
|
|
eventClass = null;
|
2014-10-14 06:21:08 +08:00
|
|
|
}
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2013-06-29 01:14:40 +08:00
|
|
|
var supportsTouch = (('ontouchstart' in window) || window.DocumentTouch && document instanceof window.DocumentTouch);
|
2014-10-13 11:48:48 +08:00
|
|
|
var supportsIePointer = window.navigator.msMaxTouchPoints !== null;
|
2013-05-22 17:48:51 +08:00
|
|
|
|
2014-10-14 06:21:08 +08:00
|
|
|
function initialize() {
|
|
|
|
updateGeometry();
|
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();
|
2014-10-29 01:49:16 +08:00
|
|
|
bindSelectionHandler();
|
2014-08-25 07:35:51 +08:00
|
|
|
bindMouseWheelHandler();
|
|
|
|
|
2014-10-13 11:48:48 +08:00
|
|
|
if (supportsTouch || supportsIePointer) {
|
2014-10-22 04:06:16 +08:00
|
|
|
bindTouchHandler(supportsTouch, supportsIePointer);
|
2013-05-22 17:48:51 +08:00
|
|
|
}
|
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);
|
2014-10-14 06:21:08 +08:00
|
|
|
$this.data('perfect-scrollbar-update', updateGeometry);
|
2013-05-22 17:48:51 +08:00
|
|
|
$this.data('perfect-scrollbar-destroy', destroy);
|
2014-10-14 06:21:08 +08:00
|
|
|
}
|
2013-05-18 11:41:15 +08:00
|
|
|
|
2013-05-22 17:48:51 +08:00
|
|
|
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
|
|
|
};
|
2014-10-14 06:21:08 +08:00
|
|
|
});
|