forked from Bounteous-Inc/scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll-tracker.min.js
8 lines (8 loc) · 4.44 KB
/
scroll-tracker.min.js
1
2
3
4
5
6
7
8
!function(a){"use strict";function b(c){if(!(this instanceof b))return new b(c);c=c||{};var d=c.context||"body";if("string"==typeof d&&(d=m.querySelector(d)),!d)throw new Error("Unable to find context "+d);this._context=d,this.minHeight=c.minHeight||0,this._marks={},this._tracked={},this._config={percentages:{each:{},every:{}},pixels:{each:{},every:{}},elements:{each:{},every:{}}};var f=k(this._checkDepth.bind(this),500),g=this._update.bind(this),h=k(g,500);a.addEventListener("scroll",f,!0),a.addEventListener("resize",h),this._artifacts={timer:e(g),resize:h,scroll:f}}function c(a){return a.handlers.map(function(b){return b.bind(this,{data:{depth:a.depth,label:a.label}})})}function d(a){var b,c=Math.floor(a.numerator/a.n);for(b=1;b<=c;b++)a.callback(b*a.n)}function e(a){var b=f();return setInterval(function(){f()!==b&&(a(),b=f())},500)}function f(){var a=m.body,b=m.documentElement;return Math.max(a.scrollHeight,a.offsetHeight,b.clientHeight,b.scrollHeight,b.offsetHeight)}function g(a){var b=a.offsetHeight,c=h(),d=a.getBoundingClientRect();return Math.max(0,d.top>0?Math.min(b,c-d.top):d.bottom<c?d.bottom:c)}function h(){return("CSS1Compat"===m.compatMode?m.documentElement:m.body).clientHeight}function i(b){return b.getBoundingClientRect().top+(void 0!==a.pageYOffset?a.pageYOffset:(m.documentElement||m.body.parentNode||m.body).scrollTop)}function j(){}function k(a,b){var c,d,e,f=null,g=0,h=function(){g=new Date,f=null,e=a.apply(c,d)};return function(){var i=new Date;g||(g=i);var j=b-(i-g);return c=this,d=arguments,j<=0?(clearTimeout(f),f=null,g=i,e=a.apply(c,d)):f||(f=setTimeout(h,j)),e}}function l(){var c,d={};for(c in b)d[c]=j;a.ScrollTracker=d}if(a.navigator.userAgent.match(/MSIE [678]/gi))return l();var m=a.document;b.prototype.destroy=function(){clearInterval(this._artifacts._timer),a.removeEventListener("resize",this._artifacts.resize),a.removeEventListener("scroll",this._artifacts.scroll,!0)},b.prototype.on=function(a,b){var c=this._config;["percentages","pixels","elements"].forEach(function(d){a[d]&&["each","every"].forEach(function(e){a[d][e]&&a[d][e].forEach(function(a){c[d][e][a]=c[d][e][a]||[],c[d][e][a].push(b)})})}),this._update()},b.prototype._update=function(){this._calculateMarks(),this._checkDepth()},b.prototype._calculateMarks=function(){function a(a,b){return function(b,c){var d=b.getBoundingClientRect().top-m._context.getBoundingClientRect().top;l({label:a+"["+c+"]",depth:d,handlers:j.elements.every[a]})}}function b(a){return function(a){var b=Math.floor(a*k/100);l({label:String(a)+"%",depth:b,handlers:j.percentages.every[h]})}}function c(a){return function(b){var c=b;l({label:String(c)+"px",depth:c,handlers:a})}}delete this._marks,this._fromTop=i(this._context),this._marks={};var e,f,g,h,j=this._config,k=this._contextHeight(),l=this._addMark.bind(this),m=this;if(!(k<this.minHeight)){for(h in j.percentages.every)d({n:Number(h),numerator:100,callback:b(j.percentages.every[h])});for(h in j.pixels.every)d({n:Number(h),numerator:k,callback:c(j.pixels.every[h])});for(h in j.percentages.each)g=Math.floor(k*Number(h)/100),l({label:h+"%",depth:g,handlers:j.percentages.each[h]});for(h in j.pixels.each)g=Number(h),l({label:h+"px",depth:g,handlers:j.pixels.each[h]});for(h in j.elements.every)e=[].slice.call(this._context.querySelectorAll(h)),e.length&&e.forEach(a(h,j.elements.every[h]));for(h in j.elements.each)(f=this._context.querySelector(h))&&(g=f.getBoundingClientRect().top-m._context.getBoundingClientRect().top,l({label:h,depth:g,handlers:j.elements.each[h]}))}},b.prototype._checkDepth=function(){var a,b=this._marks,c=this._currentDepth();for(a in b)c>=a&&!this._tracked[a]&&(b[a].forEach(function(a){a()}),this._tracked[a]=!0)},b.prototype.reset=function(){this._tracked={}},b.prototype._contextHeight=function(){return this._context!==m.body?this._context.scrollHeight-5:this._context.clientHeight-5},b.prototype._currentDepth=function(){var b,c=g(this._context);return this._context.scrollTop?b=this._context.scrollTop+c:(this._context.scrollTop=1,this._context.scrollTop?(this._context.scrollTop=0,b=this._context.scrollTop+c):b=a.pageYOffset||m.documentElement.scrollTop||m.body.scrollTop||0),c?b+c:b>=this._fromTop?b:-1},b.prototype._addMark=function(a){var b=a.depth;this._marks[b]=(this._marks[b]||[]).concat(c(a))},a.ScrollTracker=b}(this);
/*
* v2.0.0
* Created by the Google Analytics consultants at http://www.lunametrics.com/
* Written by @notdanwilkerson
* Documentation: https://github.com/lunametrics/gascroll/
* Licensed under the MIT License
*/