公司的项目http://www.umfun.com/,有个说说的页面(和腾讯QQ空间说说一样),里面有个发表图片功能,上传完图片,需要点击展开的效果。

当时手里面事情比较多(公司就我一个前端),忙不过来,就用插件来实现了,试了fancyBox、lightbox等jQuery插件。插件都满足不了项目各种奇怪的需求,但是时间有限,只能先凑合了。、

项目上线后,最近时间比较充足,我就想把写个插件封装一下。毕竟人家的插件比较臃肿,修改起来麻烦,同时免得以后又是各种修改,还是根据公司的项目需求写比较好。。。

效果预览,如果下图所示:

点击图片后,展示:

2014-4-18更新

更新内容:1、兼容性问题;

      2、点击大图切换到下一张图

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>画廊</title>
  6. <style>
  7. /*
  8. * jquery gallery CSS
  9. * ZhaoHuanLei - 20140418
  10. */
  11. .gallery-overlay {width:100%;height:100%;position:fixed;_top:absolute;top:0;left:0;z-index:99;filter:progid:DXImageTransform.Microsoft.gradient(enabled='true',startColorstr='#B2000000', endColorstr='#B2000000');background-color:rgba(0,0,0,.7);}
  12. :root .gallery-overlay {filter:none;}
  13. .gallery-close,
  14. .gallery-prev,
  15. .gallery-next {position:absolute;color:#fff;text-decoration:none;}
  16. .gallery-prev,
  17. .gallery-next {top:40%;font:bold 80px/80px simsun;}
  18. .gallery-prev {left:50px;}
  19. .gallery-next {right:50px;}
  20. .gallery-close {width:82px;height:77px;top:0;right:0;background:url(http://images.cnitblog.com/i/333689/201404/181538254946336.png) no-repeat;text-indent:-9999em;}
  21. .gallery-photo {width:100%;height:100%;position:absolute;top:50px;vertical-align:middle;text-align:center;}
  22. .gallery-photo span {height:100%;display:inline-block;vertical-align:middle;}
  23. .gallery-photo img {max-width:100%;max-height:100%;vertical-align:middle;cursor:pointer;}
  24. .gallery-thumb {width:100%;height:56px;position:absolute;bottom:50px;text-align:center;font-size:0;}
  25. .gallery-thumb a {width:50px;height:50px;overflow:hidden;margin:0 2px;display:inline-block;*zoom:1;border:3px solid transparent;opacity:.6;filter:alpha(opacity:60);}
  26. .gallery-thumb img {max-width:100px;max-height:100px;min-width:50px;min-height:50px;border:none;}
  27. .gallery-thumb .selected {border-color:#f60;opacity:1;filter:alpha(opacity:100);}
  28. </style>
  29. </head>
  30. <body style="height:2000px;">
  31.  
  32. <h1>画廊</h1>
  33. <p class="img">
  34. <a href="https://images0.cnblogs.com/i/333689/201403/181012241467455.jpg"><img src="https://images0.cnblogs.com/i/333689/201403/181012064744754.jpg" alt=""></a>
  35. <a href="https://images0.cnblogs.com/i/333689/201403/181012428021756.jpg"><img src="https://images0.cnblogs.com/i/333689/201403/181012349904375.jpg" alt=""></a>
  36. <a href="https://images0.cnblogs.com/i/333689/201403/181012573656772.jpg"><img src="https://images0.cnblogs.com/i/333689/201403/181012512096320.jpg" alt=""></a>
  37. <a href="https://images0.cnblogs.com/i/333689/201403/181013163811731.jpg"><img src="https://images0.cnblogs.com/i/333689/201403/181013035524683.jpg" alt=""></a>
  38. <a href="https://images0.cnblogs.com/i/333689/201403/181013442711411.jpg"><img src="https://images0.cnblogs.com/i/333689/201403/181013354124216.jpg" alt=""></a>
  39. </p>
  40.  
  41. <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
  42. <script>
  43. /*
  44. * jquery gallery JS
  45. * ZhaoHuanLei - 20140418
  46. */
  47.  
  48. ;(function($) {
  49. $.fn.extend({
  50. gallery: function() {
  51. $(this).on("click", function() {
  52. var self = $(this),
  53. link = self.parent().find("a"),
  54. bd = $("body");
  55. html = "\
  56. <div class='gallery-overlay'>\
  57. <div class='gallery-photo'><span></span><img src='"+ self.attr("href") +"'></div>\
  58. <div class='gallery-thumb'></div>\
  59. <a class='gallery-prev' href='javascript:;' title='上一个'>&lt;</a>\
  60. <a class='gallery-next' href='javascript:;' title='下一个'>&gt;</a>\
  61. <a class='gallery-close' href='javascript:;' title='关闭'>&times;</a>\
  62. </div>\
  63. ";
  64. bd.css("overflow-y", "hidden").append(html);
  65. var overlay = $(".gallery-overlay"),
  66. photo = $(".gallery-photo"),
  67. photoImg = photo.find("img"),
  68. thumb = $(".gallery-thumb"),
  69. prev = $(".gallery-prev"),
  70. next = $(".gallery-next"),
  71. close = $(".gallery-close"),
  72. str = "";
  73.  
  74. //浏览器缩放时候,重置
  75. function toResize() {
  76. var height = $(window).height();
  77. overlay.height(height);
  78. photo.css({"height": height - 200});
  79. photoImg.css({"max-height": height - 200});//解决safari下bug
  80. }
  81. toResize();
  82. $(window).resize(function() {
  83. toResize();
  84. });
  85.  
  86. //生成缩略图列表
  87. link.each(function() {
  88. var href = $(this).attr("href"),
  89. src = $(this).find("img").attr("src"),
  90. act = "<a href='"+ href +"'><img src='"+ src +"'/></a>";
  91. str += act;
  92. });
  93. thumb.append(str);
  94.  
  95. //图片切换
  96. var thumbLink = thumb.find("a"),
  97. len = thumbLink.length - 1,
  98. index = link.index(this);
  99. function switchPhoto(index) {
  100. var _this = thumbLink.eq(index);
  101. _this.addClass("selected").siblings().removeClass("selected");
  102. photo.find("img").attr("src", _this.attr("href"));
  103. }
  104. switchPhoto(index);
  105.  
  106. thumb.on("click", "a", function() {
  107. index = thumbLink.index(this);
  108. switchPhoto(index);
  109. return false;
  110. });
  111.  
  112. //切换下一个
  113. function switchPrev() {
  114. index--;
  115. if (index < 0) {
  116. index = len;
  117. }
  118. switchPhoto(index);
  119. }
  120. //切换上一个
  121. function switchNext() {
  122. index++;
  123. if (index > len) {
  124. index = 0;
  125. }
  126. switchPhoto(index);
  127. }
  128.  
  129. prev.on("click", function() {
  130. switchPrev();
  131. });
  132. next.on("click", function() {
  133. switchNext();
  134. });
  135. photo.on("click", "img", function() {
  136. switchNext();
  137. });
  138.  
  139. //关闭层
  140. function closeOverlay() {
  141. overlay.remove();
  142. bd.css("overflow-y", "auto");
  143. }
  144. close.on("click", function() {
  145. closeOverlay();
  146. });
  147.  
  148. return false;
  149. });
  150. }
  151. });
  152. })(jQuery);
  153. </script>
  154. <script>
  155. $(function() {
  156. $('.img a').gallery();
  157. });
  158. </script>
  159. </body>
  160. </html>

20140317上传

JQuery插件代码:

  1. /*
  2. * jquery gallery JS
  3. * ZhaoHuanLei - 20140317
  4. */
  5.  
  6. ;(function($) {
  7. $.fn.extend({
  8. gallery: function() {
  9. $(this).on("click", function() {
  10. var self = $(this),
  11. link = self.parent().find("a"),
  12. bd = $("body");
  13. html = "\
  14. <div class='gallery-overlay'>\
  15. <div class='gallery-photo'><span></span><img src='"+ self.attr("href") +"'></div>\
  16. <div class='gallery-thumb'></div>\
  17. <a class='gallery-prev' href='javascript:;' title='上一个'>&lt;</a>\
  18. <a class='gallery-next' href='javascript:;' title='下一个'>&gt;</a>\
  19. <a class='gallery-close' href='javascript:;' title='关闭'>&times;</a>\
  20. </div>\
  21. ";
  22. bd.css("overflow-y", "hidden").append(html);
  23. var overlay = $(".gallery-overlay"),
  24. photo = $(".gallery-photo"),
  25. thumb = $(".gallery-thumb"),
  26. prev = $(".gallery-prev"),
  27. next = $(".gallery-next"),
  28. close = $(".gallery-close"),
  29. str = "";
  30.  
  31. //浏览器缩放时候,重置
  32. function toResize() {
  33. var height = $(window).height();
  34. overlay.height(height);
  35. photo.css({"height": height - });
  36. photo.find("img").css({"max-height": height - });//解决safari下bug
  37. }
  38. toResize();
  39. $(window).resize(function() {
  40. toResize();
  41. });
  42.  
  43. //生成缩略图列表
  44. link.each(function() {
  45. var href = $(this).attr("href"),
  46. src = $(this).find("img").attr("src"),
  47. act = "<a href='"+ href +"'><img src='"+ src +"'/></a>";
  48. str += act;
  49. });
  50. thumb.append(str);
  51.  
  52. //图片切换
  53. var thumbLink = thumb.find("a"),
  54. len = thumbLink.length - ,
  55. index = link.index(this);
  56. function imgSwitch(index) {
  57. var _this = thumbLink.eq(index);
  58. _this.addClass("selected").siblings().removeClass("selected");
  59. photo.find("img").attr("src", _this.attr("href"));
  60. }
  61. imgSwitch(index);
  62.  
  63. thumb.on("click", "a", function() {
  64. index = thumbLink.index(this);
  65. imgSwitch(index);
  66. return false;
  67. });
  68. prev.on("click", function() {
  69. index--;
  70. if (index < ) {
  71. index = len;
  72. }
  73. imgSwitch(index);
  74. });
  75. next.on("click", function() {
  76. index++;
  77. if (index > len) {
  78. index = ;
  79. }
  80. imgSwitch(index);
  81. });
  82.  
  83. //关闭层
  84. function closeOverlay() {
  85. overlay.remove();
  86. bd.css("overflow-y", "auto");
  87. }
  88. close.on("click", function() {
  89. closeOverlay();
  90. });
  91.  
  92. return false;
  93. });
  94. }
  95. });
  96. })(jQuery);

HTML:

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>画廊</title>
  6. <link rel="stylesheet" href="style/jquery.gallery.css">
  7. </head>
  8. <body style="height:2000px;">
  9.  
  10. <h1>画廊</h1>
  11. <p class="img">
  12. <a href="https://images0.cnblogs.com/i/333689/201403/181012241467455.jpg"><img src="https://images0.cnblogs.com/i/333689/201403/181012064744754.jpg" alt=""></a>
  13. <a href="https://images0.cnblogs.com/i/333689/201403/181012428021756.jpg"><img src="https://images0.cnblogs.com/i/333689/201403/181012349904375.jpg" alt=""></a>
  14. <a href="https://images0.cnblogs.com/i/333689/201403/181012573656772.jpg"><img src="https://images0.cnblogs.com/i/333689/201403/181012512096320.jpg" alt=""></a>
  15. <a href="https://images0.cnblogs.com/i/333689/201403/181013163811731.jpg"><img src="https://images0.cnblogs.com/i/333689/201403/181013035524683.jpg" alt=""></a>
  16. <a href="https://images0.cnblogs.com/i/333689/201403/181013442711411.jpg"><img src="https://images0.cnblogs.com/i/333689/201403/181013354124216.jpg" alt=""></a>
  17. </p>
  18.  
  19. <script src="script/jquery-1.11.0.min.js"></script>
  20. <script src="script/jquery.gallery.js"></script>
  21. <script>
  22. $(function() {
  23. $('.img a').gallery();
  24. });
  25. </script>
  26. </body>
  27. </html>

CSS:

  1. /*
  2. * jquery gallery CSS
  3. * ZhaoHuanLei - 20140317
  4. */
  5. .gallery-overlay {width:%;height:%;position:fixed;_top:absolute;top:;left:;z-index:;filter:progid:DXImageTransform.Microsoft.gradient(enabled='true',startColorstr='#CC000000', endColorstr='#CC000000');background-color:rgba(,,,.);}
  6. :root .gallery-overlay {filter:none;}
  7. .gallery-close,
  8. .gallery-prev,
  9. .gallery-next {position:absolute;color:#fff;text-decoration:none;}
  10. .gallery-prev,
  11. .gallery-next {top:%;font:bold 80px/80px simsun;}
  12. .gallery-prev {left:50px;}
  13. .gallery-next {right:50px;}
  14. .gallery-close {width:82px;height:77px;top:;right:;background:url(../images/gallery-close.png) no-repeat;text-indent:-9999em;}
  15. .gallery-photo {width:%;height:%;position:absolute;top:50px;vertical-align:middle;text-align:center;}
  16. .gallery-photo span {height:%;display:inline-block;vertical-align:middle;}
  17. .gallery-photo img {max-width:%;max-height:%;vertical-align:middle;}
  18. .gallery-thumb {width:%;height:56px;position:absolute;bottom:50px;text-align:center;font-size:;}
  19. .gallery-thumb a {width:50px;height:50px;overflow:hidden;margin: 2px;display:inline-block;*zoom:;border:3px solid transparent;opacity:.;filter:alpha(opacity:);}
  20. .gallery-thumb img {max-width:100px;max-height:100px;min-width:50px;min-height:50px;border:none;}
  21. .gallery-thumb .selected {border-color:#f60;opacity:;filter:alpha(opacity:);}

PS:

1、基本功能实现了,没搞什么选项设置,有时间再扩展,现在已经满足需求了。。

2、公司的项目只需要兼容IE7+兼容就可以。so。。。IE6就没考虑了。。

jQuery插件实现图片展开效果,jquery.gallery。仿腾讯QQ空间说说图片展示效果。的更多相关文章

  1. 不定义JQuery插件,不要说会JQuery[转载]

    http://www.cnblogs.com/xcj26/p/3345556.html 不定义JQuery插件,不要说会JQuery 一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页 ...

  2. 不定义JQuery插件,不要说会JQuery 分类: JavaScript 2014-11-24 14:18 155人阅读 评论(0) 收藏

    一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#"),$("."),写了几年就对别人说非常熟悉JQuery.我曾经也是这样的人 ...

  3. [转]不定义JQuery插件,不要说会JQuery

    一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写("#"),("."),写了几年就对别人说非常熟悉JQuery.我曾经也是这样的人,直 ...

  4. 不定义JQuery插件,不要说会JQuery

    转自:http://www.cnblogs.com/xcj26/p/3345556.html 一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#") ...

  5. 转:不会定义jQuery插件,不要说会jQuery

    一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#"),$("."),写了几年就对别人说非常熟悉JQuery.我曾经也是这样的人 ...

  6. 转载:不定义JQuery插件,不要说会JQuery

    转载:http://www.cnblogs.com/xcj26/p/3345556.html 一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#") ...

  7. (转)不定义JQuery插件,不要说会JQuery

    原文地址:http://www.cnblogs.com/xcj26/p/3345556.html 一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#" ...

  8. 使用jQuery插件时避免重复引入jquery.js文件

    当一个页面使用多个jQuery插件时,需要避免重复引入jquery.js文件,因为后面映入的jQuery.js文件中定义的jQuery对象会覆盖掉前面的jQuery对象,导致之前定义的jQuery插件 ...

  9. 使用PHP打造QQ空间神奇图片

    说明 你一定在qq空间遇到过这样的东西:打开一张图片,上面有你的QQ号和昵称,你觉得很神奇,是不是? 其实原理很简单,那张图片是动态生成的,上面显示的信息是根据你访问的Url获得的,然后用程序动态的画 ...

随机推荐

  1. Python中文字符串截取

    #-*- coding:utf8 -*- s = u'中文截取' s.decode('utf8')[0:3].encode('utf8') # 结果u'中文截取 延伸阅读: UTF-8中的汉字占用多少 ...

  2. VPS CentOS-6 下 LNMP HTTP服务器的搭建

    VPS CentOS-6 下 LNMP HTTP服务器的搭建 前言 恢复更新后的第一篇博文, 前段时间由于各种理由, 把博客更新给宕掉了, 个人独立博客的开发也搁浅了, 现在随着工作的逐步稳定, 决心 ...

  3. windows下重启mysql

    其中第二种方法对我这无效,以后再搞清楚! 一.MYSQL服务 我的电脑——(右键)管理——服务与应用程序——服务——MYSQL——开启(停止.重启动) 二.命令行方式 Windows 1.点击“开始” ...

  4. 喜迎2015年新年:坦克大战(Robocode)游戏编程比赛图文总结

    2015春节前,葡萄城的软件工程师以特有的方式来迎接新年——2015新年编程邀请赛. 邀请赛的初衷,是和大家一起,寻找编程最初的单纯的快乐.       在代码的世界里,添加动力,继续远航.      ...

  5. Firefox SVG getBBox方法返回'NS_ERROR_FAILURE'错误分析

    在SVG中,我们无法给Text元素设置Width和Height属性,因此无法直接获取Text元素的高和宽.如果想要给Text元素添加背景色,最简单的办法就是在Text元素的下面添加Rect,然后给Re ...

  6. bigautocomplete实现联想输入,自动补全

    bigautocomplete是一款Jquery插件.用它实现仿搜索引擎文本框自动补全插件功能很实用,使用也很简单,引入了插件之后写几行代码就可以实现,可以灵活设置. 先看效果图: 上图是通过ajax ...

  7. winform下重画ListBox

    Windows Forms是由Win32 API封装的开发组件,最初是为了替代mfc,但却没有体现与Model View Controller架构对应的特色,进而在.net framework 3.0 ...

  8. mysql高级排序&高级匹配查询示例

    在大多数应用场景下,我们使用mysql进行查询时只会用到'=', '>' , '<' , in, like 等常用的方法,看起来,大多数情况下,已经足以应付我们的小型应用了.不过,在一些特 ...

  9. beego中orm关联查询使用解析

    这两天在学习beego框架,之前学习的时候遗漏了很多东西,比如orm.缓存.应用监控.模板处理等,这里将通过实例记录下如何使用beego自带的orm进行关联查询操作. 首先说明下,beego的orm有 ...

  10. JVM中的Stack和Frame

    JVM执行Java程序时需要装载各种数据,比如类型信息(Class).类型实例(Instance).常量数据(Constant).本地变量等.不同的数据存放在不同的内存区中,这些数据内存区称作“运行时 ...