-
Notifications
You must be signed in to change notification settings - Fork 0
/
counting-number-browser.min.js
1 lines (1 loc) · 2.18 KB
/
counting-number-browser.min.js
1
function _toPrimitive(t,e){if("object"!=typeof t||!t)return t;var i=t[Symbol.toPrimitive];if(void 0!==i){var r=i.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}function _toPropertyKey(t){var e=_toPrimitive(t,"string");return"symbol"==typeof e?e:e+""}function _defineProperty(t,e,i){return(e=_toPropertyKey(e))in t?Object.defineProperty(t,e,{value:i,enumerable:!0,configurable:!0,writable:!0}):t[e]=i,t}class CountingNumber extends HTMLElement{constructor(){super(),this.startTime=null}connectedCallback(){const t=CountingNumber.defaults;this.typedValue=this.getAttribute("value")||this.textContent,this.targetValue=Number(this.typedValue)||t.targetValue,this.duration=Number(this.getAttribute("duration"))||t.duration,this.delay=Number(this.getAttribute("delay"))||t.delay,this.culture=this.getLanguage(t.culture),this.decimals=CountingNumber.getDecimalCount(this.typedValue)||t.decimals,this.formatter=new Intl.NumberFormat(this.culture,{minimumFractionDigits:this.decimals,maximumFractionDigits:this.decimals}),this.currentValue=0,this.updateValue();const e=new IntersectionObserver((t=>{t.forEach((t=>{t.isIntersecting&&(setTimeout((()=>{requestAnimationFrame(this.animateCount.bind(this))}),this.delay),e.unobserve(this))}))}),{threshold:0});e.observe(this)}animateCount(t){this.startTime||(this.startTime=t);const e=(t-this.startTime)/this.duration,i=(r=e)<.5?4*r*r*r:(r-1)*(2*r-2)*(2*r-2)+1;var r;this.currentValue=Math.min(this.targetValue*i,this.targetValue),this.updateValue(),this.currentValue<this.targetValue&&requestAnimationFrame(this.animateCount.bind(this))}updateValue(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.currentValue;this.textContent=this.formatter.format(t)}getLanguage(t){let e=t;const i=this.closest("[lang]");return i&&""!=i.lang&&(e=i.lang),e}static getDecimalCount(t){let e=null!=t?t.toString():"0";return e.indexOf(".")<0?0:e.split(".")[1].length||0}}function registerElement(){customElements.define("counting-number",CountingNumber)}_defineProperty(CountingNumber,"defaults",{value:0,targetValue:0,duration:1e3,delay:0,decimals:0,culture:"en"}),registerElement();