-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgazeimg.gquery.min.js
12 lines (12 loc) · 11.6 KB
/
gazeimg.gquery.min.js
1
2
3
4
5
6
7
8
9
10
11
12
// ==================================================
//
// GazeImg.gQuery.js v1.2.0
// (c) 2020-present, JU Chengren (Ganxiaozhe)
//
// Licensed GPLv3 for open source use
// or GazeImg Commercial License for commercial use
//
// gquery.cn/plugins/gazeimg
//
// ==================================================
"use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};!function(e,a,t){if(!e)throw new Error("GazeImg.js need gQuery: gquery.cn");console.log("%c GazeImg v1.2.0 %c gquery.cn/plugins/gazeimg \n","color: #fff; background: #030307; padding:5px 0; margin-top: 1em;","background: #efefef; color: #333; padding:5px 0;");var s={s:{close:0,move:0,queue:[],index:0},fn:{getTran:function(e){var a=e.css("transform").replace(/[^0-9\-,.]/g,"").split(",");return 6!=a.length&&(a=[0,0,0,0,0,0]),a.map(function(e){return parseInt(e)})},pxToNum:function(e){return"string"==typeof e?parseInt(e.replace(/[^\d]/g,"")):e}},iloader:function(e){return new Promise(function(a,t){var s=new Image;s.onload=function(){a(s)},s.onerror=function(e){t(e)},s.src=e,s.complete&&a(s)})},isizeCalc:function(e){for(var a in e)isNaN(e[a])&&"string"==typeof e[a]&&(e[a]=parseInt(e[a].replace("px","")));return e.w=e.ow,e.h=e.oh,e.iz=0,e.percW=e.ow/e.ww,e.percH=e.oh/e.wh,(e.percW>1||e.percH>1)&&(e.percW>e.percH?(e.w=e.ww,e.h=e.w/e.ow*e.oh):(e.h=e.wh,e.w=e.h/e.oh*e.ow),e.iz=1),e},ishowBind:function(){e("img[data-gishow]").each(function(a){var t={$i:e(this)};t.group=t.$i.attr("data-gishow"),t.group&&t.$i.attr("data-gigp",t.group),this.removeAttribute("data-gishow"),this.classList.add("gi-click")}).on("click",function(){var a={i:this,$i:e(this),arr:[],idx:0,src:e(this).attr("src"),title:e(this).attr("data-title")||""};a.arr.push({src:a.src,title:a.title}),a.group=a.$i.attr("data-gigp"),e('img[data-gigp="'+a.group+'"').each(function(e){0==e&&(a.arr.length=0);var t={src:this.getAttribute("src"),title:this.getAttribute("data-title")||""};a.arr.push(t),a.i==this&&(a.idx=e)}),s.show(a.arr,a.idx)})},show:function(a,t){var i={};t||(t=0),a=a.map(function(e){return"object"!==(void 0===e?"undefined":_typeof(e))?{src:e,title:""}:e}),s.s.queue=a,s.s.index=t,i.stage="<div class='gazeimg-container'><div class='gazeimg-bg'></div><div class='gazeimg-inner'><div class='gazeimg-nav'><div class='gazeimg-pages'>1 / 1</div><div class='gazeimg-opts'><span class='gazeimg-opt spin'><i class='gazeimg-opt-spin'></i></span><span class='gazeimg-opt close'><i class='gazeimg-opt-close'></i></span></div></div><div class='gazeimg-stage'><div class='gazeimg-slide current'><div class='gazeimg-content'><div class='loader'></div></div></div></div><div class='gazeimg-footer'><div class='gazeimg-title'></div></div></div></div>",e("body").append(i.stage).addClass("gi-nobar"),s.navUpdate(),s.sprepare(),s.sbind(),s.sloader(e(".gazeimg-slide.current > .gazeimg-content"),a[t]),e(".gazeimg-opt").on("click",function(a){a.stopPropagation();var t=e(this),i=this.className.replace("gazeimg-opt ",""),o=t.parent(),r=e(".gazeimg-slide.current > .gazeimg-content > img");if(o.hasClass("disabled"))return!1;switch(o.addClass("disabled").wait(500).removeClass("disabled"),i){case"close":s.showClose();break;case"spin":var n="";n=r.hasClass("spin1")?"spin2":r.hasClass("spin2")?"spin3":r.hasClass("spin3")?"spin4":"spin1",r.removeClass("spin1 spin2 spin3 spin4").addClass(n),"spin4"==n&&(r.wait(250).css("transition","none").removeClass("spin4"),r.wait(500).css("transition",""))}})},sprepare:function(){if(!(s.s.queue.length<2)){var a={$stage:e(".gazeimg-stage")},t=s.s.queue,i=s.s.index;e(".gazeimg-pages").text(i+1+" / "+t.length),e(".gazeimg-container").addClass("grabbing"),e(".gazeimg-slide:not(.current)").remove(),e(".gazeimg-slide.current").css({transform:"translate(0px, 0px)"}),i>0&&(a.$stage.append("<div class='gazeimg-slide prev'><div class='gazeimg-content'><div class='loader'></div></div></div>"),s.sloader(e(".gazeimg-slide.prev > .gazeimg-content"),t[i-1])),i<t.length-1&&(a.$stage.append("<div class='gazeimg-slide next'><div class='gazeimg-content'><div class='loader'></div></div></div>"),s.sloader(e(".gazeimg-slide.next > .gazeimg-content"),t[i+1])),e(".gazeimg-container").removeClass("grabbing x-grabbing")}},sloader:function(a,t){var i={};a.html("<div class='loader'></div>"),i.$stage=e(".gazeimg-stage"),i.stageW=i.$stage.width(),i.stageH=i.$stage.height(),s.iloader(t.src).then(function(e){var o=s.isizeCalc({ww:i.stageW,wh:i.stageH,ow:e.width,oh:e.height});i.cls=o.iz?"zoom-in":"zoom-out",a.data({iW:o.w,iH:o.h,inW:o.ow,inH:o.oh,isZoom:o.iz,zoom:0}).html("<img src='"+t.src+"'/>").css({width:o.w+"px",height:o.h+"px"}).addClass(i.cls),a.find("img").on("mousedown",function(e){e.preventDefault()})}).catch(function(e){throw a.data({isZoom:0,zoom:0}).css({width:"auto"}).html("<div class='dialog'>图片加载失败!</div>"),new Error(e)})},sbind:function(){e(".gazeimg-container").on("click",function(){s.s.move>0||(s.s.close++,setTimeout(function(){1==s.s.close&&s.showClose()},50))}),e(".gazeimg-container").on({mousedown:s.slideMove,mouseup:s.slideMoveEnd,touchstart:s.slideMove,touchend:s.slideMoveEnd},".gazeimg-slide"),e(".gazeimg-container").on({click:function(){if(!(s.s.move>0)){s.s.close--;var a={$obj:e(this)};if(1!=a.$obj.data("isZoom"))return void s.showClose();1==a.$obj.data("zoom")?(a.iW=a.$obj.data("iW"),a.iH=a.$obj.data("iH"),a.zoom=0):(a.iW=a.$obj.data("inW"),a.iH=a.$obj.data("inH"),a.zoom=1),a.cls=a.zoom?"grab":"zoom-in",a.$obj.data("zoom",a.zoom).css({width:a.iW+"px",height:a.iH+"px",transform:"translate(0px, 0px)"}).removeClass("zoom-in grab").addClass(a.cls)}},mousedown:s.showMove,mouseup:s.showMoveEnd,touchstart:s.showMove,touchend:s.showMoveEnd},".gazeimg-content")},slideMove:function(i){var o=e(this),r={};"mousedown"==i.type&&1!=i.which||1==o.find(".gazeimg-content").data("zoom")||(r.tran=s.fn.getTran(o),e(".gazeimg-container").addClass("grabbing"),r.evt="touchstart"==i.type?"touchmove.drag":"mousemove.drag",this.setCapture&&this.setCapture(),e(t).on(r.evt,function(t){if("touchmove"==t.type?(r.L=r.tran[4]+t.targetTouches[0].pageX-i.targetTouches[0].pageX,r.T=r.tran[5]+t.targetTouches[0].pageY-i.targetTouches[0].pageY):(r.L=r.tran[4]+t.pageX-i.pageX,r.T=r.tran[5]+t.pageY-i.pageY),r.type||(s.s.move=1,r.type=Math.abs(r.T)>Math.abs(r.L)?"y":"x",s.s.queue.length<2&&(r.type="y"),"x"==r.type&&e(".gazeimg-container").addClass("x-grabbing")),"y"==r.type)r.opa=1-Math.abs(parseInt(r.T))/300,r.opa<0&&(r.opa=0),o.css({transform:"translate(0px,"+r.T+"px)",opacity:r.opa});else{var n=e(".gazeimg-slide.prev"),g=e(".gazeimg-slide.next");r.ww=a.innerWidth,r.xperc=r.L/r.ww,r.xperc<0?s.s.index>=s.s.queue.length-1&&(r.L/=4,r.xperc=0):s.s.index<1&&(r.L/=4,r.xperc=0),o.css({transform:"translate("+r.L+"px,0px)"}),n.css({transform:"translate("+(r.L-r.ww)+"px,0px)"}),g.css({transform:"translate("+(r.L+r.ww)+"px,0px)"})}s.s.slidePos=r}),s.s.slidePos=r)},slideMoveEnd:function(a){var i=s.s.slidePos,o=e(this);if(1!=o.find(".gazeimg-content").data("zoom")&&(e(".gazeimg-container").removeClass("grabbing"),setTimeout(function(){s.s.move=0},50),this.releaseCapture&&this.releaseCapture(),e(t).off(i.evt),"y"==i.type&&(i.opa<.5?s.showClose():o.css({transform:"translate(0px, 0px)",opacity:1})),"x"==i.type)){var r=e(".gazeimg-slide.prev"),n=e(".gazeimg-slide.next");if(Math.abs(i.xperc)<.2)return r.css({transform:"translate("+-i.ww+"px, 0px)"}),o.css({transform:"translate(0px, 0px)"}),n.css({transform:"translate("+i.ww+"px, 0px)"}),void setTimeout(function(){e(".gazeimg-container").removeClass("x-grabbing")},300);if(i.xperc<0)return s.s.index++,s.navUpdate(),o.css({transform:"translate("+-i.ww+"px, 0px)"}),n.css({transform:"translate(0px, 0px)"}),void setTimeout(function(){o.removeClass("current").addClass("prev"),n.removeClass("next").addClass("current"),s.sprepare()},300);s.s.index--,s.navUpdate(),o.css({transform:"translate("+i.ww+"px, 0px)"}),r.css({transform:"translate(0px, 0px)"}),setTimeout(function(){o.removeClass("current").addClass("next"),r.removeClass("prev").addClass("current"),s.sprepare()},300)}},navUpdate:function(){var a=s.s.queue[s.s.index];e(".gazeimg-title").text(a.title)},showMove:function(a){var i=e(this),o={};"mousedown"==a.type&&1!=a.which||0==i.data("zoom")||(o.tran=s.fn.getTran(i),i.addClass("grabbing"),o.evt="touchstart"==a.type?"touchmove.drag":"mousemove.drag",this.setCapture&&this.setCapture(),e(t).on(o.evt,function(e){s.s.move=1,"touchmove"==e.type?(o.L=o.tran[4]+e.targetTouches[0].pageX-a.targetTouches[0].pageX,o.T=o.tran[5]+e.targetTouches[0].pageY-a.targetTouches[0].pageY):(o.L=o.tran[4]+e.pageX-a.pageX,o.T=o.tran[5]+e.pageY-a.pageY),i.css({transform:"translate("+o.L+"px,"+o.T+"px)"})}),s.s.pos=o)},showMoveEnd:function(a){var i=s.s.pos,o=e(this);0!=o.data("zoom")&&(o.removeClass("grabbing"),setTimeout(function(){s.s.move=0},50),this.releaseCapture&&this.releaseCapture(),e(t).off(i.evt))},showClose:function(){e(".gazeimg-container").fadeOut(400),e(".gazeimg-container .gazeimg-inner").addClass("gia-zoomOut"),e(".gazeimg-content").off(),setTimeout(function(){e(".gazeimg-container").off().remove(),e("body").removeClass("gi-nobar"),s.s.close=0},400)}};e.fn.extend({gazeimg:function(a){a||"object"!==(void 0===a?"undefined":_typeof(a))&&(a={}),Array.isArray(a.bg)||(a.bg=["linear-gradient(to right, #C3E1CA, #E6E1BD)","linear-gradient(to right, #D4D3DD, #EFEFBB)","linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%)","linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%)","linear-gradient(to bottom, rgba(255,255,255,0.15) 0%, rgba(0,0,0,0.15) 100%)","linear-gradient(to top, #c1dfc4 0%, #deecdd 100%)","linear-gradient(to top, #d5d4d0 0%, #d5d4d0 1%, #eeeeec 31%, #efeeec 75%, #e9e9e7 100%);"]),this.each(function(t){var i={$i:e(this),cls:this.className};if(i.calc={w:this.offsetWidth,h:this.offsetHeight},i.$p=i.$i.parent(),i.src=i.$i.attr("data-gisrc"),i.$i.attr("data-gi-ready",1).removeAttr("data-gisrc"),a.class&&(i.cls=a.class),i.de=i.src.split(" "),i._di=!1,i.de.length>1)i.src=i.de[0],i._de=i.de[1].split("="),"size"==i._de[0]&&(i._di=i._de[1].replace(/['"]/g,"").split(","));else{var o=i.src.split("_"),r=o[o.length-1].replace(/\..*/,"").split("x");r.length>1&&(i._di=r)}if(Array.isArray(i._di)&&(i.calc=s.isizeCalc({ww:i.$p.width(),wh:i.$p.height(),ow:i._di[0],oh:i._di[1]})),i.w=isNaN(i.calc.w)?"100%":i.calc.w+"px",i.h=isNaN(i.calc.h)?"100%":i.calc.h+"px",i.isrc=i.$i.attr("src"),i.isrc)return i.$i.data("giSRC",i.src).attr("data-gi-prepare",1).removeAttr("data-gi-ready"),s.__observer.observe(this),!0;i.ohtml=this.outerHTML,i.cls=i.cls?" class='"+i.cls+"'":"",i.style=" style='width:"+i.w+";height:"+i.h+";background:"+a.bg[Math.floor(Math.random()*a.bg.length)]+"'",i.html="<div id='giQL-"+t+"' data-gi-prepare"+i.cls+i.style+"><div class='loader'></div></div>",this.outerHTML=i.html;var n=e("#giQL-"+t).data({giHTML:i.ohtml,giSRC:i.src}).removeAttr("id").addClass("gi-loading")[0];s.__observer.observe(n)})}}),s.__observer=new IntersectionObserver(function(a){a.forEach(function(a){if(!(a.intersectionRatio<=0)){var t={$obj:e(a.target)};t.src=t.$obj.removeAttr("data-gi-prepare").data("giSRC"),s.__observer.unobserve(a.target),s.iloader(t.src).then(function(){if("img"==t.$obj[0].tagName.toLowerCase())return t.$obj.attr("src",t.src).addClass("gia-fadeIn"),void s.ishowBind();var a=t.$obj.data("giHTML");t.$obj.ohtml(a),e("img[data-gi-ready]").attr("src",t.src).removeAttr("data-gi-ready").addClass("gia-fadeIn"),s.ishowBind()},function(){var e=t.$obj.addClass("gi-loading gi-failed").attr("class");t.$obj.ohtml("<div class='"+e+"'><a href=\""+t.src+'" target="_blank">图片 '+t.src+" 加载失败;</a></div>")})}})}),e("img[data-gisrc]").gazeimg(),e.extend({gazeimg:{show:s.show,showBind:s.ishowBind,loader:s.iloader,sizeCalc:s.isizeCalc}})}(gQuery,window,document);