一个类似于手机img展示的js插件

photoswipe的官网 http://photoswipe.com/

首先引入插件里的必须文件

<link rel="stylesheet" type="text/css" href="../public/css/default-skin/default-skin.css"/>
<link rel="stylesheet" type="text/css" href="../public/css/photoswipe.css"/>
<script type="text/javascript" src="../public/js/photoswipe.min.js"></script>
<script type="text/javascript" src="../public/js/photoswipe-ui-default.min.js"></script>

页面布局需要固定格式

<!--data-pswp-uid在每个相册中必须是唯一的,data-size指定放大时图片显示的宽和高-->
<div class="my-gallery clearfix" data-pswp-uid="1"><figure ng-repeat="pic in items.imageDetails" ><div class="aixin_pic_con"><a href="{{pic.url}}" data-size="670x712"><img src="img/diary_list01.fw.png" ng-src="{{pic.url}}"></a></div><figcaption style="display:none;" ng-bind="items.crowdTitle">在这里可增加图片描述</figcaption></figure>
</div>

为了在多个页面实现插件的调用使用 便把所有的代码写入一条js里面引入 代码

固定的html代码每个想要调用的页面都需要引入

document.writeln("<!-- Root element of PhotoSwipe. Must have class pswp. -->");
document.writeln("<div class=\"pswp\" tabindex=\"-1\" role=\"dialog\" aria-hidden=\"true\">");
document.writeln("    <!-- Background of PhotoSwipe.");
document.writeln("         It\'s a separate element as animating opacity is faster than rgba(). -->");
document.writeln("    <div class=\"pswp__bg\"><\/div>");
document.writeln("    <!-- Slides wrapper with overflow:hidden. -->");
document.writeln("    <div class=\"pswp__scroll-wrap\">");
document.writeln("        <!-- Container that holds slides.");
document.writeln("            PhotoSwipe keeps only 3 of them in the DOM to save memory.");
document.writeln("            Don\'t modify these 3 pswp__item elements, data is added later on. -->");
document.writeln("        <div class=\"pswp__container\">");
document.writeln("            <div class=\"pswp__item\"><\/div>");
document.writeln("            <div class=\"pswp__item\"><\/div>");
document.writeln("            <div class=\"pswp__item\"><\/div>");
document.writeln("        <\/div>");
document.writeln("        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->");
document.writeln("        <div class=\"pswp__ui pswp__ui--hidden\">");
document.writeln("            <div class=\"pswp__top-bar\">");
document.writeln("                <!--  Controls are self-explanatory. Order can be changed. -->");
document.writeln("                <div class=\"pswp__counter\"><\/div>");
document.writeln("                <button class=\"pswp__button pswp__button--close\" title=\"Close (Esc)\"><\/button>");
//document.writeln("                <button class=\"pswp__button pswp__button--share\" title=\"Share\"><\/button>");
document.writeln("                <button class=\"pswp__button pswp__button--fs\" title=\"Toggle fullscreen\"><\/button>");
document.writeln("                <button class=\"pswp__button pswp__button--zoom\" title=\"Zoom in\/out\"><\/button>");
document.writeln("                <!-- Preloader demo http:\/\/codepen.io\/dimsemenov\/pen\/yyBWoR -->");
document.writeln("                <!-- element will get class pswp__preloader--active when preloader is running -->");
document.writeln("                <div class=\"pswp__preloader\">");
document.writeln("                    <div class=\"pswp__preloader__icn\">");
document.writeln("                        <div class=\"pswp__preloader__cut\">");
document.writeln("                            <div class=\"pswp__preloader__donut\"><\/div>");
document.writeln("                        <\/div>");
document.writeln("                    <\/div>");
document.writeln("                <\/div>");
document.writeln("            <\/div>");
document.writeln("            <div class=\"pswp__share-modal pswp__share-modal--hidden pswp__single-tap\">");
document.writeln("                <div class=\"pswp__share-tooltip\"><\/div>");
document.writeln("            <\/div>");
document.writeln("            <button class=\"pswp__button pswp__button--arrow--left\" title=\"Previous (arrow left)\">");
document.writeln("            <\/button>");
document.writeln("            <button class=\"pswp__button pswp__button--arrow--right\" title=\"Next (arrow right)\">");
document.writeln("            <\/button>");
document.writeln("            <div class=\"pswp__caption\">");
document.writeln("                <div class=\"pswp__caption__center\"><\/div>");
document.writeln("            <\/div>");
document.writeln("        <\/div>");
document.writeln("    <\/div>");
document.writeln("<\/div>");

固定的js调用方法,每个页面也需要引入

