From a75bb0992c2a265773207dd7d5c3cbfb0ca1cefd Mon Sep 17 00:00:00 2001 From: Bryson Date: Sat, 30 Jan 2016 10:42:21 -0800 Subject: [PATCH 1/2] Keep the scrollbar at the bottom when rebuilding, as long as the user hasn't scrolled up. --- bower.json | 4 ++-- dist/ng-scrollbar.js | 7 ++++--- dist/ng-scrollbar.min.js | 4 ++-- src/ng-scrollbar.js | 10 ++++++---- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/bower.json b/bower.json index 1af471f..c9b4f66 100644 --- a/bower.json +++ b/bower.json @@ -1,10 +1,10 @@ { "name": "ng-scrollbar", - "version": "0.0.8", + "version": "0.0.9", "dependencies": { }, "main": [ - "./dist/ng-scrollbar.js", + "./dist/ng-scrollbar.js", "./dist/ng-scrollbar.css" ], "devDependencies": { diff --git a/dist/ng-scrollbar.js b/dist/ng-scrollbar.js index 6e9ddc8..31c0b6a 100644 --- a/dist/ng-scrollbar.js +++ b/dist/ng-scrollbar.js @@ -119,8 +119,9 @@ angular.module('ngScrollbar', []).directive('ngScrollbar', [ elm.detachEvent('onmousewheel', wheelHandler); } }; + var lastBottom = null; var buildScrollbar = function (rollToBottom) { - rollToBottom = flags.bottom || rollToBottom; + rollToBottom = (flags.bottom || rollToBottom) && (dragger.top === lastBottom || lastBottom === null); mainElm = angular.element(element.children()[0]); transculdedContainer = angular.element(mainElm.children()[0]); tools = angular.element(mainElm.children()[1]); @@ -160,9 +161,9 @@ angular.module('ngScrollbar', []).directive('ngScrollbar', [ win.on('touchmove', _touchDragHandler); event.preventDefault(); }); + lastBottom = parseInt(page.height, 10) - parseInt(dragger.height, 10); if (rollToBottom) { - flags.bottom = false; - dragger.top = parseInt(page.height, 10) - parseInt(dragger.height, 10); + dragger.top = lastBottom; } else { dragger.top = Math.max(0, Math.min(parseInt(page.height, 10) - parseInt(dragger.height, 10), parseInt(dragger.top, 10))); } diff --git a/dist/ng-scrollbar.min.js b/dist/ng-scrollbar.min.js index 51b7948..c7e1284 100644 --- a/dist/ng-scrollbar.min.js +++ b/dist/ng-scrollbar.min.js @@ -1,8 +1,8 @@ /** * ng-scrollbar - * @version v0.0.7 - 2015-06-19 + * @version v0.0.8 - 2016-01-30 * @link https://github.com/asafdav/ng-scrollbar * @author Asaf David * @license MIT License, http://www.opensource.org/licenses/MIT */ -"use strict";angular.module("ngScrollbar",[]).directive("ngScrollbar",["$parse","$window",function(a,b){return{restrict:"A",replace:!0,transclude:!0,scope:{showYScrollbar:"=?isBarShown"},link:function(a,c,d){var e,g,h,i,j,k,l,m,n,o,p,q={bottom:d.hasOwnProperty("bottom")},r=angular.element(b),s=!!r[0].addEventListener,t=!!r[0].removeEventListener,u={top:0},v={top:0},w=function(){l={position:"relative",overflow:"hidden","max-width":"100%",height:"100%"},v.height&&(l.height=v.height+"px"),m={position:"absolute",height:u.height+"px",top:u.top+"px"},n={position:"relative","line-height":u.height+"px"},o={position:"relative",top:v.top+"px",overflow:"hidden"}},x=function(){i.css("top",u.top+"px");var a=u.top/v.height;v.top=-Math.round(v.scrollHeight*a),g.css("top",v.top+"px")},y=function(a){var b=a.hasOwnProperty("offsetY")?a.offsetY:a.layerY,c=Math.max(0,Math.min(parseInt(u.trackHeight,10)-parseInt(u.height,10),b));u.top=c,x(),a.stopPropagation()},z=function(a){var b=40,c=a,d=c.detail,e=c.wheelDelta,g=225,h=g-1;return d=d?e&&(f=e/d)?d/f:-d/1.35:e/120,d=1>d?-1>d?(-Math.pow(d,2)-h)/g:d:(Math.pow(d,2)+h)/g,a.delta=Math.min(Math.max(d/2,-1),1),a.delta=a.delta*b,u.top=Math.max(0,Math.min(parseInt(v.height,10)-parseInt(u.height,10),parseInt(u.top,10)-a.delta)),x(),a.preventDefault?void a.preventDefault():!1},A=0,B=function(a,b,c){u.top=Math.max(0,Math.min(parseInt(u.trackHeight,10)-parseInt(u.height,10),c)),a.stopPropagation()},C=function(a){var b=0,c=a.pageY-i[0].scrollTop-A;B(a,b,c),x()},D=function(a){r.off("mousemove",C),r.off("mouseup",D),a.stopPropagation()},E=function(a){var b=0,c=a.originalEvent.changedTouches[0].pageY-i[0].scrollTop-A;B(a,b,c),x()},F=function(a){r.off("touchmove",E),r.off("touchend",F),a.stopPropagation()},G=function(a){var b=void 0!==r[0].onmousewheel?"mousewheel":"DOMMouseScroll";s?a.addEventListener(b,z,!1):a.attachEvent("onmousewheel",z)},H=function(a){var b=void 0!==r[0].onmousewheel?"mousewheel":"DOMMouseScroll";t?a.removeEventListener(b,z,!1):a.detachEvent("onmousewheel",z)},I=function(b){b=q.bottom||b,e=angular.element(c.children()[0]),g=angular.element(e.children()[0]),h=angular.element(e.children()[1]),i=angular.element(angular.element(h.children()[0]).children()[0]),j=angular.element(i.children()[0]),k=angular.element(angular.element(h.children()[0]).children()[1]),v.height=c[0].offsetHeight,v.scrollHeight=g[0].scrollHeight,v.height
'}}]); \ No newline at end of file +"use strict";angular.module("ngScrollbar",[]).directive("ngScrollbar",["$parse","$window",function(a,b){return{restrict:"A",replace:!0,transclude:!0,scope:{showYScrollbar:"=?isBarShown"},link:function(a,c,d){var e,g,h,i,j,k,l,m,n,o,p,q={bottom:d.hasOwnProperty("bottom")},r=angular.element(b),s=!!r[0].addEventListener,t=!!r[0].removeEventListener,u={top:0},v={top:0},w=function(){l={position:"relative",overflow:"hidden","max-width":"100%",height:"100%"},v.height&&(l.height=v.height+"px"),m={position:"absolute",height:u.height+"px",top:u.top+"px"},n={position:"relative","line-height":u.height+"px"},o={position:"relative",top:v.top+"px",overflow:"hidden"}},x=function(){i.css("top",u.top+"px");var a=u.top/v.height;v.top=-Math.round(v.scrollHeight*a),g.css("top",v.top+"px")},y=function(a){var b=a.hasOwnProperty("offsetY")?a.offsetY:a.layerY,c=Math.max(0,Math.min(parseInt(u.trackHeight,10)-parseInt(u.height,10),b));u.top=c,x(),a.stopPropagation()},z=function(a){var b=40,c=a,d=c.detail,e=c.wheelDelta,g=225,h=g-1;return d=d?e&&(f=e/d)?d/f:-d/1.35:e/120,d=1>d?-1>d?(-Math.pow(d,2)-h)/g:d:(Math.pow(d,2)+h)/g,a.delta=Math.min(Math.max(d/2,-1),1),a.delta=a.delta*b,u.top=Math.max(0,Math.min(parseInt(v.height,10)-parseInt(u.height,10),parseInt(u.top,10)-a.delta)),x(),a.preventDefault?void a.preventDefault():!1},A=0,B=function(a,b,c){u.top=Math.max(0,Math.min(parseInt(u.trackHeight,10)-parseInt(u.height,10),c)),a.stopPropagation()},C=function(a){var b=0,c=a.pageY-i[0].scrollTop-A;B(a,b,c),x()},D=function(a){r.off("mousemove",C),r.off("mouseup",D),a.stopPropagation()},E=function(a){var b=0,c=a.originalEvent.changedTouches[0].pageY-i[0].scrollTop-A;B(a,b,c),x()},F=function(a){r.off("touchmove",E),r.off("touchend",F),a.stopPropagation()},G=function(a){var b=void 0!==r[0].onmousewheel?"mousewheel":"DOMMouseScroll";s?a.addEventListener(b,z,!1):a.attachEvent("onmousewheel",z)},H=function(a){var b=void 0!==r[0].onmousewheel?"mousewheel":"DOMMouseScroll";t?a.removeEventListener(b,z,!1):a.detachEvent("onmousewheel",z)},I=null,J=function(b){b=(q.bottom||b)&&(u.top===I||null===I),e=angular.element(c.children()[0]),g=angular.element(e.children()[0]),h=angular.element(e.children()[1]),i=angular.element(angular.element(h.children()[0]).children()[0]),j=angular.element(i.children()[0]),k=angular.element(angular.element(h.children()[0]).children()[1]),v.height=c[0].offsetHeight,v.scrollHeight=g[0].scrollHeight,v.height
'}}]); \ No newline at end of file diff --git a/src/ng-scrollbar.js b/src/ng-scrollbar.js index 8fabf0d..9f20a1f 100644 --- a/src/ng-scrollbar.js +++ b/src/ng-scrollbar.js @@ -159,9 +159,10 @@ angular.module('ngScrollbar', []).directive('ngScrollbar', [ } }; - var buildScrollbar = function (rollToBottom) { + var lastBottom = null; - rollToBottom = flags.bottom || rollToBottom; + var buildScrollbar = function (rollToBottom) { + rollToBottom = (flags.bottom || rollToBottom) && (dragger.top === lastBottom || lastBottom === null); mainElm = angular.element(element.children()[0]); transculdedContainer = angular.element(mainElm.children()[0]); tools = angular.element(mainElm.children()[1]); @@ -210,9 +211,10 @@ angular.module('ngScrollbar', []).directive('ngScrollbar', [ event.preventDefault(); }); + lastBottom = parseInt(page.height, 10) - parseInt(dragger.height, 10); + if (rollToBottom) { - flags.bottom = false; - dragger.top = parseInt(page.height, 10) - parseInt(dragger.height, 10); + dragger.top = lastBottom; } else { dragger.top = Math.max(0, Math.min(parseInt(page.height, 10) - parseInt(dragger.height, 10), parseInt(dragger.top, 10))); } From 4e939bf7ab4088aa9e482e644da6b736a731f730 Mon Sep 17 00:00:00 2001 From: Bryson Date: Sat, 30 Jan 2016 13:30:38 -0800 Subject: [PATCH 2/2] Use the parent element for height, not the element itself. --- src/ng-scrollbar.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ng-scrollbar.js b/src/ng-scrollbar.js index 9f20a1f..68680e4 100644 --- a/src/ng-scrollbar.js +++ b/src/ng-scrollbar.js @@ -170,7 +170,7 @@ angular.module('ngScrollbar', []).directive('ngScrollbar', [ thumbLine = angular.element(thumb.children()[0]); track = angular.element(angular.element(tools.children()[0]).children()[1]); - page.height = element[0].offsetHeight; + page.height = element[0].offsetParent.offsetHeight; page.scrollHeight = transculdedContainer[0].scrollHeight; if (page.height < page.scrollHeight) {