jquery.photo.gallery-2.js 16 KB

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