萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

jQuery 全屏滾動相冊:帶導航縮圖、平滑過渡、鍵盤控制完整實現

效果特性

  • 全屏大圖展示,支持鍵盤 ←/→ 切換
  • 頂部/側邊導航:專輯分組、縮圖滾動、展開收起動畫
  • 縮圖點擊切換大圖,淡入淡出過渡
  • 鼠標跟隨縮圖區域自動滾動
  • 加載狀態指示器

全屏滾動相冊效果圖

HTML 結構

<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>

核心 JS 邏輯解析

1. 初始化與首圖加載

$(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();
});

2. 縮圖容器寬度計算與滾動綁定

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);
  });
}

3. 專輯展開/收起交互

// 向下箭頭:展開
$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');
}

4. 縮圖點擊切換大圖(淡入淡出)

$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');
  }
});

性能優化建議

  1. 圖片懶加載:縮圖初始 data-src,IntersectionObserver 進入視口時賦值 src
  2. 大圖預加載:切換前預加載下一張,new Image().src = nextSrc
  3. 事件委託:縮圖點擊用 $wrapper.on('click', 'img', fn) 替換逐個綁定
  4. CSS 動畫替代 JS 動畫:transition: opacity 0.5s 替代 .animate({opacity}),啟用 GPU 加速
  5. 防抖滾動:mousemove 綁定 requestAnimationFrame 節流

總結

本相冊通過模塊化函數(buildThumbs/makeScrollable/hideThumbs)實現關注點分離。核心亮點:

  • 鼠標位置映射滾動距離:scrollLeft = (mouseX / containerWidth) * (contentWidth - containerWidth) 實現無滾動條的自然滾動體驗
  • 大圖無縫切換:新圖插入舊圖前 + 舊圖淡出移除,避免白屏閃爍
  • 狀態機管理:current 類標記展開專輯,確保同一時間只有一組縮圖展開

代碼結構清晰,易於擴展為響應式、觸控友好、支持鍵盤操作的專業級相冊組件。

copyright © 萬盛學電腦網 all rights reserved