前端點(diǎn)擊預(yù)覽圖片Viewer.js使用方法(簡(jiǎn)單的操作)
1、 Viewer.js簡(jiǎn)介
- Viewer.js 是一款強(qiáng)大的圖片查看器。我們通過(guò)Viewer.js 在頁(yè)面上添加強(qiáng)大的圖片查看功能,同時(shí),這款優(yōu)秀的插件配置操作起來(lái)也非常的方便。
Viewer.js分為2個(gè)版本,js版本和jquery版本,下載地址分別為
純JS版本:GitHub - fengyuanchen/viewerjs: JavaScript image viewer.
jQuery 版本:GitHub - fengyuanchen/jquery-viewer: A jQuery plugin wrapper for Viewer.js.
2、Viewer.js支持的功能
- 支持移動(dòng)設(shè)備觸摸事件
- 支持響應(yīng)式
- 支持放大/縮小
- 支持旋轉(zhuǎn)(類(lèi)似微博的圖片旋轉(zhuǎn))
- 支持水平/垂直翻轉(zhuǎn)
- 支持圖片移動(dòng)
- 支持鍵盤(pán)
- 支持全屏幻燈片模式(可做屏保)
- 支持縮略圖
- 支持標(biāo)題顯示
- 支持多種自定義事件
3、Viewer.js的API
名稱(chēng) | 類(lèi)型 | 默認(rèn)值 | 說(shuō)明 |
---|---|---|---|
inline | 布爾值 | false | 啟用 inline 模式 |
button | 布爾值 | true | 顯示右上角關(guān)閉按鈕(jQuery 版本無(wú)效) |
navbar | 布爾值/整型 | true | 顯示縮略圖導(dǎo)航 |
title | 布爾值/整型 | true | 顯示當(dāng)前圖片的標(biāo)題(現(xiàn)實(shí) alt 屬性及圖片尺寸) |
toolbar | 布爾值/整型 | true | 顯示工具欄 |
tooltip | 布爾值 | true | 顯示縮放百分比 |
movable | 布爾值 | true | 圖片是否可移動(dòng) |
zoomable | 布爾值 | true | 圖片是否可縮放 |
rotatable | 布爾值 | true | 圖片是否可旋轉(zhuǎn) |
scalable | 布爾值 | true | 圖片是否可翻轉(zhuǎn) |
transition | 布爾值 | true | 使用 CSS3 過(guò)度 |
fullscreen | 布爾值 | true | 播放時(shí)是否全屏 |
keyboard | 布爾值 | true | 是否支持鍵盤(pán) |
interval | 整型 | 5000 | 播放間隔,單位為毫秒 |
zoomRatio | 浮點(diǎn)型 | 0.1 | 鼠標(biāo)滾動(dòng)時(shí)的縮放比例 |
minZoomRatio | 浮點(diǎn)型 | 0.01 | 最小縮放比例 |
maxZoomRatio | 數(shù)字 | 100 | 最大縮放比例 |
zIndex | 數(shù)字 | 2015 | 設(shè)置圖片查看器 modal 模式時(shí)的 z-index |
zIndexInline | 數(shù)字 | 0 | 設(shè)置圖片查看器 inline 模式時(shí)的 z-index |
url | 字符串/函數(shù) | src | 設(shè)置大圖片的 url |
build | 函數(shù) | null | 回調(diào)函數(shù),具體查看演示 |
built | 函數(shù) | null | 回調(diào)函數(shù),具體查看演示 |
show | 函數(shù) | null | 回調(diào)函數(shù),具體查看演示 |
shown | 函數(shù) | null | 回調(diào)函數(shù),具體查看演示 |
hide | 函數(shù) | null | 回調(diào)函數(shù),具體查看演示 |
hidden | 函數(shù) | null | 回調(diào)函數(shù),具體查看演示 |
view | 函數(shù) | null | 回調(diào)函數(shù),具體查看演示 |
viewed | 函數(shù) | null | 回調(diào)函數(shù),具體查看演示 |
4、 按照和引入的方式
- Javascript版:在html文件的head中直接引入文件
<link href="static/js/viewer.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet"> <script src="static/js/viewer.min.js" type="application/javascript"></script>
- jQuery版,需要多引入一個(gè),在html文件的head中直接引入文件
<link href="static/js/viewer.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet"> <script src="static/js/viewer.min.js" type="application/javascript"></script> <script src="static/js/jquery.min.js"></script>
- npm,直接install
npm install viewerjs
5、使用方法(簡(jiǎn)單的操作)
- 我這里用的是一個(gè)js版本,簡(jiǎn)單做了一個(gè)demo , html代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <link href="static/js/viewer.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet"> <script src="static/js/viewer.min.js" type="application/javascript"></script> <script src="static/js/jquery.min.js"></script> </head> <body> <div class="container "> <div id="image-gallery" style="display: none"> //這里面可以放多個(gè)圖片,顯示按照指定的索引進(jìn)行(索引從0開(kāi)始) <img src="static/images/banner2.jpg" alt="Image 1"> </div> // 點(diǎn)擊顯示預(yù)覽圖片 <div id="btn01" style="width: 70px;height: 30px;border: 1px solid #ccc;text-align: center;line-height: 30px;">點(diǎn)擊</div> </div> </body> //這里放js操作 </html
- js代碼,點(diǎn)擊顯示圖片,操作事件
<script> // 添加事件 document.addEventListener('DOMContentLoaded', function () { // 獲取點(diǎn)擊后需要顯示的圖片id var gallery = document.getElementById('image-gallery'); // 給顯示的圖片添加一下屬性在dom中 var viewer = new Viewer(gallery, { inline: false, button: true, navbar: true, toolbar: true, title: true, tooltip: true, movable: true, zoomable: true, rotatable: true, scalable: true, }); // 添加按鈕點(diǎn)擊事件 document.getElementById('btn01').addEventListener('click', function () { // 這里指定要預(yù)覽的圖片索引 viewer.view(0); // 預(yù)覽第一張圖片(索引從0開(kāi)始) }); }); </script>
- 效果展示
總結(jié)
到此這篇關(guān)于前端點(diǎn)擊預(yù)覽圖片Viewer.js使用的文章就介紹到這了,更多相關(guān)前端點(diǎn)擊預(yù)覽圖片Viewer.js內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js 單引號(hào)替換成雙引號(hào),雙引號(hào)替換成單引號(hào)的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇js 單引號(hào)替換成雙引號(hào),雙引號(hào)替換成單引號(hào)的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02Javascript寫(xiě)入txt和讀取txt文件示例
這篇文章主要介紹了Javascript寫(xiě)入txt和讀取txt文件的方法,需要的朋友可以參考下2014-02-02Javascript節(jié)點(diǎn)關(guān)系實(shí)例分析
這篇文章主要介紹了Javascript節(jié)點(diǎn)關(guān)系,實(shí)例分析了javascript操作父子節(jié)點(diǎn)及兄弟節(jié)點(diǎn)的相關(guān)技巧,需要的朋友可以參考下2015-05-05Javascript之旅 對(duì)象的原型鏈之由來(lái)
本人是Javascript菜鳥(niǎo),下面是前幾天學(xué)習(xí)Javascript的旅程心得,希望對(duì)和我一樣的入門(mén)者有點(diǎn)用,也希望高手批評(píng)指正。2010-08-08webapi根據(jù)id獲取元素的實(shí)現(xiàn)思路
掌握document.getElementById() 根據(jù)id獲取元素,在頁(yè)面畫(huà)出一個(gè)寬200 高200 粉色的盒子, 在控制臺(tái)打印這個(gè)盒子,接到這樣的需求如何處理呢,下面小編給大家分享webapi根據(jù)id獲取元素的實(shí)現(xiàn)思路,感興趣的朋友一起看看吧2024-02-02Javascript學(xué)習(xí)之談?wù)凧S的全局變量跟局部變量(推薦)
這篇文章主要介紹了Javascript學(xué)習(xí)之談?wù)凧S的全局變量跟局部變量雖然腳本之家小編以前發(fā)過(guò),但還是這篇文章整理的比較好,需要的朋友可以參考一下2016-08-08node在兩個(gè)div之間移動(dòng),用ztree實(shí)現(xiàn)
本文介紹了“node在兩個(gè)div之間移動(dòng),用ztree實(shí)現(xiàn)”的方法,需要的朋友可以參考一下2013-03-03echarts自定義tooltip中的內(nèi)容代碼示例
在ECharts中,通過(guò)formatter函數(shù)自定義圖例樣式,可以實(shí)現(xiàn)更靈活的圖表展示,滿足特定的視覺(jué)需求,這篇文章主要介紹了echarts自定義tooltip中內(nèi)容的相關(guān)資料,需要的朋友可以參考下2024-10-10