Skip to content

Commit b0437f1

Browse files
committed
fix bug when scroll document
1 parent 6facf55 commit b0437f1

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/boxesscroll.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ box-scroll.horizontal, box-hscroll {
6060
height: auto !important;
6161
position:absolute;
6262
bottom:10px;
63-
right:10px;
63+
right:20px;
6464
z-index: 1000;
6565
min-width: 10px;
6666
padding: 3px 7px;

src/boxesscroll.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
}
133133
$document.on("mousedown", mousedownOnDoc);
134134
$document.on("keydown", keydownOnOnDoc);
135+
$document.on("scroll", invalidSizes);
135136
}
136137
function removeEventListeners() {
137138
ctrl.ngelt.off('wheel', wheelOnElt);
@@ -144,6 +145,11 @@
144145
ng.element($window).off("resize", updateSize);
145146
$document.off("mousedown", mousedownOnDoc);
146147
$document.off("keydown", keydownOnOnDoc);
148+
$document.off("scroll", invalidSizes);
149+
}
150+
function invalidSizes(event) {
151+
ctrl.getScrollbarArea().invalid = true;
152+
ctrl.getEltArea().invalid = true;
147153
}
148154
function keydownOnOnDoc(event) {
149155
if (!$scope.allowKeynav || !hasFocus || event.which < 33 || event.which > 40)
@@ -492,14 +498,17 @@
492498
if ($scope.max) {
493499
$scope.ngLimit = $scope.max;
494500
} else if ($scope.total) {
495-
if (items.length) {
496-
var size = ctrl.horizontal ?
497-
getArea(items[items.length - 1]).right - getArea(items[0]).left :
498-
getArea(items[items.length - 1]).bottom - getArea(items[0]).top;
499-
var offset = getOffsetPixelContainerBeforeItem(items[0]); // on ignore les éléments avant
500-
var empty = getHeightArea() - offset - size;
501+
var nbItems = items.length;
502+
if (nbItems) {
503+
var firstItem = items[0];
504+
var lastItem = items[nbItems - 1];
505+
var containerSize = ctrl.horizontal ?
506+
getArea(lastItem).right - getArea(firstItem).left :
507+
getArea(lastItem).bottom - getArea(firstItem).top;
508+
var offset = getOffsetPixelContainerBeforeItem(firstItem); // on ignore les éléments avant
509+
var empty = getHeightArea() - offset - containerSize; // zone non remplie
501510
var inc = 0;
502-
var average = size / items.length;
511+
var average = containerSize / nbItems;
503512
if (average) { // protect div par 0
504513
var floatValue = empty / average;
505514
inc = floatValue < 0 ? Math.ceil(floatValue) : Math.ceil(floatValue); // on veut en voir une de plus
@@ -769,7 +778,7 @@
769778
ngelt.append(ctrl.ngsb);
770779
ctrl.sb = getHtmlElement(ctrl.ngsb);
771780
var watcherClears = [];
772-
if (ngelt.css('display') === 'none') { // si c'est une popup, on surveille le display via un $interval global
781+
if (ngelt.css('display') === 'none') { // si c'est une popup, on surveille le display
773782
var watcherClear = scope.$watch(function (scope) {
774783
return scope.ctrl.ngelt.css('display');
775784
}, function (v1, v2, s) {
@@ -807,19 +816,23 @@
807816
}
808817
}));
809818
}
819+
var totalTimer;
810820
watcherClears.push(scope.$watch('total', function (v1, v2, s) {
811821
if (v1 !== v2) {
812-
$timeout(s.ctrl.updateTotal, s.debounce || DEBOUNCE, true);
822+
if(totalTimer) $timeout.cancel(totalTimer);
823+
totalTimer = $timeout(s.ctrl.updateTotal, s.debounce || DEBOUNCE, true);
813824
}
814825
}));
826+
var limitTimer;
815827
watcherClears.push(scope.$watch('ngLimit', function (v1, v2, s) {
816828
if (v1 !== v2) {
817-
$timeout(s.ctrl.updateLimit, s.debounce || DEBOUNCE, true);
829+
if(limitTimer) $timeout.cancel(limitTimer);
830+
limitTimer = $timeout(s.ctrl.updateLimit, s.debounce || DEBOUNCE, true);
818831
}
819832
}));
820833
watcherClears.push(scope.$watch('ngBegin', function (v1, v2, s) {
821834
if (v1 >= 0 && v1 <= s.total - s.ctrl.getInnerLimit()) {
822-
$timeout(s.ctrl.updateBegin, s.debounce || DEBOUNCE, true);
835+
s.ctrl.updateBegin();
823836
} else if (v1 < 0) {
824837
s.ngBegin = 0;
825838
} else {

0 commit comments

Comments
 (0)