移动web HTML5使用photoswipe模仿微信朋友圈图片放大浏览
原文网址:http://www.cnblogs.com/theroad/p/5397229.html
使用js框架是PhotoSwipe。
PhotoSwipe是一个图片放大插件,兼容pc和移动端,经历过多个版本的迭代且一直在不断更新,踩过的坑不知凡几,在移动端有着巨大的优势。
1、可控制多种风格如:
标题、分享、全屏按钮,点击事件、是否加入字幕,背景透明等。
2、可支持移动端触摸手势兼容pc端
所有的基本手势支持:滑动下一个或上一个,拖动平移、缩放、放大或关闭,点击切换控件,双击放大或缩放。
3、分享
默认的UI有一个按钮,分享链接。默认的链接是facebook,推特和Pinterest,但你可以通过API设置分享类型。
4、用户界面
用户界面是完全从核心脚本分离。完全可以自定义界面。默认photoswipe UI是响应式的,桌面、平板电脑和移动设备完全可以使用。
5、更多功能等你发现。
官网:http://photoswipe.com/
github:https://github.com/dimsemenov/photoswipe
如何使用?
1、在官网下载PhotoSwipe,在页面中引入
<link rel="stylesheet prefetch" href="css/photoswipe.css">
<link rel="stylesheet prefetch" href="css/default-skin/default-skin.css">
<script src="js/photoswipe.js"></script>
<script src="js/photoswipe-ui-default.min.js"></script>
注:prefetch规定应该对目标文档进行缓存。
2、页面中必须加入以下代码结构(此结构是插件图片浏览必须代码,作者并没有集成到js中,所以使用者必须手动加入自己的网页中):
<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true"><!-- Background of PhotoSwipe. It's a separate element as animating opacity is faster than rgba(). --><div class="pswp__bg"></div><!-- Slides wrapper with overflow:hidden. --><div class="pswp__scroll-wrap"><!-- Container that holds slides. PhotoSwipe keeps only 3 of them in the DOM to save memory.Don't modify these 3 pswp__item elements, data is added later on. --><div class="pswp__container"><div class="pswp__item"></div><div class="pswp__item"></div><div class="pswp__item"></div></div><!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. --><div class="pswp__ui pswp__ui--hidden"><div class="pswp__top-bar"><!-- Controls are self-explanatory. Order can be changed. --><div class="pswp__counter"></div><button class="pswp__button pswp__button--close" title="Close (Esc)"></button><button class="pswp__button pswp__button--share" title="Share"></button><button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button><button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button><!-- element will get class pswp__preloader--active when preloader is running --><div class="pswp__preloader"><div class="pswp__preloader__icn"><div class="pswp__preloader__cut"><div class="pswp__preloader__donut"></div></div></div></div></div><div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap"><div class="pswp__share-tooltip"></div> </div><button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)"></button><button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)"></button><div class="pswp__caption"><div class="pswp__caption__center"></div></div></div></div></div>
3、需要浏览的图片加入photoswipe结构代码,这里需要注意的是
data-pswp-uid在每个相册中必须是唯一的,data-size是指定放大时图片显示的宽和高,若指定的宽高与图片不符会导致显示的图片变形;目前还没找到去掉
data-size的办法,但然有时间可以找下替代办法。
<!--data-pswp-uid在每个相册中必须是唯一的,data-size指定放大时图片显示的宽和高-->
<div class="my-gallery" data-pswp-uid="1"><figure><a href="img/m3.jpg" data-size="670x712"><img src="img/th1.jpg"></a></figure>
</div>
4、加入js代码,此代码作者也没有集成到photoswipe框架中,需要自己手动加入网页里
<script type="text/javascript">var initPhotoSwipeFromDOM = function(gallerySelector) {// 解析来自DOM元素幻灯片数据(URL,标题,大小...)// (children of gallerySelector)var parseThumbnailElements = function(el) {var thumbElements = el.childNodes,numNodes = thumbElements.length,items = [],figureEl,linkEl,size,item;for(var i = 0; i < numNodes; i++) {figureEl = thumbElements[i]; // <figure> element// 仅包括元素节点if(figureEl.nodeType !== 1) {continue;} 25 linkEl = figureEl.children[0]; // <a> elementsize = 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> contentitem.title = figureEl.children[1].innerHTML; }if(linkEl.children.length > 0) {// <img> 缩略图节点, 检索缩略图网址item.msrc = linkEl.children[0].getAttribute('src');} item.el = figureEl; // 保存链接元素 for getThumbBoundsFnitems.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;// find root element of slidevar 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- attributevar clickedGallery = clickedListItem.parentNode,childNodes = clickedListItem.parentNode.childNodes,numChildNodes = childNodes.length,nodeIndex = 0,index;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 foundopenPhotoSwipe( 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);// 这里可以定义参数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)galleryUID: galleryElement.getAttribute('data-pswp-uid'),getThumbBoundsFn: function(index) {// See Options -> getThumbBoundsFn section of documentation for more infovar thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnailpageYScroll = window.pageYOffset || document.documentElement.scrollTop,rect = thumbnail.getBoundingClientRect(); return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};}};// PhotoSwipe opened from URLif(fromURL) {if(options.galleryPIDs) {// parse real index when custom PIDs are used for(var j = 0; j < items.length; j++) {if(items[j].pid == index) {options.index = j;break;}}} else {// in URL indexes start from 1options.index = parseInt(index, 10) - 1;}} else {options.index = parseInt(index, 10);}// exit if index not foundif( isNaN(options.index) ) {return;}if(disableAnimation) {options.showAnimationDuration = 0;}// Pass data to PhotoSwipe and initialize itgallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);gallery.init();};// loop through all gallery elements and bind eventsvar galleryElements = document.querySelectorAll( gallerySelector );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=1var hashData = photoswipeParseHash();if(hashData.pid && hashData.gid) {openPhotoSwipe( hashData.pid , galleryElements[ hashData.gid - 1 ], true, true );}};// execute above functioninitPhotoSwipeFromDOM('.my-gallery');
</script>
移动web HTML5使用photoswipe模仿微信朋友圈图片放大浏览相关推荐
- 类微信朋友圈图片放大效果,点击图片放大动画
要用到的 就是坐标转换方法 - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view; 1:我这里演示的是 tableviewHeader上子v ...
- 安卓开发仿微信图片拖拽_使用Android 模仿微信朋友圈图片拖拽返回
1概述 目前的app的动画效果是越来越炫了,很多主流app的图片预览返回都有类似功能,比较常见的是ios自带相册,微信朋友圈等等.自己项目中也有类似功能,最近整理了一下这个功能的代码,做个笔记记录,有 ...
- android模仿微信朋友圈图片预览转场缩放动画
先上效果图 其实实现起来非常简单,比如现在有两个Activity:GridActivity跳转DetailActivity,首先在GridActivity跳转DetailActivity的代码上加入A ...
- Android仿微信朋友圈图片展示效果,图片查看器
现在越来越多的APP都会有图片展示,这里是模仿微信朋友圈图片展示效果,图片查看器. 主要分为4部分: 1.透明Activity 2.计算gridView下iamgeView Item所在位置 3.一张 ...
- 模仿微信朋友圈 图片浏览 js javascript 支持图片预览,滑动切换,双指缩放,图片缓存
模仿微信朋友圈 图片浏览 js javascript 支持图片预览,滑动切换,双指缩放,图片缓存 2017年08月10日 12:11:38 阅读数:2311 previewImage-mobile 仿 ...
- html5 自动分享到朋友圈,html5手机端分享微信朋友圈代码
特效描述:html5手机端 分享微信朋友圈.html5手机端分享微信朋友圈代码 代码结构 1. HTML代码 发送给朋友 分享到朋友圈 button{width:100%;text-ali ...
- Android控件之 证件照控件 包含模仿微信朋友圈照片浏览
本次带来的是一个证件照控件 功能:可以自定义证件照存储路径.压缩大小.水印文字.水印颜色.水印大小 certificateview 使用说明: 将其添加到存储库末尾的根的build.gradle中: ...
- canva画图 图片居中裁剪_css实现不定宽高的图片img居中裁剪_类似微信朋友圈图片效果...
需求如下: 前端需要显示矩形的缩略图,接口返回的图片尺寸大小不一,宽高不相等,需要前端来处理并显示成正方形,类似微信朋友圈图片的效果,等比例正方形显示在列表中,让图片根据宽高来自适应显示在页面上.那么 ...
- Android仿微信朋友圈图片上传选择器布局
标题有点绕口,直接上一个效果图,如果符合你的需求的请在往下看,避免浪费你的时间 当当当当,标红的区域就是今天我们要干的活了 ,搞起来! 思路: 对android有点了解的人都知道在列表显示中我们可以使 ...
最新文章
- 完全背包:以重量分阶段
- oracle删除分区空间,Oracle 11g维护分区(三)——Dropping Partitions
- 在SqlServer存储过程中使用Cursor(游标)操作记录
- python中的字典和集合_Python 字典和集合
- mysql5.1数据库乱码_MySql5.1以上版本中文乱码的解决方法
- vs编译时出现大量ws2def.h的错误的解决方法
- 一个很好用的桌面取色器和一个在线取色器
- 树莓派修改root密码
- LocalDateTime日期格式之间的转换
- free、detele与野指针
- 用JSDoc生成js文档
- 微信小程序实现语音合成功能
- 日本杂货连锁Loft海外首家直营店上海开业;豪森药业创新药首次“出海” | 美通企业日报...
- Python学习总结(1)——Python知识清单(基础知识数据科学)
- 神州信息与瀚华金控签署战略协议 共推数字普惠金融
- 采用FPGA开发高清相机sensorISP芯片要点分析
- Python-OpenCV 读取和保存视频和解决保存失败的原因分析
- 第14节 单臂路由工作原理及简单实验
- 键盘没有小键盘怎么办?怎么打开屏幕软键盘?
- python能调用身份证读卡器吗_用Python在Linux下调用新中新DKQ-A16D读卡器,读二代证数据...