相冊展示PhotoSwipe.js插件實現(xiàn)
PhotoSwipe.js官網(wǎng):http://photoswipe.com/ ,在這個網(wǎng)站上可以下載到PhotoSwipe的文件以及相關(guān)的例子。
這個組件主要是用來展示圖片、相冊用的,還是很實用的。
一、使用這個組件需要引入兩個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 -->
二、然后頁面結(jié)構(gòu)可以是這樣子的
<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代碼中除了頁面結(jié)構(gòu)外,真正有用的只有 id="Gallery"和<a href="圖片路徑"></a>(在后面會有說明),其他的class神馬的只是起到美化最初的頁面結(jié)構(gòu)的作用(和你真正想要的效果的頁面不同,也就是說,你只要按照上述頁面的結(jié)構(gòu)進行排版,你想要的頁面效果是插件js自身完成的,是不需要你寫效果布局的)。
頁面需要的js和頁面結(jié)構(gòu)都有了,下面就是使用插件了。
三、你可以采用兩種方式進行插件的聲明
1、是用瀏覽器默認的方式addEventListener()的方式進行
document.addEventListener("DOMContentLoaded",function(){ Code.photoSwipe('a','#Gallery'); //此處就涉及到上述頁面結(jié)構(gòu)中的 id="Gallery"和<a href="..."></a>,其中id="Gallery"是容器 //<a href="圖片路徑"></a>,此處href中一定是當前所指向的圖片的路徑 },false);
2、使用Jquery的方式:
$(document).ready(function(){ $("#Gallery a").photoSwipe(); });
四、通過這樣的設置你的頁面大概會是這樣的
一開始的頁面效果:
點擊任意一張圖片后頁面的形式變成如下(這個頁面其實才是我真正想要的頁面):
可以明顯的看到頁面上方<img />中的alt中的內(nèi)容,下方會有四個按鈕,依次代表:關(guān)閉頁面回到最初顯示的樣子(就是上上圖)、自動播放、上一頁圖片、下一頁圖片。
這樣一個相冊的效果就出現(xiàn)了。當然在這個頁面可以使用鼠標左右滑動進行切換,如果在手持設備上還可以通過手指的左右滑動進行。
這個插件還有很多自己的屬性:
allowUserZoom: 允許用戶雙擊放大/移動方式查看圖片. 默認值 = true
autoStartSlideshow: 當PhotoSwipe激活后,自動播放幻燈片. 默認值 = false
allowRotationOnUserZoom: 只有 iOS 支持 - 允許用戶在縮放/平移模式下 用手勢旋轉(zhuǎn)圖像. 默認值 = 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 瀏覽器下的大圖片內(nèi)存溢出將會很有用. 大多數(shù)情況下,normal模式就可以了。
doubleTapSpeed: 雙擊的最大間隔. 默認值 = 300(毫秒)
doubleTapZoomLevel: 當用戶雙擊的時候,放大的倍數(shù), 默認的 "zoom-in"(拉近) 級別. 默認值 = 2.5
enableDrag: 允許拖動上一張/下一張圖片到當前界面. 默認值 = true
enableKeyboard: 允許鍵盤操作(左右箭頭切換,Esc退出,Enter自動播放,空格鍵 顯/隱標題欄/退出). 默認 = true
enableMouseWheel: 允許鼠標滾輪操作. 默認 = true
fadeInSpeed: 淡入效果元素的速度(持續(xù)時間),毫秒. 默認 = 250
fadeOutSpeed: 淡出效果元素的速度(持續(xù)時間),毫秒. 默認 = 250
imageScaleMethod: 圖片縮放方法(模式). 可選值: "fit", "fitNoUpscale" 和 "zoom". 模式"fit" 保證圖像適應屏幕. "fitNoUpscale" 和 "fit"類似但是不會放大圖片. "zoom"將圖片全屏, 但有可能圖片縮放不是等比例的. 默認 = "fit"
invertMouseWheel: 反轉(zhuǎn)鼠標滾輪。默認情況下,鼠標向下滾動將切換到下一張,向上切換到上一張 . 默認 = false
jQueryMobile: 指示 PhotoSwipe 是否集成進了 jQuery Mobile 項目. 默認情況下, PhotoSwipe will try and work this out for you
jQueryMobileDialogHash: jQuery Mobile的window,dialog頁面 所使用的hash標簽。 默認值 = "&ui-state=dialog"
loop: 相冊是否自動循環(huán). 默認 = true
margin: 兩張圖之間的間隔,單位是像素. 默認 = 20
maxUserZoom: 最大放大倍數(shù). 默認 = 5.0 (設置為0將被忽略)
minUserZoom: 圖像最小的縮小倍數(shù). 默認 = 0.5 (設置為0將會忽略)
mouseWheelSpeed: 響應鼠標滾輪的靈敏度. 默認 = 500(毫秒)
nextPreviousSlideSpeed: 當點擊上一張,下一張按鈕后,延遲多少毫秒執(zhí)行切換. 默認 = 0 (立即切換)
preventHide: 阻止用戶關(guān)閉 PhotoSwipe. 同時也會隱藏 工具欄上的"close"關(guān)閉按鈕. 在獨享的頁面使用 (示例是源碼中的 examples/08-exclusive-mode.html). 默認 = false
preventSlideshow: 阻止自動播放模式. 同時也會隱藏工具欄里的播放按鈕. 默認 = false
slideshowDelay: 自動播放模式下,多長時間播放下一張. Default = 3000(毫秒)
slideSpeed: 圖片滑進視圖的時間. 默認 = 250(毫秒)
swipeThreshold: 手指滑動多少像素才觸發(fā)一個 swipe 手勢事件. 默認 = 50
swipeTimeThreshold: 定義觸發(fā)swipe(滑動)手勢的最大毫秒數(shù),太慢了則不會觸發(fā)滑動,只會拖動當前照片的位置. 默認 = 250
slideTimingFunction: 滑動時的 Easing function . 默認 = "ease-out"
zIndex: 初始的zIndex值. 默認 = 1000
enableUIWebViewRepositionTimeout: 檢查設備的方向是否改變。默認 = false
uiWebViewResetPositionDelay: 定時檢查設備的方向是否改變的時間 默認 = 500(毫秒)
preventDefaultTouchEvents: 阻止默認的touch事件,比如頁面滾動。 默認 = true
target: 必須是一個合法的DOM元素(如DIV)。默認是window(全頁面)。而如果是某個低級別的DOM,則在DOM內(nèi)顯示,可能非全屏。
如果不需要展示第一個頁面直接展示第二個頁面,可以這樣設置:
$(document).ready(function(){ // Set up PhotoSwipe, setting "preventHide: true" var thumbEls = Code.photoSwipe('a', '#Gallery', { preventHide: true }); Code.PhotoSwipe.Current.show(0); });
當然這個插件還有很多其他的監(jiān)聽函數(shù):
document.addEventListener('DOMContentLoaded',function(){ //onBeforeShow 在gallery將要展示之前調(diào)用該方法 Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onBeforeShow,function(e){ console.log("onBeforeShow"); }); // onshow 在gallery展示的時候調(diào)用 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的大小和位置發(fā)生變化時或者設備的方向或者窗口大小改變時,出發(fā)該方法 Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onResetPosition, function(e){ console.log('onResetPosition'); }); // onSlideshowStart 當gallery開始滑動展示的時候(此方法可能是我理解有誤,實驗過程中一直沒有觸發(fā)過 的),原文是:When the gallery has started the slideshow Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onSlideshowStart, function(e){ console.log('onSlideshowStart'); }); // onSlideshowStop 當Gallery活動結(jié)束的時候 Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onSlideshowStop, function(e){ console.log('onSlideshowStop'); }); // onBeforeCaptionAndToolbarShow 在頂部狀態(tài)欄和底部的工具欄展示之前觸發(fā) Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onBeforeCaptionAndToolbarShow, function(e){ console.log('onBeforeCaptionAndToolbarShow'); }); // onBeforeCaptionAndToolbarHide 在頂部狀態(tài)欄和底部的工具欄隱藏之前觸發(fā) Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onBeforeCaptionAndToolbarHide, function(e){ console.log('onBeforeCaptionAndToolbarHide'); }); // onViewportClick 在gallery中點擊屏幕的時候觸發(fā),此時一般會觸發(fā)onBeforeCaptionAndToolbarShow 或者onBeforeCaptionAndToolbarHide 方法 Code.PhotoSwipe.Current.addEventListener(Code.PhotoSwipe.EventTypes.onViewportClick, function(e){ console.log('onViewportClick'); }); },false);
由于在photoSwipe官網(wǎng)中沒有發(fā)現(xiàn)api接口的調(diào)用方式,且現(xiàn)在的js水平也不咋地,所以它的一些api接口基本上不是很了解,但是我在查看它的例子的時候發(fā)現(xiàn)有個變量會經(jīng)常出現(xiàn),Code.PhotoSwipe或者Code.PhotoSwipe.Current,所有我就在控制臺中進行了一些實驗,當我輸入Code.PhotoSwipe的時候,出現(xiàn)了如下內(nèi)容:
雖然不能完全看懂里面是什么,但是能看到其中有Current這個元素,接著在控制臺輸入Code.PhotoSwipe.Current,得到下面的內(nèi)容:
在這里面可以發(fā)現(xiàn)更多的信息,比如:currentIndex表明當前所處的圖片是在列表中的索引位置,整個連接起來就是 Code.PhotoSwipe.Current.currentIndex 代表當前圖片所處的索引位置,這個信息對我來說很重要,我們可以通過這個信息在不同的頁面中展示不同的頁面信息。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript中正則表達式判斷匹配規(guī)則及常用方法
JS作為一門常用于web開發(fā)的語言,必然要具備正則這種強大的特性,本文將對JS中的正則用法及常用函數(shù)進行一番總結(jié)2017-08-08uniapp中解析markdown支持網(wǎng)頁和小程序?qū)崿F(xiàn)示例
這篇文章主要為大家介紹了uniapp中解析markdown支持網(wǎng)頁和小程序?qū)崿F(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09JavaScript實現(xiàn)將Word文檔解析成瀏覽器認識的HTML
這篇文章主要為大家詳細介紹了如何使用JavaScript實現(xiàn)將Word文檔解析成瀏覽器認識的HTML,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2024-02-02JavaScript設計模式--簡單工廠模式實例分析【XHR工廠案例】
這篇文章主要介紹了JavaScript設計模式--簡單工廠模式,結(jié)合實例形式分析了JavaScript設計模式中簡單工廠模式原理與XHR工廠應用案例,需要的朋友可以參考下2020-05-05