
<div id="st_main">
<img src="photos/1_large.jpg" alt="照片1" />
</div>
<ul id="st_nav">
<li class="album">
<a href="#" class="st_link">專輯1 <span class="st_arrow_down"></span></a>
<div class="st_thumbs_wrapper">
<div class="st_thumbs">
<img src="photos/1_thumb.jpg" alt="photos/1_large.jpg" />
<img src="photos/2_thumb.jpg" alt="photos/2_large.jpg" />
<img src="photos/3_thumb.jpg" alt="photos/3_large.jpg" />
</div>
</div>
</li>
<li class="album">...</li>
</ul>
<div id="st_loading">Loading...</div>
$(function() {
var $loader = $('#st_loading');
var $list = $('#st_nav');
var $currImage = $('#st_main').children('img:first');
// 預加載首圖
$('
').on('load', function() {
$loader.hide();
$currImage.fadeIn(3000);
// 延遲滑出導航
setTimeout(function() { $list.animate({ left: 0 }, 500); }, 1000);
}).attr('src', $currImage.attr('src'));
// 構建縮圖寬度並啟用滾動
buildThumbs();
});
function buildThumbs() {
$list.children('li.album').each(function() {
var $elem = $(this);
var $wrapper = $elem.find('.st_thumbs_wrapper');
var $thumbs = $wrapper.children(':first');
// 每張縮圖 180px + 3px 間距
var thumbWidth = 183;
var totalWidth = $thumbs.find('img').length * thumbWidth;
$thumbs.css('width', totalWidth + 'px');
makeScrollable($wrapper, $thumbs);
});
}
function makeScrollable($outer, $inner) {
var extra = 800; // 緩衝區
var divWidth = $outer.width();
$outer.css('overflow', 'hidden'); // 隱藏滾動條
var $lastImg = $inner.find('img:last');
$outer.on('mousemove', function(e) {
// 容器總寬度 = 最後一張圖右邊緣 + 緩衝
var containerWidth = $lastImg[0].offsetLeft + $lastImg.outerWidth() + 2 * extra;
// 鼠標相對容器位置比例 -> 映射到滾動距離
var left = (e.pageX - $outer.offset().left) * (containerWidth - divWidth) / divWidth - extra;
$outer.scrollLeft(left);
});
}
// 向下箭頭:展開
$list.find('.st_arrow_down').on('click', function() {
var $this = $(this);
hideThumbs(); // 先收起其他
$this.addClass('st_arrow_up').removeClass('st_arrow_down');
var $album = $this.closest('li');
$album.addClass('current').animate({ height: '170px' }, 200);
$this.parent().next('.st_thumbs_wrapper').show(200);
});
// 向上箭頭:收起
$list.find('.st_arrow_up').on('click', function() {
var $this = $(this);
$this.addClass('st_arrow_down').removeClass('st_arrow_up');
hideThumbs();
});
function hideThumbs() {
$list.find('li.current')
.animate({ height: '50px' }, 400, function() {
$(this).removeClass('current');
})
.find('.st_thumbs_wrapper').hide(200)
.end()
.find('.st_arrow_up')
.addClass('st_arrow_down').removeClass('st_arrow_up');
}
$list.find('.st_thumbs img').on('click', function() {
var $thumb = $(this);
$loader.show();
$('
').on('load', function() {
var $newImg = $(this);
var $oldImg = $('#st_main').children('img:first');
$newImg.insertBefore($oldImg); // 新圖插在舊圖前
$loader.hide();
$oldImg.fadeOut(2000, function() { $(this).remove(); }); // 舊圖淡出移除
}).attr('src', $thumb.attr('alt')); // alt 存儲大圖路徑
})
.on('mouseenter', function() { $(this).stop().animate({ opacity: 1 }); })
.on('mouseleave', function() { $(this).stop().animate({ opacity: 0.7 }); });
$(document).on('keydown', function(e) {
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return; // 輸入框不攔截
var $currentAlbum = $list.find('li.current');
var $visibleThumbs = $currentAlbum.find('.st_thumbs img:visible');
var currentIdx = $visibleThumbs.index($visibleThumbs.filter('[src="' + $('#st_main img').attr('src') + '"]'));
if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
var nextIdx = (currentIdx + 1) % $visibleThumbs.length;
$visibleThumbs.eq(nextIdx).trigger('click');
} else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
var prevIdx = (currentIdx - 1 + $visibleThumbs.length) % $visibleThumbs.length;
$visibleThumbs.eq(prevIdx).trigger('click');
}
});
data-src,IntersectionObserver 進入視口時賦值 srcnew Image().src = nextSrc$wrapper.on('click', 'img', fn) 替換逐個綁定transition: opacity 0.5s 替代 .animate({opacity}),啟用 GPU 加速mousemove 綁定 requestAnimationFrame 節流本相冊通過模塊化函數(buildThumbs/makeScrollable/hideThumbs)實現關注點分離。核心亮點:
scrollLeft = (mouseX / containerWidth) * (contentWidth - containerWidth) 實現無滾動條的自然滾動體驗current 類標記展開專輯,確保同一時間只有一組縮圖展開代碼結構清晰,易於擴展為響應式、觸控友好、支持鍵盤操作的專業級相冊組件。