(function() {var initPhotoSwipeFromDOM = function(gallerySelector) {
//          console.log(gallerySelector)// 解析来自DOM元素幻灯片数据(URL,标题,大小...)// (children of gallerySelector)var parseThumbnailElements = function(el) {var thumbElements = el.childNodes,numNodes = thumbElements.length,items = [],figureEl,linkEl,size,item,divEl;//              console.log(el)for(var i = 0; i < numNodes; i++) { figureEl = thumbElements[i]; // <figure> element // 仅包括元素节点 if(figureEl.nodeType !== 1) { continue; } divEl = figureEl.children[0]; linkEl = divEl.children[0]; // <a> element size = linkEl.getAttribute('data-size').split('x'); // 创建幻灯片对象 item = { src: linkEl.getAttribute('href'), w: parseInt(size[0], 10), h: parseInt(size[1], 10) }; if(figureEl.children.length > 1) { // <figcaption> content item.title = figureEl.children[1].innerHTML;  } if(linkEl.children.length > 0) { // <img> 缩略图节点, 检索缩略图网址 item.msrc = linkEl.children[0].getAttribute('src'); } item.el = figureEl; // 保存链接元素 for getThumbBoundsFn items.push(item); } return items; }; // 查找最近的父节点 var closest = function closest(el, fn) { return el && ( fn(el) ? el : closest(el.parentNode, fn) ); }; // 当用户点击缩略图触发 var onThumbnailsClick = function(e) { e = e || window.event; e.preventDefault ? e.preventDefault() : e.returnValue = false; var eTarget = e.target || e.srcElement; // console.log(eTarget) // find root element of slide 查找幻灯片的根元素 var clickedListItem = closest(eTarget, function(el) { return (el.tagName && el.tagName.toUpperCase() === 'FIGURE'); }); if(!clickedListItem) { return; } // find index of clicked item by looping through all child nodes 通过遍历所有子节点,找到单击项的索引 // alternatively, you may define index via data- attribute 或者,您可以通过data - attribute定义索引 var clickedGallery = clickedListItem.parentNode, childNodes = clickedListItem.parentNode.childNodes, numChildNodes = childNodes.length, nodeIndex = 0, index; // console.log(clickedGallery) for (var i = 0; i < numChildNodes; i++) { if(childNodes[i].nodeType !== 1) { continue;  } if(childNodes[i] === clickedListItem) { index = nodeIndex; break; } nodeIndex++; } if(index >= 0) { // open PhotoSwipe if valid index found openPhotoSwipe( index, clickedGallery ); } return false; }; // parse picture index and gallery index from URL (#&pid=1&gid=2) var photoswipeParseHash = function() { var hash = window.location.hash.substring(1), params = {}; if(hash.length < 5) { return params; } var vars = hash.split('&'); for (var i = 0; i < vars.length; i++) { if(!vars[i]) { continue; } var pair = vars[i].split('=');  if(pair.length < 2) { continue; } params[pair[0]] = pair[1]; } if(params.gid) { params.gid = parseInt(params.gid, 10); } return params; }; var openPhotoSwipe = function(index, galleryElement, disableAnimation, fromURL) { var pswpElement = document.querySelectorAll('.pswp')[0], gallery, options, items; items = parseThumbnailElements(galleryElement); // console.log(galleryElement) // 这里可以定义参数 options = { barsSize: { top: 100, bottom: 100 }, fullscreenEl : false, // shareButtons: [ // {id:'wechat', label:'分享微信', url:'#'}, // {id:'weibo', label:'新浪微博', url:'#'}, // {id:'download', label:'保存图片', url:'{{raw_image_url}}', download:true} // ], // define gallery index (for URL) 定义图库索引(用于URL) galleryUID: galleryElement.getAttribute('data-pswp-uid'), getThumbBoundsFn: function(index) { // See Options -> getThumbBoundsFn section of documentation for more info var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail pageYScroll = window.pageYOffset || document.documentElement.scrollTop, rect = thumbnail.getBoundingClientRect();  return {x:rect.left, y:rect.top + pageYScroll, w:rect.width}; } }; // PhotoSwipe opened from URL PhotoSwipe从URL打开 if(fromURL) { if(options.galleryPIDs) { // parse real index when custom PIDs are used 使用定制pid时解析真实索引 for(var j = 0; j < items.length; j++) { if(items[j].pid == index) { options.index = j; break; } } } else { // in URL indexes start from 1 在URL索引中,从1开始 options.index = parseInt(index, 10) - 1; } } else { options.index = parseInt(index, 10); } // exit if index not found if( isNaN(options.index) ) { return; } if(disableAnimation) { options.showAnimationDuration = 0; } // Pass data to PhotoSwipe and initialize it gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options); gallery.init(); }; // loop through all gallery elements and bind events var galleryElements = document.querySelectorAll( gallerySelector ); // console.log(galleryElements.length) for(var i = 0, l = galleryElements.length; i < l; i++) { galleryElements[i].setAttribute('data-pswp-uid', i+1);  galleryElements[i].onclick = onThumbnailsClick; } // Parse URL and open gallery if it contains #&pid=3&gid=1 var hashData = photoswipeParseHash(); if(hashData.pid && hashData.gid) { openPhotoSwipe( hashData.pid , galleryElements[ hashData.gid - 1 ], true, true ); } }; //延迟后执行 避免开始获取不到 setTimeout(function(){ // execute above function initPhotoSwipeFromDOM('.my-gallery'); },600) })();

**需要注意的是每个结构必须一致,方法也要在页面加载以后调用不然点击以后可能会遇到页面跳转道图片地址的情况。

**可根据自己情况进行改动

photoswipe.min.js 使用相关推荐

  1. jquery-1.11.3.js与jquery-1.11.3.min.js区别

    2019独角兽企业重金招聘Python工程师标准>>> 唯一的区别就是在: Download the compressed, production jQuery 1.11.3,Pro ...

  2. jquery.dataTables.min.js: Uncaught TypeError: Cannot read property 'style' of undefined

    Jquery DataTable使用报这个错: jquery.dataTables.min.js: Uncaught TypeError: Cannot read property 'style' o ...

  3. js post中服务器500错误信息,node.js - Node Express Post 500(内部服务器错误)jquery-3.4.1.min.js - 堆栈内存溢出...

    我一直在研究 node js/express js 应用程序有一段时间了. 我有从 jquery $ajax 调用中获取和发布的工作. 出于某种原因,我有一个帖子"loginUser&quo ...

  4. jquery-1.4.4.min.js无法解析json中result.data问题

    如下图该版本无法解析json数据 解决方法引用jquer.min.js,并且将$修改为jQuery 转载于:https://www.cnblogs.com/xiaz/p/10228994.html

  5. 多语言切换jquery.i18n.min.js

    项目需求,切换多语言,采用 jQuery.i18n.min.js文件内容如下 ------------------------------------------------- /*!  * jque ...

  6. html5shiv.js和respond.min.js的作用

    html5shiv:解决ie9以下浏览器对html5新增标签的不识别,并导致CSS不起作用的问题. respond.min:让不支持css3 Media Query的浏览器包括IE6-IE8等其他浏览 ...

  7. JavaScript--jquery.min.js文件

    1 /*! jQuery v1.12.3 | (c) jQuery Foundation | jquery.org/license*/ 2 !function(a,b){"object&qu ...

  8. jquery.placeholder.min.js让你的IE浏览器支持placeholder

    2019独角兽企业重金招聘Python工程师标准>>> jquery.placeholder.min.js让你的IE浏览器支持placeholder http://www.ijque ...

  9. getplotlyoffline(‘http://cdn.plot.ly/plotly-latest.min.js‘)无法下载如何解决

    matlab安装plotly的离线脚本时,无法下载,如何解决 简单记录一下,问题出在这句命令,getplotlyoffline('http://cdn.plot.ly/plotly-latest.mi ...

最新文章

  1. MongoDB之conf配置文件详解
  2. centOs7上用yum安装软件报错
  3. 探讨如何成为技术团队管理者
  4. 使用XmlTextReader 读取XML
  5. python 最简单的实现适配器设计模式
  6. 原F1000Prime推荐:ACE2泛癌分析图谱(TCGA数据库挖掘)
  7. AngularJS中ng-options简单用法及预选项失败的原因
  8. js权威指南---学习笔记01
  9. 你还在用HTTP吗?HTTP/3都来了。。。
  10. Python小游戏--贪吃蛇
  11. Python 字符串 循环
  12. Android Studio高级使用技巧
  13. 年龄识别数据集IMDB-WIKI
  14. Unity Live2D SDK的使用方法(二)——播放动画
  15. 《科学管理原理》读后感
  16. 重庆生小孩可以领那些钱
  17. C++学习心得和进阶路线总结
  18. Sherlock之Instructions指令介绍(Sherlock Version: 7.2.5.1 64-bit)
  19. 计算机收藏夹无法添加,Win7系统收藏夹不能用怎么办?
  20. C语言 日期转时间戳

热门文章

  1. 河北工业大学计算机学院胡天宇,机械工程学院
  2. 佛吉尼亚大学计算机世界排名,弗吉尼亚大学计算机科学专业介绍_计算机科学专业排名及就业方向和前景-小站留学...
  3. 【内网安全】——Windows权限维持
  4. 音视频 FFmpeg
  5. HNCU1101:马的移动(BFS)
  6. 《穷查理宝典》阅读总结
  7. 【dSPACE】从0开启dSPACE之路(1)dSPACE功能及其组件介绍
  8. 不合格机器人工程讲师如何坦然面对失败
  9. du命令--查看文件、文件夹、磁盘大小
  10. 有什么让人笑得停不下来的段子?