PhotoSwipe.js 相簿展示外掛程式學習

來源:互聯網
上載者:User

標籤:android   des   cWeb   style   blog   http   

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/full/01.jpg"><img src="images/thumb/01.jpg" alt="Image 01" /></a></div>        <div class="gallery-item"><a href="images/full/02.jpg"><img src="images/thumb/02.jpg" alt="Image 02" /></a></div>        <div class="gallery-item"><a href="images/full/03.jpg"><img src="images/thumb/03.jpg" alt="Image 03" /></a></div>    </div>    <div class="gallery-row">        <div class="gallery-item"><a href="images/full/04.jpg"><img src="images/thumb/04.jpg" alt="Image 04" /></a></div>        <div class="gallery-item"><a href="images/full/05.jpg"><img src="images/thumb/05.jpg" alt="Image 05" /></a></div>        <div class="gallery-item"><a href="images/full/06.jpg"><img src="images/thumb/06.jpg" alt="Image 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中的內容,下方會有四個按鈕,依次代表:關閉頁面回到最初顯示的樣子(就是上)、自動播放、上一頁圖片、下一頁圖片。

這樣一個相簿的效果就出現了。當然在這個頁面可以使用滑鼠左右滑動進行切換,如果在手持功能上還可以通過手指的左右滑動進行。

 

這個外掛程式還有很多自己的屬性:

allowUserZoom: 允許使用者雙擊放大/移動方式查看圖片. 預設值 = trueautoStartSlideshow: 當PhotoSwipe啟用後,自動播放投影片. 預設值 = falseallowRotationOnUserZoom: 只有 iOS 支援 - 允許使用者在縮放/平移模式下 用手勢旋轉映像. 預設值 = falsebackButtonHideEnabled: 按返回鍵隱藏相簿投影片. 主要是 Android 和 Blackberry使用. 支援 BB6, Android v2.1, iOS 4 以及更新版本. 預設值 = truecaptionAndToolbarAutoHideDelay: 標題列和工具列自動隱藏的延遲時間. 預設值為 = 5000(毫秒). 如果設為 0 則不會自動隱藏(tap/單擊切換顯隱)captionAndToolbarFlipPosition: 標題列和工具列切換位置(讓 caption顯示在底部而 toolbar顯示在頂部). 預設值 = falsecaptionAndToolbarHide: 隱藏 標題列和工具列. 預設值 = falsecaptionAndToolbarOpacity: 標題列和工具列 的透明度(0-1). 預設值 = 0.8captionAndToolbarShowEmptyCaptions: 即使當前圖片的標題是空,也顯示標題列. 預設值 = truecacheMode: 緩衝模式,Code.PhotoSwipe.Cache.Mode.normal (預設,正常) 或者 Code.PhotoSwipe.Cache.Mode.aggressive(激進,積極). 決定 PhotoSwipe 如何管理圖片緩衝 cache.Aggressive 模式將會積極地地設定非 "當前,上一張,下一張"的圖片為空白的類型. 對於老版本iOS 瀏覽器下的大圖片記憶體溢出將會很有用. 大多數情況下,normal模式就可以了。doubleTapSpeed: 雙擊的最大間隔. 預設值 = 300(毫秒)doubleTapZoomLevel: 當使用者雙擊的時候,放大的倍數, 預設的 "zoom-in"(拉近) 層級. 預設值 = 2.5enableDrag: 允許拖動上一張/下一張圖片到當前介面. 預設值 = trueenableKeyboard: 允許鍵盤操作(左右箭頭切換,Esc退出,Enter自動播放,空格鍵 顯/隱標題列/退出). 預設 = trueenableMouseWheel: 允許滑鼠滾輪操作. 預設 = truefadeInSpeed: 淡入效果元素的速度(期間),毫秒. 預設 = 250fadeOutSpeed: 淡出效果元素的速度(期間),毫秒. 預設 = 250imageScaleMethod: 圖片縮放方法(模式). 可選值: "fit", "fitNoUpscale" 和 "zoom". 模式"fit" 保證映像適應螢幕. "fitNoUpscale" 和 "fit"類似但是不會放大圖片. "zoom"將圖片全屏, 但有可能圖片縮放不是等比例的. 預設 = "fit"invertMouseWheel: 反轉滑鼠滾輪。預設情況下,滑鼠向下滾動將切換到下一張,向上切換到上一張 . 預設 = falsejQueryMobile: 指示 PhotoSwipe 是否整合進了 jQuery Mobile 項目. 預設情況下, PhotoSwipe will try and work this out for youjQueryMobileDialogHash: jQuery Mobile的window,dialog頁面 所使用的hash標籤。 預設值 = "&ui-state=dialog"loop: 相簿是否自動迴圈. 預設 = truemargin: 兩張圖之間的間隔,單位是像素. 預設 = 20maxUserZoom: 最大放大倍數. 預設 = 5.0 (設定為0將被忽略)minUserZoom: 映像最小的縮小倍數. 預設 = 0.5 (設定為0將會忽略)mouseWheelSpeed: 響應滑鼠滾輪的靈敏度. 預設 = 500(毫秒)nextPreviousSlideSpeed: 當點擊上一張,下一張按鈕後,延遲多少毫秒執行切換. 預設 = 0 (立即切換)preventHide: 阻止使用者關閉 PhotoSwipe. 同時也會隱藏 工具列上的"close"關閉按鈕. 在獨享的頁面使用 (樣本是源碼中的 examples/08-exclusive-mode.html). 預設 = falsepreventSlideshow: 阻止自動播放模式. 同時也會隱藏工具列裡的播放按鈕. 預設 = falseslideshowDelay: 自動播放模式下,多長時間播放下一張. Default = 3000(毫秒)slideSpeed: 圖片滑進視圖的時間. 預設 = 250(毫秒)swipeThreshold: 手指滑動多少像素才觸發一個 swipe 手勢事件. 預設 = 50swipeTimeThreshold: 定義觸發swipe(滑動)手勢的最大毫秒數,太慢了則不會觸發滑動,只會拖動當前照片的位置. 預設 = 250slideTimingFunction: 滑動時的 Easing function . 預設 = "ease-out"zIndex: 初始的zIndex值. 預設 = 1000enableUIWebViewRepositionTimeout: 檢查裝置的方向是否改變。預設 = falseuiWebViewResetPositionDelay: 定時檢查裝置的方向是否改變的時間 預設 = 500(毫秒)preventDefaultTouchEvents: 阻止預設的touch事件,比如頁面滾動。 預設 = truetarget: 必須是一個合法的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 slideshowCode.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 代表當前圖片所處的索引位置,這個資訊對我來說很重要,我們可以通過這個資訊在不同的頁面中展示不同的頁面資訊。

其他的方法還有待進一步學習。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.