前端點(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面向?qū)ο蟮某绦蛟O(shè)計(jì)相關(guān)知識(shí)小結(jié)
這篇文章主要介紹了JS面向?qū)ο蟮某绦蛟O(shè)計(jì),現(xiàn)在很多代碼都是基于面向?qū)ο髮?shí)現(xiàn),需要的朋友可以參考下2018-05-05js 立即調(diào)用的函數(shù)表達(dá)式如何寫(xiě)
如果不需要顯示調(diào)用函數(shù), 讓這個(gè)函數(shù)在定義的時(shí)候就執(zhí)行的話(huà), 該如何寫(xiě)才可以呢,下面為大家介紹下具體的實(shí)現(xiàn)步驟,喜歡的朋友可以了解下2014-01-01js實(shí)現(xiàn)數(shù)字從零慢慢增加到指定數(shù)字示例
今天小編就為大家分享一篇js實(shí)現(xiàn)數(shù)字從零慢慢增加到指定數(shù)字示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11js實(shí)現(xiàn)表單提交后不重新刷新當(dāng)前頁(yè)面
本文介紹了如何通過(guò)js實(shí)現(xiàn)表單提交后不重新刷新當(dāng)前頁(yè)面的方法實(shí)例.既提交了FORM保存了數(shù)據(jù),頁(yè)面也不會(huì)跳轉(zhuǎn),很實(shí)用。需要的朋友可以參考下2016-11-11高性能web開(kāi)發(fā) 如何加載JS,JS應(yīng)該放在什么位置?
所有瀏覽器在下載JS的時(shí)候,會(huì)阻止一切其他活動(dòng),比如其他資源的下載,內(nèi)容的呈現(xiàn)等等。至到JS下載、解析、執(zhí)行完畢后才開(kāi)始繼續(xù)并行下載其他資源并呈現(xiàn)內(nèi)容。2010-05-05js如何獲取file控件的完整路徑具體實(shí)現(xiàn)代碼
需要隱藏input file然后獲取它的值,但連jquery都無(wú)法獲取它的值,下面與大家分享下使用js的具體獲取方法,感興趣的朋友可以參考下哈2013-05-05