DIV CSS 佈局教程網

手 }
編輯:關於JavaScript     

行動裝置螢幕小,小圖往往看不清細節,所以「點擊看大圖、再點一次收起」幾乎是必備互動。這裡用 jQuery 實作一個可重複使用的版本,並補上行動端無法使用 image map 熱點的解法。

一、需求

點擊縮圖 → 全螢幕黑底 → 顯示原圖並置中;再點大圖 → 收起。重點是「置中」必須考慮目前捲動位置,否則在長頁面會跑到看不見的地方。

二、程式碼

 <!DOCTYPE html>
<html>
<meta charset="utf-8"/>
<head runat="server">
<title>JQuery 點擊圖片查看大圖 by starof</title>
<style type="text/css">
 .exampleImg { height:100px; cursor:pointer; }
</style>
<script src="https://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
 $.fn.ImgZoomIn = function () {
  // 半透明黑底
  var bgstr = '<div id="ImgZoomInBG" style="background:#000000;'
   + 'filter:Alpha(Opacity=70); opacity:0.7; position:fixed; left:0; top:0;'
   + ' z-index:10000; width:100%; height:100%; display:none;">'
   + '<iframe src="about:blank" frameborder="0" scrolling="no"'
   + ' style="width:100%; height:100%;"></iframe></div>';

  // 大圖本身,點擊即關閉
  var imgstr = '<img id="ImgZoomInImage" src="' + $(this).attr('src')
   + '" style="cursor:pointer; display:none; position:absolute; z-index:10001;" />';

  if ($('#ImgZoomInBG').length < 1) { $('body').append(bgstr); }
  if ($('#ImgZoomInImage').length < 1) { $('body').append(imgstr); }
  else { $('#ImgZoomInImage').attr('src', $(this).attr('src')); }

  $('#ImgZoomInImage').on('click', function(){
   $('#ImgZoomInImage').hide();
   $('#ImgZoomInBG').hide();
  });

  // 置中計算需加上目前捲動量
  $('#ImgZoomInImage').css('left',
   $(window).scrollLeft() + ($(window).width()  - $('#ImgZoomInImage').width())  / 2);
  $('#ImgZoomInImage').css('top',
   $(window).scrollTop()  + ($(window).height() - $('#ImgZoomInImage').height()) / 2);

  $('#ImgZoomInBG').show();
  $('#ImgZoomInImage').show();
 };

 $(document).ready(function () {
  // 寫法一:用 bind 綁定
  $("#imgTest").bind("click", function () { $(this).ImgZoomIn(); });
  // 寫法二:直接寫在行內
  // <img class="exampleImg" src="images/p1_nav2.png" onClick="$(this).ImgZoomIn();" />
 });
</script>
</head>
<body>
<div>
 <img class="exampleImg" src="images/03.jpg" id="imgTest"/>
</div>
</body>
</html>

三、行動端的熱點問題

手機版頁面常需要一張大圖上開 four 個可點區域(例如四個入口),但行動裝置不支援 image map 的 map/area 熱點。解法是用四個 a 標籤絕對定位蓋在四個象限上:

 <dl>
 <dd style="display:block;">
  <img src="images/four-duche.jpg" onClick="$(this).ImgZoomIn();">
  <a href="javascript:;" class="topleft"     onClick="$(this).ImgZoomIn();"></a>
  <a href="javascript:;" class="topright"    onClick="$(this).ImgZoomIn();"></a>
  <a href="javascript:;" class="bottomleft"  onClick="$(this).ImgZoomIn();"></a>
  <a href="javascript:;" class="bottomright" onClick="$(this).ImgZoomIn();"></a>
 </dd>
</dl>

<style>
 .topleft,.topright,.bottomleft,.bottomright{
  width:50%; height:50%; position:absolute;
 }
 .topleft    { top:0;    left:0;   }
 .topright   { top:0;    right:0;  }
 .bottomleft { bottom:0; left:0;   }
 .bottomright{ bottom:0; right:0;  }
</style>

父層記得設 position:relative,四個 a 才會以圖片為定位基準。若要更精細的區域,用 percentage 微調寬高與偏移即可。

補充:行動端圖片延遲加載

由於行動網路費用與手機運算能力的限制,為使用者省流量是無線應用的重要考量,可以說每一個位元組都該省。節約流量可從三處著手:

  1. 使用快取:善用瀏覽器本機儲存(已於前文討論)。
  2. 延遲加載程式碼:觸底偵測後再透過介面取資料。
  3. 資源延遲加載:圖片進入可視區域才載入;音訊、影片則等使用者點擊(不考慮自動播放)。

以下說明圖片延遲加載的做法,範例基於 jQuery 與 jQuery Mobile。

原理:使用者滑動螢幕,滾動結束後(用 jQuery 的 scrollstop 事件最合適)偵測出現在 viewport 中的圖片,把真正的 src 屬性換上去。

技巧:滾動結束不要立刻偵測,加上一秒延時。使用者可能馬上開始下一次滑動;以目前的網路環境,一秒的延遲足以確認使用者是真的想看這些內容。由於有計時器節流,快速翻動螢幕時不會發出大量請求。

 var refreshTimer = null,
  mebook = mebook || {};

 /* 滾動結束、螢幕靜止一秒後,偵測哪些圖片出現在 viewport 中。
    與 PC 端不同,受無線網速與手機運算能力影響,
    一秒的延遲在手機上是可以接受的。 */
 $(window).on('scrollstop', function () {
  if (refreshTimer) {
   clearTimeout(refreshTimer);
   refreshTimer = null;
  }
  refreshTimer = setTimeout(refreshAll, 1e3);
 });

 $.belowthefold = function (element) {
  var fold = $(window).height() + $(window).scrollTop();
  return fold <= $(element).offset().top;
 };

 $.abovethetop = function (element) {
  var top = $(window).scrollTop();
  return top >= $(element).offset().top + $(element).height();
 };

 /* 判斷元素是否出現在 viewport 中 */
 $.inViewport = function (element) {
  return !$.belowthefold(element) && !$.abovethetop(element);
 };

 mebook.getInViewportList = function () {
  var list = $('#bookList li');
  list.each(function (i) {
   var li = list.eq(i);
   if ($.inViewport(li)) {
    mebook.loadImg(li);
   }
  });
 };

 mebook.loadImg = function (li) {
  var img = li.find('img[_src]');
  if (img.length) {
   var src = img.attr('_src');
   img.attr('src', src).load(function () {
    img.removeAttr('_src');
   });
  }
 };

現代做法:img 加上 loading="lazy" 與 decoding="async",瀏覽器原生就會處理延遲載入,不必自己寫;再搭配 IntersectionObserver 可以更精準地在進入視窗時觸發。自訂的 _src 屬性也建議改用 data-src,並記得為圖片加上 width/height 或 aspect-ratio 以避免版面位移。

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