ColorBox 是一款輕量而美觀的 jQuery 燈箱插件,可以用來彈出圖片組瀏覽層,支持切換、燈箱效果等。本示例就用它來實現圖片組的彈出瀏覽。
以下是完整代碼,把它保存為 html 文件打開即可預覽效果:
<!DOCTYPE html>
<html>
<head>
<title>使用 ColorBox 彈出圖片組浏覽層</title>
<link rel="stylesheet" href="colorbox.css" />
<script src="jquery-1.9.1.min.js"></script>
<script src="jquery.colorbox-min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".keleyi_com").colorbox({
rel: 'group1', //相同 rel 值的元素會被歸為一組,支持上一張/下一張切換
transition: "none", //切換效果,可設為 fade、slide 等
innerWidth: '700px' //彈出層的內容寬度
});
});
</script>
</head>
<body>
<div style="width:400px;margin:0 auto;">
請點擊下面鏈接,即可在彈出層顯示圖片組。
<p><a class="keleyi_com" href="pic1.jpg" title="菊花">圖片一</a></p>
<p><a class="keleyi_com" href="pic2.jpg" title="開心">圖片二</a></p>
<p><a class="keleyi_com" href="pic3.jpg" title="風景">圖片三</a></p>
</div>
</body>
</html>使用要點在於:給所有需要歸為一組的鏈接添加相同的 class(這裡是 keleyi_com),並在 colorbox 方法中通過 rel 參數把它們關聯起來,這樣彈出後就能在圖片之間自由切換。title 屬性則作為每張圖的說明文字顯示。只需引入 ColorBox 的 CSS 與 JS,再一行初始化調用,就能快速搭建起專業的圖片瀏覽層。