jquery-imgview.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. for(var i in opts)this[i]=opts[i];
  19. if(obj)this.url=obj.attr('src');
  20. this.mheiht = document.body.scrollHeight,sed=$(window).height();
  21. if(this.mheiht<sed)this.mheiht=sed;
  22. this.showview();
  23. };
  24. this.remove=function(){
  25. $('#imgview_main').remove();
  26. };
  27. this.showview=function(){
  28. var s='<div id="imgview_main" style="position:absolute;left:0px;top:0px;width:100%;height:100%;z-index:800">';
  29. 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>';
  30. s+='<span onclick="$(this).parent().remove()" style="position:fixed;z-index:2;top:2px;right:5px;color:white"><i class="icon-remove"></i></span>';
  31. s+='<div id="imgview_span" style="position:fixed;z-index:1;left:47%;top:47%;overflow:hidden;color:white">';
  32. 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>';
  33. s+=' <img style="position:absolute;z-index:0;left:0px;top:0px" id="imgview_spanimg" width="100%" height="100%" src="images/mloading.gif" >';
  34. s+='</div>';
  35. 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"><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>';
  36. s+=' &nbsp; <i style="cursor:pointer" class="icon-move" title="原始大小" id="imgview_zoom-move"></i>';
  37. 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>';
  38. s+=' &nbsp; <i style="cursor:pointer" class="icon-refresh" title="旋转90度" id="imgview_zoom-refresh"></i>';
  39. s+='</div>';
  40. s+='</div>';
  41. $('body').append(s);
  42. $('#imgview_mask').click(function(){
  43. $('#imgview_main').remove();
  44. });
  45. this.showez(32,32,0);
  46. var img = new Image();
  47. img.src = this.url;
  48. img.onload = function(){
  49. if(get('imgview_main')){
  50. me.showez(this.width,this.height, 1);
  51. try{
  52. $('#imgview_span').mousewheel(function(e){
  53. me.bilixxx(e.deltaY*0.1);
  54. });}catch(e){}
  55. me.rotate(me.dushu);
  56. me.initmove();
  57. }
  58. me.onloadsuccess(this);
  59. }
  60. img.onerror=function(e){
  61. $('#imgview_span').html('无图');
  62. }
  63. $('#imgview_zoom-out').click(function(){
  64. me.bilixxx(-0.1);
  65. });
  66. $('#imgview_zoom-in').click(function(){
  67. me.bilixxx(0.1);
  68. });
  69. $('#imgview_zoom-refresh').click(function(){
  70. me.clickrotate();
  71. });
  72. $('#imgview_zoom-move').click(function(){
  73. me.bl=1;
  74. me.rotate(0);
  75. me.bilixxx(0);
  76. });
  77. };
  78. this.showez=function(w, h, lx){
  79. this.width = w;
  80. this.height = h;
  81. var zw = $(window).width(),zh=$(window).height();
  82. var wm = zw-50,wh = zh-50;
  83. var bl= 1,nw=w,nh=h;
  84. if(w>wm){
  85. bl=wm/w;
  86. nh = h*bl;
  87. }
  88. if(nh>wh){
  89. bl= wh/h;
  90. }
  91. this.showbl(bl,lx);
  92. };
  93. this.showbl=function(bl,lx){
  94. this.bl = bl;
  95. $('#imgview_nowbili').html(''+parseInt(bl*100)+'%');
  96. var zw = $(window).width(),zh=$(window).height();
  97. var nw = this.width*this.bl,nh=this.height*this.bl;
  98. var l = (zw-nw)*0.5,t = (zh-nh)*0.5;
  99. var arr = {left:''+l+'px',top:''+t+'px',width:''+nw+'px',height:''+nh+'px'};
  100. var o1 = $('#imgview_span');
  101. if(lx!=2){
  102. if(lx==1)get('imgview_spanimg').src=this.url;
  103. o1.css(arr);
  104. }else{
  105. o1.stop();
  106. o1.animate(arr,300);
  107. }
  108. };
  109. this.bilixxx=function(lx){
  110. var bl = this.bl+lx;
  111. if(bl<0)bl=0.05;
  112. if(bl>3)bl=3;
  113. this.showbl(bl,2);
  114. };
  115. this.initmove=function(){
  116. if(this.ismobile){
  117. this.movehammer();
  118. return;
  119. }
  120. var o = $('#imgview_spanmask');
  121. var x=0,y=0,oldl,oldt;
  122. o.mousedown(function(e){
  123. x=e.clientX;
  124. y=e.clientY;
  125. var o1=get('imgview_span');
  126. oldl = parseInt(o1.style.left);
  127. oldt = parseInt(o1.style.top);
  128. me.movebo=true;
  129. });
  130. o.mousemove(function(e){
  131. if(!me.movebo)return;
  132. var _x = e.clientX-x,_y=e.clientY-y;
  133. $('#imgview_span').css({left:''+(oldl+_x)+'px',top:''+(oldt+_y)+'px'});
  134. });
  135. o.mouseup(function(e){
  136. me.movebo=false;
  137. });
  138. };
  139. this.rotate=function(ds){
  140. var o = get('imgview_span');
  141. var val= "rotate("+ds+"deg)";
  142. o.style.transform=val;
  143. o.style.webkitTransform=val;
  144. o.style.msTransform=val;
  145. o.style.MozTransform=val;
  146. o.style.OTransform=val;
  147. };
  148. this.clickrotate=function(){
  149. this.dushu+=90;
  150. if(this.dushu>=360)this.dushu=0;
  151. this.rotate(this.dushu);
  152. };
  153. this.movehammer=function(){
  154. var o = get('imgview_spanmask');
  155. var x=0,y=0,oldl,oldt;
  156. this.touchci = 0;
  157. o.addEventListener('touchstart',function(e){
  158. me.touchci++;
  159. x=e.touches[0].clientX;
  160. y=e.touches[0].clientY;
  161. var o1=get('imgview_span');
  162. oldl = parseInt(o1.style.left);
  163. oldt = parseInt(o1.style.top);
  164. me.movebo=true;
  165. clearTimeout(me.touctimes);
  166. me.touctimes = setTimeout(function(){me.touchci=0},200);
  167. });
  168. o.addEventListener('touchmove',function(e){
  169. e.preventDefault();
  170. if(!me.movebo)return;
  171. var _x = e.touches[0].clientX-x,_y=e.touches[0].clientY-y;
  172. $('#imgview_span').css({left:''+(oldl+_x)+'px',top:''+(oldt+_y)+'px'});
  173. });
  174. o.addEventListener('touchend',function(e){
  175. me.movebo=false;
  176. if(me.touchci==2){
  177. me.bilixxx(0.1);
  178. me.touchci=0;
  179. }
  180. });
  181. }
  182. }
  183. $.imgview = function(options){
  184. var cls = new funclass(options,false);
  185. cls.init();
  186. return cls;
  187. }
  188. $.fn.imgview = function(options){
  189. var cls = new funclass(options, $(this));
  190. cls.init();
  191. return cls;
  192. }
  193. $.imgviewclose= function(){
  194. var bo = get('imgview_main');
  195. $('#imgview_main').remove();
  196. return bo;
  197. }
  198. })(jQuery);