PhotoSwipe是一个开发用于手机和Touch设备,功能强大的图片/照片集展示控件。它采用HTML、CSS+JavaScript实现,没有依赖任何JavaScript框架。
可以设置淡入/淡出的效果,激活放大功能,缩放图片等。PhotoSwipe支持的设备包括:iPhone、iPad、Android、Blackberry和桌面浏览器。它还有一个可选基于jQuery实现的版本。
优点:
界面美观,简易的人性化操作
兼容移动设备,图像展示友好。
纯JS,不依赖其它JS库,轻量级!比FancyBox小太多太多了
官方网站:http://www.photoswipe.com/
源码示例:http://github.com/downloads/codecomputerlove/PhotoSwipe/code.photoswipe-3.0.5.zip
Github:https://github.com/codecomputerlove/PhotoSwipe
在线demo:http://www.photoswipe.com/latest/examples/04-jquery-mobile.html
如何使用
一、引入Css和Js文件:
<!-- photoswipe 之前先引用klass,如果需要提高加载速度,可以给 script 加上 defer 标记/属性--> <script type="text/javascript" src="klass.min.js"></script> <!-- 重要提示,如果不使用jQuery版本,在IE下面会出错,当然,使用jQuery版本,则需要引入jQuery--> <script type="text/javascript" src="code.photoswipe-3.0.5.min.js"></script>
二、然后页面结构可以是这样子的:
<!-- ul li 之类是用于显示缩略图的,也可以根据需要调整.<a>下面的<img> 元素,即为缩略图,如果不需要,则src设置为空即可 --> <ul id="Gallery"> <li><a href="images/full/01.jpg"><img src="images/thumb/01.jpg" alt="Image 01" /></a></li> <li><a href="images/full/02.jpg"><img src="images/thumb/02.jpg" alt="Image 02" /></a></li> <li><a href="images/full/03.jpg"><img src="images/thumb/03.jpg" alt="Image 03" /></a></li> <li><a href="images/full/04.jpg"><img src="images/thumb/04.jpg" alt="Image 04" /></a></li> <li><a href="images/full/05.jpg"><img src="images/thumb/05.jpg" alt="Image 05" /></a></li> <li><a href="images/full/06.jpg"><img src="images/thumb/06.jpg" alt="Image 06" /></a></li> </ul>
其实在这段html代码中除了页面结构外,真正有用的只有 id="Gallery"和<a href="图片路径"></a>(在后面会有说明),其他的class神马的只是起到美化最初的页面结构的作用(和你真正想要的效果的页面不同,也就是说,你只要按照上述页面的结构进行排版,你想要的页面效果是插件js自身完成的,是不需要你写效果布局的)。
页面需要的js和页面结构都有了,下面就是使用插件了。
三、你可以采用两种方式进行插件的声明:
1、是用浏览器默认的方式addEventListener()的方式进行
document.addEventListener("DOMContentLoaded", function () {
Code.photoSwipe('a', '#Gallery'); //此处就涉及到上述页面结构中的 id="Gallery"和<a href="..."></a>,其中id="Gallery"是容器,
//<a href="图片路径"></a>,此处href中一定是当前所指向的图片的路径
}, false);2、使用Jquery的方式:
$(document).ready(function(){
$("#Gallery a").photoSwipe();
});属性对照表
| 属性 | 描述 |
| allowUserZoom | 允许用户双击放大/移动方式查看图片. 默认值 = true |
| autoStartSlideshow | 当PhotoSwipe激活后,自动播放幻灯片. 默认值 = false |
| allowRotationOnUserZoom | 只有 iOS 支持 - 允许用户在缩放/平移模式下 用手势旋转图像. 默认值 = false |
| backButtonHideEnabled | 按返回键隐藏相册幻灯片. 主要是 Android 和 Blackberry使用. 支持 BB6, Android v2.1, iOS 4 以及更新版本. 默认值 = true |
| captionAndToolbarAutoHideDelay | 标题栏和工具栏自动隐藏的延迟时间. 默认值为 = 5000(毫秒). 如果设为 0 则不会自动隐藏(tap/单击切换显隐) |
| captionAndToolbarFlipPosition | 标题栏和工具栏切换位置(让 caption显示在底部而 toolbar显示在顶部). 默认值 = false |
| captionAndToolbarHide | 隐藏 标题栏和工具栏. 默认值 = false |
| captionAndToolbarOpacity | 标题栏和工具栏 的透明度(0-1). 默认值 = 0.8 |
| captionAndToolbarShowEmptyCaptions | 即使当前图片的标题是空,也显示标题栏. 默认值 = true |
| cacheMode | 缓存模式,Code.PhotoSwipe.Cache.Mode.normal (默认,正常) 或者 Code.PhotoSwipe.Cache.Mode.aggressive(激进,积极). 决定 PhotoSwipe 如何管理图片缓存 cache |
| Aggressive | 模式将会积极地地设置非 "当前,上一张,下一张"的图片为空的类型. 对于老版本iOS 浏览器下的大图片内存溢出将会很有用. 大多数情况下,normal模式就可以了 |
| doubleTapSpeed | 双击的最大间隔. 默认值 = 300(毫秒) |
| doubleTapZoomLevel | 当用户双击的时候,放大的倍数, 默认的 "zoom-in"(拉近) 级别. 默认值 = 2.5 |
| enableDrag | 允许拖动上一张/下一张图片到当前界面. 默认值 = true |
| enableKeyboard | 允许键盘操作(左右箭头切换,Esc退出,Enter自动播放,空格键 显/隐标题栏/退出). 默认 = true |
| enableMouseWheel | 允许鼠标滚轮操作. 默认 = true |
| fadeInSpeed | 淡入效果元素的速度(持续时间),毫秒. 默认 = 250 |
| fadeOutSpeed | 淡出效果元素的速度(持续时间),毫秒. 默认 = 250 |
| imageScaleMethod | 图片缩放方法(模式). 可选值: "fit", "fitNoUpscale" 和 "zoom". 模式"fit" 保证图像适应屏幕. "fitNoUpscale" 和 "fit"类似但是不会放大图片. "zoom"将图片全屏, 但有可能图片缩放不是等比例的. 默认 = "fit" |
| invertMouseWheel | 反转鼠标滚轮。默认情况下,鼠标向下滚动将切换到下一张,向上切换到上一张 . 默认 = false |
| jQueryMobile | 指示 PhotoSwipe 是否集成进了 jQuery Mobile 项目. 默认情况下, PhotoSwipe will try and work this out for you |
| jQueryMobileDialogHash | jQuery Mobile的window,dialog页面 所使用的hash标签。 默认值 = "&ui-state=dialog" |
| loop | 相册是否自动循环. 默认 = true |
| margin | 两张图之间的间隔,单位是像素. 默认 = 20 |
| maxUserZoom | 最大放大倍数. 默认 = 5.0 (设置为0将被忽略) |
| minUserZoom | 图像最小的缩小倍数. 默认 = 0.5 (设置为0将会忽略) |
| mouseWheelSpeed | 响应鼠标滚轮的灵敏度. 默认 = 500(毫秒) |
| nextPreviousSlideSpeed | 当点击上一张,下一张按钮后,延迟多少毫秒执行切换. 默认 = 0 (立即切换) |
| preventHide | 阻止用户关闭 PhotoSwipe. 同时也会隐藏 工具栏上的"close"关闭按钮. 在独享的页面使用 (示例是源码中的 examples/08-exclusive-mode.html). 默认 = false |
| preventSlideshow | 阻止自动播放模式. 同时也会隐藏工具栏里的播放按钮. 默认 = false |
| slideshowDelay | 自动播放模式下,多长时间播放下一张. Default = 3000(毫秒) |
| slideSpeed | 图片滑进视图的时间. 默认 = 250(毫秒) |
| swipeThreshold | 手指滑动多少像素才触发一个 swipe 手势事件. 默认 = 50 |
| swipeTimeThreshold | 定义触发swipe(滑动)手势的最大毫秒数,太慢了则不会触发滑动,只会拖动当前照片的位置. 默认 = 250 |
| slideTimingFunction | 滑动时的 Easing function . 默认 = "ease-out" |
| zIndex | 初始的zIndex值. 默认 = 1000 |
| enableUIWebViewRepositionTimeout | 检查设备的方向是否改变。默认 = false |
| uiWebViewResetPositionDelay | 定时检查设备的方向是否改变的时间 默认 = 500(毫秒) |
| preventDefaultTouchEvents | 阻止默认的touch事件,比如页面滚动。 默认 = true |
| target | 必须是一个合法的DOM元素(如DIV)。默认是window(全页面)。而如果是某个低级别的DOM,则在DOM内显示,可能非全屏 |
如果不需要展示第一个页面直接展示第二个页面,可以这样设置:
$(document).ready(function(){
// Set up PhotoSwipe, setting "preventHide: true"
var thumbEls = Code.photoSwipe('a', '#Gallery', { preventHide: true });
Code.PhotoSwipe.Current.show(0);
});当然这个插件还有很多其他的监听函数:
document.addEventListener('DOMContentLoaded',function(){
//onBeforeShow 在gallery将要展示之前调用该方法
Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onBeforeShow,function(e){
console.log("onBeforeShow");
});
// onshow 在gallery展示的时候调用
Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onShow,function(e){
console.log("onShow");
});
// onBeforeHide 在gallery隐藏之前
Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onBeforeHide, function(e){
console.log('onBeforeHide');
});
// onHide 在Gallery隐藏的时候
Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onHide, function(e){
console.log('onHide');
});
// onShowNext 在展示下一个的时候
Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onShowNext, function(e){
console.log('onShowNext');
});
// onShowPrevious 在展示上一个的时候
Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onShowPrevious, function(e){
console.log('onShowPrevious');
});
// onDisplayImage 在图片展示
Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onDisplayImage, function(e){
console.log('onDisplayImage');
});
// onResetPosition 当Gallery的大小和位置发生变化时或者设备的方向或者窗口大小改变时,出发该方法
Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onResetPosition, function(e){
console.log('onResetPosition');
});
// onSlideshowStart 当gallery开始滑动展示的时候(此方法可能是我理解有误,实验过程中一直没有触发过
的),原文是:When the gallery has started the slideshow
Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onSlideshowStart, function(e){
console.log('onSlideshowStart');
});
// onSlideshowStop 当Gallery活动结束的时候
Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onSlideshowStop, function(e){
console.log('onSlideshowStop');
});
// onBeforeCaptionAndToolbarShow 在顶部状态栏和底部的工具栏展示之前触发
Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onBeforeCaptionAndToolbarShow, function(e){
console.log('onBeforeCaptionAndToolbarShow');
});
// onBeforeCaptionAndToolbarHide 在顶部状态栏和底部的工具栏隐藏之前触发
Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onBeforeCaptionAndToolbarHide, function(e){
console.log('onBeforeCaptionAndToolbarHide');
});
// onViewportClick 在gallery中点击屏幕的时候触发,此时一般会触发onBeforeCaptionAndToolbarShow或者onBeforeCaptionAndToolbarHide 方法
Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onViewportClick, function(e){
console.log('onViewportClick');
});
},false);参考文章:
http://photoswipe.com/documentation/getting-started.html
http://blog.csdn.net/renfufei/article/details/10360855
未经允许请勿转载:程序喵 » PhotoSwipe.js 相册展示插件学习
程序喵