joshua-arrival-listener
Version:
adjust whether element reach / leave the edge of viewport. Also can set offset.
1 lines • 4.63 kB
JavaScript
!function(t,o){"object"==typeof exports&&"object"==typeof module?module.exports=o():"function"==typeof define&&define.amd?define([],o):"object"==typeof exports?exports.ArrivalListener=o():t.ArrivalListener=o()}(this,function(){return function(t){function o(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,o),i.l=!0,i.exports}var e={};return o.m=t,o.c=e,o.i=function(t){return t},o.d=function(t,e,n){o.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:n})},o.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return o.d(e,"a",e),e},o.o=function(t,o){return Object.prototype.hasOwnProperty.call(t,o)},o.p="",o(o.s=0)}([function(t,o,e){"use strict";function n(t,o){if(!(t instanceof o))throw new TypeError("Cannot call a class as a function")}function i(t){return 0<t&&t<1}function r(t){return i(t)?"*":"-"}Object.defineProperty(o,"__esModule",{value:!0});var a=function(){function t(t,o){for(var e=0;e<o.length;e++){var n=o[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(o,e,n){return e&&t(o.prototype,e),n&&t(o,n),o}}(),h={"+":function(t,o){return t+o},"-":function(t,o){return t-o},"*":function(t,o){return t*o},"/":function(t,o){return t/o}},m=function(){function t(o){n(this,t),this.offsetTopEnterBottom=0,this.offsetTopLeaveBottom=0,this.offsetBottomReachTop=0,this.offsetBottomEnterBottom=0,this.offsetBottomLeaveBottom=0,this.onTopEnterBottom=function(){},this.onTopLeaveBottom=function(){},this.onBottomEnterTop=function(){},this.onBottomLeaveTop=function(){},this.onBottomEnterBottom=function(){},this.onBottomLeaveBottom=function(){};for(var e in o)this[e]=o[e];this.entranceHandler(!0),window.addEventListener("scroll",this.entranceHandler.bind(this)),window.addEventListener("resize",this.entranceHandler.bind(this))}return a(t,[{key:"entranceHandler",value:function(t){var o=window.innerHeight,e=this.el.getBoundingClientRect(),n=getComputedStyle(this.el).transform,i=this.parseTransform(n),r={top:e.top-(i[5]||0),bottom:e.bottom,left:e.left,right:e.right,width:e.width,height:e.height};this.adjustEdge(o,r,t)}},{key:"computeFlag",value:function(t,o){var e=void 0,n={bottomReachTop:"function"==typeof this.offsetBottomReachTop?this.offsetBottomReachTop():this.offsetBottomReachTop,topEnterBottom:"function"==typeof this.offsetTopEnterBottom?this.offsetTopEnterBottom():this.offsetTopEnterBottom,topLeaveBottom:"function"==typeof this.offsetTopLeaveBottom?this.offsetTopLeaveBottom():this.offsetTopLeaveBottom,bottomEnterBottom:"function"==typeof this.offsetBottomEnterBottom?this.offsetBottomEnterBottom():this.offsetBottomEnterBottom,bottomLeaveBottom:"function"==typeof this.offsetBottomLeaveBottom?this.offsetBottomLeaveBottom():this.offsetBottomLeaveBottom},i=o.bottom+n.bottomReachTop<=0,a=o.bottom+n.bottomReachTop>0;e=r(n.topEnterBottom);var m=o.top<=h[e](t,n.topEnterBottom);e=r(n.topLeaveBottom);var s=o.top>h[e](t,n.topLeaveBottom);e=r(n.bottomEnterBottom);var f=o.bottom<=h[e](t,n.bottomEnterBottom);return e=r(n.bottomLeaveBottom),{topHigherThanBottom:m,topLowerThanBottom:s,bottomHigherThanTop:i,bottomLowerThanTop:a,bottomHigherThanBottom:f,bottomLowerThanBottom:o.bottom>h[e](t,n.bottomLeaveBottom)}}},{key:"adjustEdge",value:function(t,o,e){var n=this.computeFlag(t,o);e===!0&&(n.topHigherThanBottom&&this.onTopEnterBottom.call(this.el),n.topLowerThanBottom&&this.onTopLeaveBottom.call(this.el),n.bottomHigherThanTop&&this.onBottomLeaveTop.call(this.el),n.bottomLowerThanTop&&this.onBottomEnterTop.call(this.el),n.bottomHigherThanBottom&&this.onBottomEnterBottom.call(this.el),n.bottomLowerThanBottom&&this.onBottomLeaveBottom.call(this.el)),n.topHigherThanBottom&&!this.flagTopReachBottom&&(this.flagTopReachBottom=!0,this.onTopEnterBottom.call(this.el)),n.topLowerThanBottom&&this.flagTopReachBottom&&(this.flagTopReachBottom=!1,this.onTopLeaveBottom.call(this.el)),n.bottomHigherThanTop&&!this.flagBottomReachTop&&(this.flagBottomReachTop=!0,this.onBottomLeaveTop.call(this.el)),n.bottomLowerThanTop&&this.flagBottomReachTop&&(this.flagBottomReachTop=!1,this.onBottomEnterTop.call(this.el)),n.bottomHigherThanBottom&&!this.flagBottomReachBottom&&(this.flagBottomReachBottom=!0,this.onBottomEnterBottom.call(this.el)),n.bottomLowerThanBottom&&this.flagBottomReachBottom&&(this.flagBottomReachBottom=!1,this.onBottomLeaveBottom.call(this.el))}},{key:"parseTransform",value:function(t){return t.split(/\(|,|\)/).slice(1,-1).map(function(t){return parseFloat(t)})}}]),t}();o.default=m,t.exports=o.default}])});