forked from KoryNunn/scroll-into-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrollIntoView.min.js
1 lines (1 loc) · 3.66 KB
/
scrollIntoView.min.js
1
"use strict";var COMPLETE="complete",CANCELED="canceled";function raf(a){return"requestAnimationFrame"in window?window.requestAnimationFrame(a):void setTimeout(a,16)}function setElementScroll(a,b,c){var d=Math.max;d(0,b),d(0,c),a.self===a?a.scrollTo(b,c):(a.scrollLeft=b,a.scrollTop=c)}function getTargetScrollLocation(a,b){var e,f,g,h,i,j,k,c=Math.min,d=Math.max,l=a.align,m=a.target,n=m.getBoundingClientRect(),o=l&&null!=l.left?l.left:.5,p=l&&null!=l.top?l.top:.5,q=l&&null!=l.leftOffset?l.leftOffset:0,r=l&&null!=l.topOffset?l.topOffset:0,s=o,t=p;if(a.isWindow(b))j=c(n.width,b.innerWidth),k=c(n.height,b.innerHeight),f=n.left+b.pageXOffset-b.innerWidth*s+j*s,g=n.top+b.pageYOffset-b.innerHeight*t+k*t,f-=q,g-=r,f=a.align.lockX?b.pageXOffset:f,g=a.align.lockY?b.pageYOffset:g,h=f-b.pageXOffset,i=g-b.pageYOffset;else{j=n.width,k=n.height,e=b.getBoundingClientRect();var u=n.left-(e.left-b.scrollLeft),v=n.top-(e.top-b.scrollTop);f=u+j*s-b.clientWidth*s,g=v+k*t-b.clientHeight*t,f-=q,g-=r,f=d(c(f,b.scrollWidth-b.clientWidth),0),g=d(c(g,b.scrollHeight-b.clientHeight),0),f=a.align.lockX?b.scrollLeft:f,g=a.align.lockY?b.scrollTop:g,h=f-b.scrollLeft,i=g-b.scrollTop}return{x:f,y:g,differenceX:h,differenceY:i}}function animate(a){var b=Math.min,c=a._scrollSettings;if(c){var d=c.maxSynchronousAlignments,e=getTargetScrollLocation(c,a),f=Date.now()-c.startTime,g=b(1/c.time*f,1);if(c.endIterations>=d)return setElementScroll(a,e.x,e.y),a._scrollSettings=null,c.end(COMPLETE);var h=1-c.ease(g);return setElementScroll(a,e.x-e.differenceX*h,e.y-e.differenceY*h),f>=c.time?(c.endIterations++,c.scrollAncestor&&animate(c.scrollAncestor),void animate(a)):void raf(animate.bind(null,a))}}function defaultIsWindow(a){return a.self===a}function transitionScrollTo(a,b,c,d,e){function f(a){b._scrollSettings=null,b.parentElement&&b.parentElement._scrollSettings&&b.parentElement._scrollSettings.end(a),c.debug&&console.log("Scrolling ended with type",a,"for",b),e(a),g&&(b.removeEventListener("touchstart",g,k),b.removeEventListener("wheel",g,k))}var g,h=!b._scrollSettings,i=b._scrollSettings,j=Date.now(),k={passive:!0};i&&i.end(CANCELED);var l=c.maxSynchronousAlignments;return null==l&&(l=3),b._scrollSettings={startTime:j,endIterations:0,target:a,time:c.time,ease:c.ease,align:c.align,isWindow:c.isWindow||defaultIsWindow,maxSynchronousAlignments:l,end:f,scrollAncestor:d},(!("cancellable"in c)||c.cancellable)&&(g=f.bind(null,CANCELED),b.addEventListener("touchstart",g,k),b.addEventListener("wheel",g,k)),h&&animate(b),g}function defaultIsScrollable(a){return"pageXOffset"in a||(a.scrollHeight!==a.clientHeight||a.scrollWidth!==a.clientWidth)&&"hidden"!==getComputedStyle(a).overflow}function defaultValidTarget(){return!0}function findParentElement(a){if(a.assignedSlot)return findParentElement(a.assignedSlot);if(a.parentElement)return"BODY"===a.parentElement.tagName?a.parentElement.ownerDocument.defaultView||a.parentElement.ownerDocument.ownerWindow:a.parentElement;if(a.getRootNode){var b=a.getRootNode();if(11===b.nodeType)return b.host}}module.exports=function(a,b,c){function d(a){f--,!f&&c&&c(a)}if(a){"function"==typeof b&&(c=b,b=null),b||(b={}),b.time=isNaN(b.time)?1e3:b.time,b.ease=b.ease||function(a){var b=Math.pow;return 1-b(1-a,a/2)},b.align=b.align||{};var e=findParentElement(a),f=1,g=b.validTarget||defaultValidTarget,h=b.isScrollable;b.debug&&(console.log("About to scroll to",a),!e&&console.error("Target did not have a parent, is it mounted in the DOM?"));for(var i=[];e;)if(b.debug&&console.log("Scrolling parent node",e),g(e,f)&&(h?h(e,defaultIsScrollable):defaultIsScrollable(e))&&(f++,i.push(e)),e=findParentElement(e),!e){d(COMPLETE);break}return i.reduce(function(c,e,f){return transitionScrollTo(a,e,b,i[f+1],d)},null)}};