From aa213398f39a5e2eb7b2df9117cfe313e3f6cd38 Mon Sep 17 00:00:00 2001 From: Hyunje Alex Jun Date: Sat, 16 Feb 2013 00:17:23 +0900 Subject: [PATCH] Enhance scroll preventDefault logic. Now scroll preventDefault works more like default browser scroll. When the scroll leaches top, bottom, left and right end, don't prevent default scrolling. For sure, when the content size is smaller than the container size, also don't prevent default scrolling. --- src/perfect-scrollbar.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/perfect-scrollbar.js b/src/perfect-scrollbar.js index c3cf365..8cdc248 100644 --- a/src/perfect-scrollbar.js +++ b/src/perfect-scrollbar.js @@ -166,6 +166,25 @@ // bind handlers var bindMouseWheelHandler = function() { + var shouldPreventDefault = function(deltaX, deltaY) { + var scrollTop = $this.scrollTop(); + if(scrollTop == 0 && deltaY > 0 && deltaX == 0) { + return false; + } + else if(scrollTop >= content_height - container_height && deltaY < 0 && deltaX == 0) { + return false; + } + + var scrollLeft = $this.scrollLeft(); + if(scrollLeft == 0 && deltaX < 0 && deltaY == 0) { + return false; + } + else if(scrollLeft >= content_width - container_width && deltaX > 0 && deltaY == 0) { + return false; + } + return true; + }; + $this.mousewheel(function(e, delta, deltaX, deltaY) { $this.scrollTop($this.scrollTop() - (deltaY * 10)); $this.scrollLeft($this.scrollLeft() + (deltaX * 10)); @@ -173,7 +192,7 @@ // update bar position updateBarSizeAndPosition(); - if(content_height > container_height || content_width > container_width) { + if(shouldPreventDefault(deltaX, deltaY)) { e.preventDefault(); } });