jquery-imgview.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * 图片查看
  3. * createname:雨中磐石
  4. * homeurl:http://www.rockoa.com/
  5. * Copyright (c) 2016 rainrock (xh829.com)
  6. * Date:2016-01-01
  7. */
  8. (function ($) {
  9. function get(id){return document.getElementById(id)};
  10. function funclass(opts, obj){
  11. if(!opts)opts={};
  12. var me = this;
  13. this.ismobile = false;
  14. this.downbool = true;
  15. this.dushu = 0;
  16. this.onloadsuccess=function(){};
  17. this.init=function(){
  18. if(get('imgview_main'))return;
  19. for(var i in opts)this[i]=opts[i];
  20. if(obj)this.url=obj.attr('src');
  21. this.mheiht = document.body.scrollHeight,sed=$(window).height();
  22. if(this.mheiht<sed)this.mheiht=sed;
  23. this.showview();
  24. };
  25. this.remove=function(){
  26. $('#imgview_main').remove();
  27. };
  28. this.showview=function(){
  29. var s='<div id="imgview_main" style="position:absolute;left:0px;top:0px;width:100%;height:100%;z-index:800">';
  30. s+='<div style="position:absolute;z-index:0;left:0px;top:0px;width:100%;height:'+this.mheiht+'px;background-color:rgba(0,0,0,0.6)" id="imgview_mask"></div>';
  31. s+='<span onclick="$(this).parent().remove()" style="position:fixed;z-index:2;top:2px;right:5px;color:white"><i class="icon-remove"></i></span>';
  32. s+='<div id="imgview_span" style="position:fixed;z-index:1;left:47%;top:47%;overflow:hidden;color:white">';
  33. s+=' <div id="imgview_spanmask" style="position:absolute;z-index:1;left:0px;top:0px;background-color:rgba(0,0,0,0);width:100%;height:100%;cursor:move;user-select:none;-moz-user-select:none;-webkit-user-select:none;-ms-user-select:none;-khtml-user-select:none;"></div>';
  34. s+=' <img style="position:absolute;z-index:0;left:0px;top:0px" id="imgview_spanimg" width="100%" height="100%" src="images/mloading.gif" >';
  35. s+='</div>';
  36. s+='<div style="position:fixed;z-index:2;left:0px;bottom:0px;text-align:center;color:white;width:100%;font-size:20px;background-color:rgba(0,0,0,0.2);height:40px;line-height:40px;overflow:hidden;user-select:none"><i style="cursor:pointer" id="imgview_zoom-out" class="icon-zoom-out" title="缩小"></i> &nbsp; <span id="imgview_nowbili" style="font-size:14px">100%</span> &nbsp; <i style="cursor:pointer" class="icon-zoom-in" title="放大" id="imgview_zoom-in"></i>';
  37. s+=' &nbsp; <i style="cursor:pointer" class="icon-move" title="原始大小" id="imgview_zoom-move"></i>';
  38. if(!this.ismobile && this.downbool)s+=' &nbsp; <a target="_blank" download="" style="color:white;font-size:20px" href="'+this.url+'"><i style="cursor:pointer" class="icon-download-alt" title="下载"></i></a>';
  39. s+=' &nbsp; <i style="cursor:pointer" class="icon-refresh" title="旋转90度" id="imgview_zoom-refresh"></i>';
  40. s+='</div>';
  41. s+='</div>';
  42. $('body').append(s);
  43. $('#imgview_mask').click(function(){
  44. $('#imgview_main').remove();
  45. });
  46. this.showez(32,32,0);
  47. var img = new Image();
  48. img.src = this.url;
  49. img.onload = function(){
  50. if(get('imgview_main')){
  51. me.showez(this.width,this.height, 1);
  52. try{
  53. $('#imgview_span').mousewheel(function(e){
  54. me.bilixxx(e.deltaY*0.1);
  55. });}catch(e){}
  56. me.rotate(me.dushu);
  57. me.initmove();
  58. }
  59. me.onloadsuccess(this);
  60. }
  61. img.onerror=function(e){
  62. $('#imgview_span').html('无图');
  63. }
  64. $('#imgview_zoom-out').click(function(){
  65. me.bilixxx(-0.1);
  66. });
  67. $('#imgview_zoom-in').click(function(){
  68. me.bilixxx(0.1);
  69. });
  70. $('#imgview_zoom-refresh').click(function(){
  71. me.clickrotate();
  72. });
  73. $('#imgview_zoom-move').click(function(){
  74. me.bl=1;
  75. me.rotate(0);
  76. me.bilixxx(0);
  77. });
  78. };
  79. this.showez=function(w, h, lx){
  80. this.width = w;
  81. this.height = h;
  82. var zw = $(window).width(),zh=$(window).height();
  83. var wm = zw-50,wh = zh-50;
  84. var bl= 1,nw=w,nh=h;
  85. if(w>wm){
  86. bl=wm/w;
  87. nh = h*bl;
  88. }
  89. if(nh>wh){
  90. bl= wh/h;
  91. }
  92. this.showbl(bl,lx);
  93. };
  94. this.showbl=function(bl,lx){
  95. this.bl = bl;
  96. $('#imgview_nowbili').html(''+parseInt(bl*100)+'%');
  97. var zw = $(window).width(),zh=$(window).height();
  98. var nw = this.width*this.bl,nh=this.height*this.bl;
  99. var l = (zw-nw)*0.5,t = (zh-nh)*0.5;
  100. var arr = {left:''+l+'px',top:''+t+'px',width:''+nw+'px',height:''+nh+'px'};
  101. var o1 = $('#imgview_span');
  102. if(lx!=2){
  103. if(lx==1)get('imgview_spanimg').src=this.url;
  104. o1.css(arr);
  105. }else{
  106. o1.stop();
  107. o1.animate(arr,300);
  108. }
  109. };
  110. this.bilixxx=function(lx){
  111. var bl = this.bl+lx;
  112. if(bl<0)bl=0.05;
  113. if(bl>3)bl=3;
  114. this.showbl(bl,2);
  115. };
  116. this.initmove=function(){
  117. if(this.ismobile){
  118. this.movehammer();
  119. return;
  120. }
  121. var o = $('#imgview_spanmask');
  122. var x=0,y=0,oldl,oldt;
  123. o.mousedown(function(e){
  124. x=e.clientX;
  125. y=e.clientY;
  126. var o1=get('imgview_span');
  127. oldl = parseInt(o1.style.left);
  128. oldt = parseInt(o1.style.top);
  129. me.movebo=true;
  130. });
  131. o.mousemove(function(e){
  132. if(!me.movebo)return;
  133. var _x = e.clientX-x,_y=e.clientY-y;
  134. $('#imgview_span').css({left:''+(oldl+_x)+'px',top:''+(oldt+_y)+'px'});
  135. });
  136. o.mouseup(function(e){
  137. me.movebo=false;
  138. });
  139. };
  140. this.rotate=function(ds){
  141. var o = get('imgview_span');
  142. var val= "rotate("+ds+"deg)";
  143. o.style.transform=val;
  144. o.style.webkitTransform=val;
  145. o.style.msTransform=val;
  146. o.style.MozTransform=val;
  147. o.style.OTransform=val;
  148. };
  149. this.clickrotate=function(){
  150. this.dushu+=90;
  151. if(this.dushu>=360)this.dushu=0;
  152. this.rotate(this.dushu);
  153. };
  154. this.movehammer=function(){
  155. var o = get('imgview_spanmask');
  156. var x=0,y=0,oldl,oldt;
  157. this.touchci = 0;
  158. o.addEventListener('touchstart',function(e){
  159. me.touchci++;
  160. x=e.touches[0].clientX;
  161. y=e.touches[0].clientY;
  162. var o1=get('imgview_span');
  163. oldl = parseInt(o1.style.left);
  164. oldt = parseInt(o1.style.top);
  165. me.movebo=true;
  166. clearTimeout(me.touctimes);
  167. me.touctimes = setTimeout(function(){me.touchci=0},200);
  168. });
  169. o.addEventListener('touchmove',function(e){
  170. e.preventDefault();
  171. if(!me.movebo)return;
  172. var _x = e.touches[0].clientX-x,_y=e.touches[0].clientY-y;
  173. $('#imgview_span').css({left:''+(oldl+_x)+'px',top:''+(oldt+_y)+'px'});
  174. });
  175. o.addEventListener('touchend',function(e){
  176. me.movebo=false;
  177. if(me.touchci==2){
  178. me.bilixxx(0.1);
  179. me.touchci=0;
  180. }
  181. });
  182. }
  183. this.loadimg=function(){
  184. return "data:image/svg+xml,%3C%3Fxml version='1.0' encoding='UTF-8'%3F%3E%3Csvg width='80px' height='80px' viewBox='0 0 80 80' version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'%3E%3Ctitle%3Eloading%3C/title%3E%3Cdefs%3E%3ClinearGradient x1='94.0869141%25' y1='0%25' x2='94.0869141%25' y2='90.559082%25' id='linearGradient-1'%3E%3Cstop stop-color='%23606060' stop-opacity='0' offset='0%25'%3E%3C/stop%3E%3Cstop stop-color='%23606060' stop-opacity='0.3' offset='100%25'%3E%3C/stop%3E%3C/linearGradient%3E%3ClinearGradient x1='100%25' y1='8.67370605%25' x2='100%25' y2='90.6286621%25' id='linearGradient-2'%3E%3Cstop stop-color='%23606060' offset='0%25'%3E%3C/stop%3E%3Cstop stop-color='%23606060' stop-opacity='0.3' offset='100%25'%3E%3C/stop%3E%3C/linearGradient%3E%3C/defs%3E%3Cg stroke='none' stroke-width='1' fill='none' fill-rule='evenodd' opacity='0.9'%3E%3Cg%3E%3Cpath d='M40,0 C62.09139,0 80,17.90861 80,40 C80,62.09139 62.09139,80 40,80 L40,73 C58.2253967,73 73,58.2253967 73,40 C73,21.7746033 58.2253967,7 40,7 L40,0 Z' fill='url(%23linearGradient-1)'%3E%3C/path%3E%3Cpath d='M40,0 L40,7 C21.7746033,7 7,21.7746033 7,40 C7,58.2253967 21.7746033,73 40,73 L40,80 C17.90861,80 0,62.09139 0,40 C0,17.90861 17.90861,0 40,0 Z' fill='url(%23linearGradient-2)'%3E%3C/path%3E%3Ccircle id='Oval' fill='%23606060' cx='40.5' cy='3.5' r='3.5'%3E%3C/circle%3E%3C/g%3E%3CanimateTransform attributeName='transform' begin='0s' dur='1s' type='rotate' values='0 40 40;360 40 40' repeatCount='indefinite'/%3E%3C/g%3E%3C/svg%3E%0A";
  185. }
  186. }
  187. $.imgview = function(options){
  188. var cls = new funclass(options,false);
  189. cls.init();
  190. return cls;
  191. }
  192. $.fn.imgview = function(options){
  193. var cls = new funclass(options, $(this));
  194. cls.init();
  195. return cls;
  196. }
  197. $.imgviewclose= function(){
  198. var bo = get('imgview_main');
  199. $('#imgview_main').remove();
  200. return bo;
  201. }
  202. })(jQuery);
粤ICP备19079148号