-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhover-preview.min.js
1 lines (1 loc) · 1.27 KB
/
hover-preview.min.js
1
"use strict";!function(){function r(i){var n;o.el&&(u.start(),o.index=(o.index+1)%o.images.length,((n=o.images[o.index])?new Promise(function(e,t){var i=new Image;i.onload=function(){e(n)},i.onerror=function(){t(new Error("There is an error on loading image ".concat(n)))},i.src=n}):Promise.reject(new Error("Empty url"))).then(function(){function e(){o.setSrc(),r()}var t=u.stop();500<t|i?e():s.set(e,500-t)}).catch(function(){r()}))}function t(e){o.el=e.target,o.poster=o.el.getAttribute("src"),o.index=-1,o.images=o.el.getAttribute("data-preview").split("|"),s.set(function(){return r(!0)},70)}function i(){o.reset(),s.reset()}var e=Array.from(document.querySelectorAll(".hover-preview")),s={id:0,set:function(e,t){clearTimeout(this.id),this.id=setTimeout(e,t)},reset:function(){clearTimeout(this.id)}},o={el:null,poster:null,index:null,images:null,setSrc:function(){this.el&&this.el.setAttribute("src",this.images[this.index])},reset:function(){this.el&&this.el.setAttribute("src",this.poster),this.el=this.poster=this.index=this.images=null}},u={previous:null,start:function(){this.previous=Date.now()},stop:function(){var e=this.previous?Date.now()-this.previous:0;return this.previous=null,e}};e.forEach(function(e){e.addEventListener("mouseenter",t),e.addEventListener("mouseleave",i)})}();