photoswipe.js插件
PhotoSwipe.js官網:http://photoswipe.com/,在這個網站上可以下載到PhotoSwipe的文件以及相關的例子。
這個組件主要是用來展示圖片、相冊用的,還是很實用的。
?
一、使用這個組件需要引入兩個js文件:
1 <script type="text/javascript" src="simple-inheritance.min.js"> 2 <script type="text/javascript" src="code-photoswipe-1.0.11.min.js"> <!--當前最新版的應該是1.0.11 -->二、然后頁面結構可以是這樣子的:
<div id="Gallery"><div class="gallery-row"><div class="gallery-item"><a href="images/01.jpg"><img src="images/thumb/01.jpg" alt="01" /></a></div><div class="gallery-item"><a href="images/02.jpg"><img src="images/thumb/02.jpg" alt=" 02" /></a></div><div class="gallery-item"><a href="images/03.jpg"><img src="images/thumb/03.jpg" alt=" 03" /></a></div></div><div class="gallery-row"><div class="gallery-item"><a href="images/04.jpg"><img src="images/thumb/04.jpg" alt="04" /></a></div><div class="gallery-item"><a href="images/05.jpg"><img src="images/thumb/05.jpg" alt="05" /></a></div><div class="gallery-item"><a href="images/06.jpg"><img src="images/thumb/06.jpg" alt=" 06" /></a></div></div> </div>其實在這段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(); });四、通過這樣的設置你的頁面大概會是這樣的:
一開始的頁面效果:
點擊任意一張圖片后頁面的形式變成如下(這個頁面其實才是我真正想要的頁面):
可以明顯的看到頁面上方<img />中的alt中的內容,下方會有四個按鈕,依次代表:關閉頁面回到最初顯示的樣子(就是上上圖)、自動播放、上一頁圖片、下一頁圖片。
這樣一個相冊的效果就出現了。當然在這個頁面可以使用鼠標左右滑動進行切換,如果在手持設備上還可以通過手指的左右滑動進行。
五.這個插件還有很多自己的屬性:
如果不需要展示第一個頁面直接展示第二個頁面,可以這樣設置:
$(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);?
由于在photoSwipe官網中沒有發現api接口的調用方式,且現在的js水平也不咋地,所以它的一些api接口基本上不是很了解,但是我在查看它的例子的時候發現有個變量會經常出現,Code.PhotoSwipe或者Code.PhotoSwipe.Current,所有我就在控制臺中進行了一些實驗,當我輸入Code.PhotoSwipe的時候,出現了如下內容:
?雖然不能完全看懂里面是什么,但是能看到其中有Current這個元素,接著在控制臺輸入Code.PhotoSwipe.Current,得到下面的內容:
?在這里面可以發現更多的信息,比如:currentIndex表明當前所處的圖片是在列表中的索引位置,整個連接起來就是?Code.PhotoSwipe.Current.currentIndex?代表當前圖片所處的索引位置,這個信息對我來說很重要,我們可以通過這個信息在不同的頁面中展示不同的頁面信息。
六.下面是自己簡單列表事列
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title>測試</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="apple-touch-fullscreen" content="yes" /><meta name="apple-mobile-web-app-capable" content="yes" /><meta name="apple-mobile-web-app-status-bar-style" content="black" /><meta name="format-detection" content="telephone=no" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /><link href="css/styles.css" rel="stylesheet" type="text/css" /><link href="css/photoswipe.css" rel="stylesheet" /> </head> <body><ul id="Gallery"><li><a href="images/full/001.jpg"><img src="images/full/001.jpg" alt="獅子巖觀光留影" ></a></li><li><a href="images/full/003.jpg"><img src="images/full/003.jpg" alt="獅子巖觀光留影" ></a></li><li><a href="images/full/008.jpg"><img src="images/full/008.jpg" alt="獅子巖觀光留影" ></a></li><li><a href="images/full/009.jpg"><img src="images/full/009.jpg" alt="獅子巖觀光留影" ></a></li></ul> <script type="text/javascript" src="js/jquery-1.8.3.js"></script> <script src="js/simple-inheritance.min.js"></script> <script src="js/code-photoswipe-1.0.11.min.js"></script> <script type="text/javascript">document.addEventListener('DOMContentLoaded', function () {Code.photoSwipe('a', '#Gallery');}, false); </script> </body> </html> css body,ul,li{ padding: 0; margin: 0; } img { border: none; } ul,li{list-style: none; }#Gallery li{ float: left; width: 33.33333333%; } #Gallery li a { display: block; margin: 5px; border: 1px solid #3c3c3c; } #Gallery li img { display: block; width: 100%; height: 100px; }總結
以上是生活随笔為你收集整理的photoswipe.js插件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NGINX负载均衡+监控
- 下一篇: 橡皮擦的英语_多省停考全国英语等级考试!