Няма описание

jquery.photo.gallery.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * AppGo使用的 图片查看插件
  3. * Author : lufeng@bingosoft.net
  4. * Version: 1.0.0
  5. * Date : 2015/11/17
  6. */
  7. (function($) {
  8. var windowMargin = 8; //加多边距的宽高,使得图片看起来有边框效果
  9. //图片查看器
  10. $.fn.extend({
  11. photoGallery: function(options) {
  12. var isFirefox = navigator.userAgent.indexOf("Firefox") > -1 ;
  13. var MOUSEWHEEL_EVENT = isFirefox ? "DOMMouseScroll" : "mousewheel";
  14. var defaults = {
  15. //图片缩放倍率
  16. ratio : 1.2,
  17. //右下角缩略图宽度
  18. thumbnailsWidth : 180,
  19. //右下角缩略图高度
  20. thumbnailsHeight : 120,
  21. //HTML模版
  22. template : {
  23. //操作工具
  24. OPERTATION : '<div class="oper">' +
  25. '<span class="prev"><i class="icon_tool-prev"></i></span>' +
  26. '<span class="next"><i class="icon_tool-next"></i></span>' +
  27. '</div>' +
  28. '<div class="tool">' +
  29. '<div class="toolct">' +
  30. '<span class="oper_fullscreen" title="查看全屏"><i class="icon_tool-fullscreen"></i></span>' +
  31. '<span class="oper_bigger" title="放大图片"><i class="icon_tool-bigger"></i></span>' +
  32. '<span class="oper_smaller" title="缩小图片"><i class="icon_tool-smaller"></i></span>' +
  33. '<span class="oper_rotate" title="向右旋转"><i class="icon_tool-rotate"></i></span>' +
  34. // '<span class="oper_download" title="下载图片"><i class="icon_tool-download"></i></span>' +
  35. '</div>' +
  36. '</div>',
  37. //缩略图
  38. THUMBNAILS : "<div class='thumbnails'>" +
  39. '<span class="thumbClose" title="关闭缩略图"><i class="icon_close-small"></i></span>' +
  40. '<img ondragstart="return false;"/>' +
  41. '<div class="thumbDrag"><span></span></div>' +
  42. "</div>",
  43. //大图
  44. IMAGE : '<img class="image" ondragstart="return false;"/>'
  45. }
  46. };
  47. var o = $.extend(defaults, options),
  48. $gallery = $(this);
  49. $gallery.append(o.template.OPERTATION).append(o.template.THUMBNAILS);
  50. var $tool = $(this).find(".tool"),
  51. $fullscreen = $(this).find(".oper_fullscreen"),
  52. $bigger = $(this).find(".oper_bigger"),
  53. $smaller = $(this).find(".oper_smaller"),
  54. $rotate = $(this).find(".oper_rotate"),
  55. $download = $(this).find(".oper_download"),
  56. $prev = $(this).find(".prev"),
  57. $next = $(this).find(".next"),
  58. $thumbnails = $(this).find(".thumbnails"),
  59. $image,
  60. $thumbImg,
  61. imageWidth,
  62. imageHeight,
  63. imgRatio,
  64. dragX,
  65. dragY,
  66. cW,
  67. cH,
  68. w,h,isVertical,
  69. thumbX,
  70. thumbY;
  71. //上一张
  72. $prev.on('click',function(){
  73. if(o.activeIndex > 0) o.activeIndex--;
  74. toggleImage();
  75. }).on("mouseover",function(e){
  76. if(o.activeIndex > 0)
  77. $(this).addClass("active");
  78. }).on("mouseout",function(e){
  79. $(this).removeClass("active");
  80. });
  81. //下一张
  82. $next.on('click',function(){
  83. if(o.activeIndex < o.imgs.length -1) o.activeIndex++;
  84. toggleImage();
  85. }).on("mouseover",function(e){
  86. if(o.activeIndex < o.imgs.length -1)
  87. $(this).addClass("active");
  88. }).on("mouseout",function(e){
  89. $(this).removeClass("active");
  90. });
  91. //缩略图
  92. $thumbnails.css({
  93. height: o.thumbnailsHeight,
  94. width : o.thumbnailsWidth
  95. }).on("mouseenter",function(e){
  96. thumbX = -1;
  97. }).on("mousedown",function(e){
  98. thumbX=e.pageX || e.clientX;
  99. thumbY=e.pageY || e.clientY;
  100. cW = document.body.clientWidth;
  101. cH = document.body.clientHeight;
  102. e.stopPropagation();
  103. }).on("mousemove",function(e){
  104. if(thumbX > 0){
  105. var nextDragX=e.pageX || e.clientX;
  106. var nextDragY=e.pageY || e.clientY;
  107. var $td= $(this).find(".thumbDrag"),
  108. imageWidth = $image.width(),
  109. imageHeight = $image.height(),
  110. thumbImgWidth = $thumbImg.width(),
  111. thumbImgHeight = $thumbImg.height(),
  112. left =parseFloat($td.css("left")) + (nextDragX - thumbX),
  113. top =parseFloat($td.css("top")) + (nextDragY - thumbY),
  114. w = $td.width(),
  115. h = $td.height(),
  116. it,
  117. il,
  118. maxL,
  119. maxT;
  120. if(isVertical){
  121. thumbImgWidth = [thumbImgHeight, thumbImgHeight = thumbImgWidth][0];
  122. imageWidth = [imageHeight, imageHeight = imageWidth][0];
  123. }
  124. it = (o.thumbnailsHeight - thumbImgHeight) / 2 ,
  125. il = (o.thumbnailsWidth - thumbImgWidth) / 2,
  126. maxL = o.thumbnailsWidth - w - il - 2, //减去2像素边框部分
  127. maxT = o.thumbnailsHeight - h - it - 2;
  128. if(left < il ) left = il;
  129. else if(left > maxL) left = maxL;
  130. if(top < it ) top = it;
  131. else if(top > maxT) top = maxT;
  132. $td.css({
  133. left : left,
  134. top : top
  135. })
  136. thumbX=nextDragX;
  137. thumbY=nextDragY;
  138. if(imageWidth < cW) left = (cW - imageWidth) / 2;
  139. else left = -imageWidth * (left-il) / thumbImgWidth;
  140. if(imageHeight < cH ) top = (cH - imageHeight) / 2;
  141. else top = -imageHeight * (top-it) / thumbImgHeight;
  142. $image.offset({
  143. left : left,
  144. top : top
  145. });
  146. }
  147. }).on("mouseup",function(e){
  148. thumbX = -1;
  149. });
  150. $thumbnails.find(".thumbClose").on("click",function(){
  151. $thumbnails.hide();
  152. });
  153. //显示工具栏
  154. $gallery.on("mouseover",function(e){
  155. $tool.show();
  156. }).on("mouseenter",function(e){
  157. dragX = -1;
  158. }).on("mouseout",function(e){
  159. $tool.hide();
  160. }).on("mousedown",function(e){
  161. dragX=e.pageX || e.clientX;
  162. dragY=e.pageY || e.clientY;
  163. cW = document.body.clientWidth;
  164. cH = document.body.clientHeight;
  165. e.stopPropagation();
  166. }).on("mousemove",function(e){
  167. if(dragX > 0){
  168. var nextDragX=e.pageX || e.clientX;
  169. var nextDragY=e.pageY || e.clientY ;
  170. var o = $image.offset(),
  171. left =o.left + (nextDragX - dragX),
  172. top =o.top + (nextDragY - dragY),
  173. w = $image.width(),
  174. h = $image.height();
  175. if(isVertical){
  176. w = [h, h = w][0];
  177. }
  178. if(w > cW){
  179. if(left > 0){
  180. left = 0 ;
  181. }
  182. else if(left < cW - w){
  183. left = cW - w;
  184. }
  185. }else{
  186. left = o.left;
  187. }
  188. if(h > cH){
  189. if(top > 0){
  190. top = 0 ;
  191. }
  192. else if(top < cH - h){
  193. top = cH - h;
  194. }
  195. } else{
  196. top = o.top;
  197. }
  198. $image.offset({
  199. left : left,
  200. top : top
  201. });
  202. dragX=nextDragX;
  203. dragY=nextDragY;
  204. setThumbnails(); //缩略图拖拽点
  205. }
  206. }).on("mouseup",function(e){
  207. dragX = -1;
  208. });
  209. //全屏
  210. var isMax,preWidth, preHeight, preTop, preLeft;
  211. $fullscreen.on("click", function(){
  212. var parentD = window.parent.document,
  213. J = $(parentD.getElementById("J_pg"));
  214. if(!isMax){
  215. isMax = true;
  216. preWidth = document.body.clientWidth;
  217. preHeight = document.body.clientHeight;
  218. preTop = J.css("top");
  219. preLeft = J.css("left");
  220. J.css({
  221. top: 0,
  222. left : 0,
  223. width : parentD.body.clientWidth,
  224. height : parentD.body.clientHeight,
  225. });
  226. } else{
  227. isMax = false;
  228. J.css({
  229. top: preTop,
  230. left : preLeft,
  231. width : preWidth,
  232. height : preHeight
  233. });
  234. }
  235. });
  236. //放大图片
  237. $bigger.on("click", function(){
  238. biggerImage();
  239. });
  240. //缩小图片
  241. $smaller.on("click", function(){
  242. smallerImage();
  243. });
  244. //旋转
  245. $rotate.on("click", function(){
  246. var rotateClass = $image.attr("class").match(/(rotate)(\d*)/);
  247. if(rotateClass){
  248. var nextDeg = (rotateClass[2] * 1 + 90) % 360;
  249. $image.removeClass(rotateClass[0]).addClass("rotate" + nextDeg);
  250. $thumbImg.removeClass(rotateClass[0]).addClass("rotate" + nextDeg);
  251. resizeImage(nextDeg);
  252. resizeThumbImg(nextDeg);
  253. isVertical = nextDeg == 90 || nextDeg == 270;
  254. } else{
  255. $image.addClass("rotate90");
  256. $thumbImg.addClass("rotate90");
  257. resizeImage("90");
  258. resizeThumbImg("90");
  259. isVertical = true;
  260. }
  261. });
  262. //下载
  263. $download.on("click", function(){
  264. var imgUrl = $image.attr("src");
  265. if(!imgUrl) return;
  266. alert("没有找到兼容所有浏览器方法,所以暂不实现");
  267. });
  268. $(window).on("resize",function(){
  269. setImagePosition();
  270. });
  271. if(document.attachEvent){
  272. document.attachEvent("on"+MOUSEWHEEL_EVENT, function(e){
  273. mouseWheelScroll(e);
  274. });
  275. } else if(document.addEventListener){
  276. document.addEventListener(MOUSEWHEEL_EVENT, function(e){
  277. mouseWheelScroll(e);
  278. }, false);
  279. }
  280. function mouseWheelScroll(e){
  281. var _delta = parseInt(e.wheelDelta || -e.detail);
  282. //向上滚动
  283. if (_delta > 0) {
  284. biggerImage();
  285. }
  286. //向下滚动
  287. else {
  288. smallerImage();
  289. }
  290. }
  291. //键盘左右键
  292. document.onkeydown = function(e){
  293. e = e || window.event;
  294. if (e.keyCode) {
  295. if(e.keyCode == 37 ){ //left
  296. if(o.activeIndex > 0) o.activeIndex--;
  297. toggleImage();
  298. }
  299. if(e.keyCode == 39 ){ //right
  300. if(o.activeIndex < o.imgs.length -1) o.activeIndex++;
  301. toggleImage();
  302. }
  303. }
  304. };
  305. function init(){
  306. toggleImage();
  307. $(o.imgs).each(function(i, img){
  308. $(o.template.IMAGE)
  309. .appendTo($gallery)
  310. .attr("src", img.url)
  311. .attr("index", i)
  312. .css({
  313. width : img.imgWidth,
  314. height : img.imgHeight,
  315. left : (cW - img.imgWidth)/2,
  316. top: (cH - img.imgHeight)/2
  317. }).on("dblclick", function(){
  318. app.window.close();
  319. }); ;
  320. });
  321. $image = $(".image[index='"+o.activeIndex+"']", $gallery).addClass("active");
  322. }
  323. function toggleImage(){
  324. imageWidth = o.imgs[o.activeIndex].imgWidth;
  325. imageHeight = o.imgs[o.activeIndex].imgHeight;
  326. imgRatio = imageWidth/ imageHeight;
  327. cW = document.body.clientWidth;
  328. cH = document.body.clientHeight;
  329. $(".image", $gallery).removeClass("active");
  330. $image = $(".image[index='"+o.activeIndex+"']", $gallery).addClass("active").css({
  331. width : imageWidth,
  332. height : imageHeight
  333. }).removeClass("rotate0 rotate90 rotate180 rotate270");
  334. $thumbImg = $thumbnails.find("img").attr("src", o.imgs[o.activeIndex].url);
  335. $thumbnails.find("img").removeAttr("class").removeAttr("style");
  336. isVertical = false;
  337. $thumbnails.hide();
  338. $prev.removeClass("active");
  339. $next.removeClass("active");
  340. setImagePosition();
  341. }
  342. function biggerImage(){
  343. var w = $image.width(),
  344. h = $image.height(),
  345. nextW = w * o.ratio,
  346. nextH = h * o.ratio;
  347. if(nextW - w < 1) nextW = Math.ceil(nextW);
  348. var percent = (nextW / imageWidth * 100).toFixed(0) ;
  349. if(percent > 90 && percent < 110){
  350. percent = 100;
  351. nextW = imageWidth;
  352. nextH = imageHeight;
  353. }
  354. else if(percent > 1600) {
  355. percent = 1600;
  356. nextW = imageWidth * 16;
  357. nextH = imageHeight * 16;
  358. }
  359. $image.width(nextW).height(nextH);
  360. setImagePosition();
  361. showPercentTip(percent);
  362. showThumbnails(nextW, nextH);
  363. }
  364. function smallerImage(){
  365. var w = $image.width(),
  366. h = $image.height(),
  367. nextW,
  368. nextH;
  369. var percent = (w / o.ratio / imageWidth * 100).toFixed(0) ;
  370. if(percent < 5) {
  371. percent = 5;
  372. nextW = imageWidth / 20;
  373. nextH = imageHeight / 20;
  374. }
  375. else if(percent > 90 && percent < 110){
  376. percent = 100;
  377. nextW = imageWidth;
  378. nextH = imageHeight;
  379. } else{
  380. nextW = w / o.ratio;
  381. nextH = h / o.ratio;
  382. }
  383. $image.width(nextW).height(nextH);
  384. setImagePosition();
  385. showPercentTip(percent);
  386. showThumbnails(nextW, nextH);
  387. }
  388. //显示缩略图
  389. function showThumbnails(width, height){
  390. if(isVertical) width = [height, height = width][0];
  391. if(width > document.body.clientWidth || height > document.body.clientHeight){
  392. $thumbnails.show();
  393. setThumbnails();
  394. } else{
  395. $thumbnails.hide();
  396. }
  397. }
  398. //重置图片宽高
  399. function resizeImage(rotateDeg){
  400. var mH = document.body.clientHeight - windowMargin,
  401. mW = document.body.clientWidth - windowMargin;
  402. if(rotateDeg == '90' || rotateDeg == '270'){
  403. mW = [mH, mH = mW][0];
  404. }
  405. var width, height;
  406. width = Math.min(imageWidth, mW);
  407. height = Math.min(imageHeight, mH);
  408. if(width / height > imgRatio){
  409. width = height * imgRatio;
  410. } else{
  411. height = width / imgRatio;
  412. }
  413. $image.css({
  414. width:width,
  415. height:height
  416. });
  417. setImagePosition();
  418. }
  419. function resizeThumbImg(rotateDeg){
  420. var maxW = o.thumbnailsWidth, maxH = o.thumbnailsHeight;
  421. if(rotateDeg == '90' || rotateDeg == '270'){
  422. maxW = [maxH, maxH = maxW][0];
  423. }
  424. $thumbImg.css({
  425. maxWidth : maxW,
  426. maxHeight : maxH
  427. });
  428. $thumbnails.hide();
  429. }
  430. //显示百分比提示
  431. function showPercentTip(percent){
  432. $gallery.find(".percentTip").remove();
  433. $("<div class='percentTip'><span>"+percent+"%</span></div>").appendTo($gallery).fadeOut(1500);
  434. }
  435. //设置图片位置
  436. function setImagePosition(){
  437. var w = $image.width(),
  438. h = $image.height(),
  439. cW = document.body.clientWidth,
  440. cH = document.body.clientHeight;
  441. var left = (cW - w)/2,
  442. top = (cH - h)/2;
  443. $image.css("left", left +"px").css("top", top+"px");
  444. }
  445. //设置缩略图拖拽区域
  446. function setThumbnails(){
  447. var $img = $thumbnails.find("img"),
  448. sW = $img.width(),
  449. sH = $img.height(),
  450. w = $image.width(),
  451. h = $image.height(),
  452. imf = $image.offset(),
  453. imfl = imf.left,
  454. imft = imf.top,
  455. cW = document.body.clientWidth,
  456. cH = document.body.clientHeight,
  457. tW,
  458. tH,
  459. tl,
  460. tt;
  461. if(isVertical){
  462. sW = [sH, sH = sW][0];
  463. w = [h, h = w][0];
  464. }
  465. tW = sW / (w / cW);
  466. if(w < cW) tW = sW;
  467. tH = sH / (h / cH);
  468. if(h < cH) tH = sH;
  469. tl = (o.thumbnailsWidth - sW)/2 + -imfl/w * sW ;
  470. if(w < cW) tl = (o.thumbnailsWidth - sW)/2;
  471. tt = (o.thumbnailsHeight - sH)/2 + -imft/h * sH ;
  472. if(h < cH) tt = (o.thumbnailsHeight - sH)/2;
  473. $thumbnails.find(".thumbDrag").css({
  474. width: tW,
  475. height: tH,
  476. left: tl,
  477. top: tt
  478. });
  479. }
  480. init();
  481. return this;
  482. }
  483. });
  484. $.extend({
  485. //打开图片查看器
  486. openPhotoGallery : function(obj){
  487. var $img = $(obj),
  488. imgUrl = $img[0].src;
  489. if(!imgUrl) return;
  490. //HTML5提供了一个新属性naturalWidth/naturalHeight可以直接获取图片的原始宽高
  491. var img = $img[0],
  492. imgHeight = img.naturalHeight,
  493. imgWidth = img.naturalWidth,
  494. ratio = imgWidth / imgHeight,
  495. wH = 415,
  496. wW = 615,
  497. winHeight,
  498. winWidth,
  499. maxHeight = document.body.clientHeight - windowMargin * 2,
  500. maxWidth = document.body.clientWidth- windowMargin;
  501. winWidth = Math.max(wW, imgWidth);
  502. winHeight = Math.max(wH, imgHeight);
  503. if(winWidth > maxWidth) {
  504. winWidth = maxWidth;
  505. winHeight =Math.max(wH, Math.ceil(winWidth / ratio));
  506. if(imgWidth > winWidth) {
  507. imgWidth = winWidth;
  508. imgHeight = Math.ceil(imgWidth / ratio);
  509. }
  510. }
  511. if(winHeight > maxHeight) {
  512. winHeight = maxHeight;
  513. winWidth = Math.max(wW, Math.ceil(winHeight * ratio));
  514. if(imgHeight > winHeight) {
  515. imgHeight = winHeight;
  516. imgWidth = Math.ceil(imgHeight * ratio);
  517. }
  518. }
  519. var $gallerys = $(obj).closest(".gallerys"),
  520. activeIndex=0,
  521. imgs = [];
  522. $gallerys.find(".gallery-pic").each(function(i, elem){
  523. var url = this.src,
  524. img = $(this)[0],
  525. nH = img.naturalHeight,
  526. nW = img.naturalWidth,
  527. ratio = nW / nH,
  528. w = nW,
  529. h = nH;
  530. if(url == imgUrl){
  531. activeIndex = i;
  532. w = imgWidth;
  533. h = imgHeight;
  534. }
  535. else{
  536. if(nW > winWidth) {
  537. w = winWidth;
  538. nH = h = Math.ceil(w / ratio);
  539. if( h > winHeight){
  540. nH = h = winHeight;
  541. w = Math.ceil(h * ratio);
  542. }
  543. }
  544. if(nH > winHeight) {
  545. h = winHeight;
  546. w = Math.ceil(h * ratio);
  547. if( w > winWidth){
  548. w = winWidth;
  549. h = Math.ceil(w / ratio);
  550. }
  551. }
  552. }
  553. imgs.push({
  554. url: url,
  555. imgHeight : h,
  556. imgWidth : w
  557. });
  558. });
  559. localStorage["photoGalleryImgs"] = JSON.stringify(imgs); //因为此字符串可能是base64字符,appgo无法传
  560. localStorage["photoGalleryActiveIndex"] = activeIndex;
  561. $("#J_pg").remove();
  562. $("<iframe></iframe").appendTo("body")
  563. .attr("id", "J_pg")
  564. .attr("src", "../js/jquery-photo-gallery/gallery.html")
  565. .css({
  566. position : "absolute",
  567. left : (document.body.clientWidth - winWidth) /2,
  568. top : (document.body.clientHeight - winHeight) /2,
  569. width : winWidth,
  570. height : winHeight,
  571. background: 'rgba(177, 178, 179, 0.6)',
  572. border: '1px solid #6D6D6D',
  573. 'border-radius': '4px'
  574. });
  575. },
  576. //做初始化
  577. initGallery : function(){
  578. var activeIndex = localStorage["photoGalleryActiveIndex"],
  579. imgs = JSON.parse(localStorage["photoGalleryImgs"]);
  580. localStorage.removeItem("photoGalleryActiveIndex");
  581. localStorage.removeItem("photoGalleryImgs");
  582. $(".gallery").photoGallery({
  583. imgs : imgs,
  584. activeIndex:activeIndex
  585. });
  586. $(".closeWin").click(function(){
  587. var _parent = window.parent || window.top,
  588. _jg = _parent.document.getElementById("J_pg");
  589. $(_jg).remove();
  590. });
  591. }
  592. });
  593. })(jQuery);