DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> WEB網站前端 >> 網頁特效代碼
jQuery zoomify:點擊縮圖以 Lightbox 方式放大預覽
編輯:網頁特效代碼     

商品列表、作品集、部落格相簿……只要頁面上會出現大量圖片,就一定會遇到「縮圖太小看不清細節」的問題。zoomify 這支 jQuery 外掛把 Lightbox 的放大行為做到極簡:使用者點一下縮圖,高解析版本就會浮層顯示在畫面中央,點擊空白處即可關閉回到原本的瀏覽位置。

效果預覽

下圖為實際執行的示意畫面:

使用要點

整體流程只有三步:載入 jQuery、載入 zoomify 的 CSS 與 JS,最後用一行 $('.example img').zoomify(); 把要啟用的圖片交給外掛管理。外掛會自動判斷圖片的原始尺寸,並依視窗大小縮放,因此不需要額外準備大圖專用的彈出層。

版面這裡示範了 Bootstrap 的格線系統:col-md-12 是單張大圖,兩張並排的用 col-md-6,四張並排則是 col-md-3。搭配 col-xs-* 的寫法,手機上也能自動調整成合理的欄數。

原始碼可由此下載:https://hovertree.com/h/bjaf/hdhgnftp.htm

HTML 與初始化程式碼

<!DOCTYPE html>
<html lang="zh">
<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">
<title>jQuery zoomify圖片放大預覽代碼 - 何問起</title>
<base target="_blank" />
<link rel="stylesheet" href="https://hovertree.com/texiao/jqimg/6/css/bootstrap-grid.min.css">
<link rel="stylesheet" href="https://hovertree.com/texiao/jqimg/6/dist/zoomify.min.css">
<link rel="stylesheet" href="https://hovertree.com/texiao/jqimg/6/css/style.css">

</head>
<body>
<div class="container">
<div class="examples">
<div class="row">
<div class="example col-md-12 col-xs-12 ">
<p><img src="https://hovertree.com/hvtimg/bjafjd/xusm0re6.jpg" class="img-rounded" alt=""></p>
</div>
</div>
<div class="row">
<div class="example col-xs-6 col-md-6">
<p><img src="https://hovertree.com/hvtimg/bjafjd/hs6oi257.jpg" class="img-rounded" alt=""></p>
</div>
<div class="example col-xs-6 col-md-6">
<p><img src="https://hovertree.com/hvtimg/bjafjd/070joeh7.jpg" class="img-rounded" alt=""></p>
</div>
</div>
<div class="row">
<div class="example col-xs-3 col-md-3">
<p><img src="https://hovertree.com/hvtimg/bjafjd/81q15f9j.jpg" class="img-rounded" alt=""></p>
</div>
<div class="example col-xs-3 col-md-3">
<p><img src="https://hovertree.com/hvtimg/bjafjc/rgevo2ea.jpg" class="img-rounded" alt=""></p>
</div>
<div class="example col-xs-3 col-md-3">
<p><img src="https://hovertree.com/hvtimg/201511/6j9j6tk5.png" class="img-rounded" alt=""></p>
</div>
<div class="example col-xs-3 col-md-3">
<p><img src="https://hovertree.com/hvtimg/bjafjf/1lf4d3qj.jpg" class="img-rounded" alt=""></p>
</div>
</div>
</div>
</div>

<script src="https://down.hovertree.com/jquery/jquery-2.2.4.min.js" type="text/javascript"></script>
<script src="https://hovertree.com/texiao/jqimg/6/dist/zoomify.min.js"></script>
<script type="text/javascript">
$('.example img').zoomify();
</script>

<div style="text-align:center;margin:1px 0; font:normal 14px/24px 'MicroSoft YaHei';">
<p>適用浏覽器:FireFox、Chrome、Safari、Opera、Edge、IE11等新版浏覽器. 不支持IE8及以下浏覽器。</p>
<p>來源:<a href="https://hovertree.com/" target="_blank">何問起</a> <a href="https://hovertree.com/menu/texiao/">網頁特效</a>
<a href="https://hovertree.com/h/bjaf/yb41c3xl.htm">代碼說明</a></p>
</div>
</body>
</html>

實作小提醒

若圖片很多,建議為每張 img 補上有意義的 alt 文字,對 SEO 與螢幕閱讀器都有幫助。另外,放大後的大圖最好事先壓縮並使用 loading="lazy",避免一次載入過量圖檔拖慢首屏速度。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved