想為作品集網站增添攝影儀式感?本教學將引導你使用 HTML5 Canvas 與 jQuery,打造一個具備相機快門動畫的互動式照片牆。最終成果將封裝為 jQuery 外掛,輕鬆整合至任何專案。

$.fn.shutterGallery,支援選項配置(快門速度、顏色、回呼)。<div id="gallery" class="shutter-gallery">
<img src="photo1.jpg" alt="作品 1" data-full="photo1-large.jpg">
<img src="photo2.jpg" alt="作品 2" data-full="photo2-large.jpg">
<!-- 更多照片 -->
</div>
<button id="trigger-shutter">拍攝</button>
(function($) {
$.fn.shutterGallery = function(options) {
const settings = $.extend({
shutterSpeed: 300,
shutterColor: '#000',
onShutterComplete: null
}, options);
return this.each(function() {
const $gallery = $(this);
const $imgs = $gallery.find('img');
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// ... 初始化 Canvas 尺寸、繪製首張照片 ...
function animateShutter(callback) {
let progress = 0;
const animate = () => {
progress += 16 / settings.shutterSpeed;
if (progress >= 1) { progress = 1; }
// 繪製快門遮罩(從上往下或左右合攏)
drawShutter(progress);
if (progress < 1) requestAnimationFrame(animate);
else if (callback) callback();
};
requestAnimationFrame(animate);
}
$gallery.on('click', 'img', function() {
animateShutter(() => {
if (settings.onShutterComplete) settings.onShutterComplete(this);
});
});
});
};
})(jQuery);
$('#gallery').shutterGallery({
shutterSpeed: 400,
shutterColor: '#222',
onShutterComplete: function(imgEl) {
// 例如開啟 Lightbox 顯示大圖
console.log('快門完成,照片:', imgEl.dataset.full);
}
});
透過 Canvas 的像素級控制與 jQuery 的封裝優勢,我們快速實現了極具質感的相機快門效果。這類微互動能顯著提升作品集的專業觀感,且外掛化架構讓復用與維護更輕鬆。立即嘗試整合到你的專案,讓訪客感受「咔嚓」的驚喜!