From 05b20f45efbd3206814b9a1eed610380f7de9436 Mon Sep 17 00:00:00 2001 From: Alexandr Subbotin Date: Wed, 22 Oct 2014 15:40:12 +0400 Subject: [PATCH] decrease mouse handlers count and every-time class checking during mousemove --- src/perfect-scrollbar.js | 68 +++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/src/perfect-scrollbar.js b/src/perfect-scrollbar.js index da90c5e..3935d8b 100644 --- a/src/perfect-scrollbar.js +++ b/src/perfect-scrollbar.js @@ -250,32 +250,30 @@ var currentLeft; var currentPageX; - var inScrolling = false; + 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); + }; + $scrollbarX.bind(eventClass('mousedown'), function (e) { currentPageX = e.pageX; currentLeft = $scrollbarX.position().left; $scrollbarXRail.addClass('in-scrolling'); - inScrolling = true; + + $(ownerDocument).bind(eventClass('mousemove'), mouseMoveHandler); + $(ownerDocument).one(eventClass('mouseup'), mouseUpHandler); + e.stopPropagation(); e.preventDefault(); }); - $(ownerDocument).bind(eventClass('mousemove'), function (e) { - if (inScrolling) { - updateScrollLeft(currentLeft, e.pageX - currentPageX); - updateGeometry(); - e.stopPropagation(); - e.preventDefault(); - } - }); - - $(ownerDocument).bind(eventClass('mouseup'), function (e) { - if (inScrolling) { - inScrolling = false; - $scrollbarXRail.removeClass('in-scrolling'); - } - }); - currentLeft = currentPageX = null; } @@ -284,32 +282,30 @@ var currentTop; var currentPageY; - var inScrolling = false; + 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); + }; + $scrollbarY.bind(eventClass('mousedown'), function (e) { currentPageY = e.pageY; currentTop = $scrollbarY.position().top; - inScrolling = true; $scrollbarYRail.addClass('in-scrolling'); + + $(ownerDocument).bind(eventClass('mousemove'), mouseMoveHandler); + $(ownerDocument).one(eventClass('mouseup'), mouseUpHandler); + e.stopPropagation(); e.preventDefault(); }); - $(ownerDocument).bind(eventClass('mousemove'), function (e) { - if (inScrolling) { - updateScrollTop(currentTop, e.pageY - currentPageY); - updateGeometry(); - e.stopPropagation(); - e.preventDefault(); - } - }); - - $(ownerDocument).bind(eventClass('mouseup'), function (e) { - if (inScrolling) { - inScrolling = false; - $scrollbarYRail.removeClass('in-scrolling'); - } - }); - currentTop = currentPageY = null; }