DIV CSS 佈局教程網

仿 QQ 商城的左右點擊滾動焦點圖特效實作
編輯:JavaScript綜合知識     

這個焦點圖外掛的效果參考 QQ 商城:所有圖片橫向排列,點擊左右箭頭整排位移,底部有數字指示器可點擊跳轉,並支援自動播放。以下拆解它的實作。

CSS 結構

.focus { width:800px; height:280px; overflow:hidden; position:relative; }
.focus ul { height:380px; position:absolute; }
.focus ul li { float:left; width:800px; height:280px; overflow:hidden;
               position:relative; background:#000; }
.focus .btnBg { position:absolute; width:800px; height:20px; left:0; bottom:0;
                background:#000; display:none; }
.focus .btn { position:absolute; width:780px; height:23px; padding:0 10px;
              right:0; bottom:6px; text-align:right; }
.focus .btn span { display:inline-block; width:25px; height:24px; line-height:24px;
                   text-align:center; margin-left:5px; cursor:pointer; background:#fff; }
.focus .btn span.on { background:#fff; }
.focus .preNext { width:45px; height:100px; position:absolute; top:90px;
                  background:url(img/sprite.png) no-repeat 0 0; cursor:pointer; }
.focus .pre { left:0; }
.focus .next { right:0; background-position:right top; }

外層 .focus 用 overflow: hidden 裁切,ul 以 absolute 承載並靠 left 位移;左右箭頭用同一張 sprite 圖的兩個背景位置,省下載入次數。

HTML 與初始化

<div class="focus" id="focus001">
  <ul>
    <li><a href="#" target="_blank"><img src="img/03.jpg" alt="" /></a></li>
    <li><a href="#" target="_blank"><img src="img/02.jpg" /></a></li>
    <li><a href="#" target="_blank"><img src="img/03.jpg" alt="" /></a></li>
  </ul>
</div>

<script>
$(document).ready(function () {
  $.focus("#focus001");
});
</script>

外掛核心

$(function () {
  jQuery.focus = function (slid) {
    var sWidth = $(slid).width();               // 顯示面積寬度
    var len = $(slid).find("ul li").length;     // 圖片張數
    var index = 0;
    var picTimer;

    // 動態產生數字按鈕與左右箭頭
    var btn = "<div class='btnBg'></div><div class='btn'>";
    for (var i = 0; i < len; i++) {
      btn += "<span>" + (i + 1) + "</span>";
    }
    btn += "</div><div class='preNext pre'></div><div class='preNext next'></div>";
    $(slid).append(btn);

    $(slid).find("div.btnBg").css("opacity", 0.5);

    // 數字按鈕:滑入即切換
    $(slid + " div.btn span").css("opacity", 0.4).mouseenter(function () {
      index = $(slid + " .btn span").index(this);
      showPics(index);
    }).eq(0).trigger("mouseenter");

    // 箭頭透明度
    $(slid + " .preNext").css("opacity", 0.2).hover(
      function () { $(this).stop(true, false).animate({ "opacity": "0.5" }, 300); },
      function () { $(this).stop(true, false).animate({ "opacity": "0.2" }, 300); }
    );

    // 上一張
    $(slid + " .pre").click(function () {
      index -= 1;
      if (index == -1) index = len - 1;
      showPics(index);
    });

    // 下一張
    $(slid + " .next").click(function () {
      index += 1;
      if (index == len) index = 0;
      showPics(index);
    });

    // 所有 li 同一排浮動,需撐開 ul 寬度
    $(slid + " ul").css("width", sWidth * len);

    // 滑入暫停自動播放,滑出恢復
    $(slid).hover(function () {
      clearInterval(picTimer);
    }, function () {
      picTimer = setInterval(function () {
        showPics(index);
        index++;
        if (index == len) index = 0;
      }, 4000);   // 4000 毫秒為自動播放間隔
    }).trigger("mouseleave");

    // 顯示指定圖片
    function showPics(index) {
      var nowLeft = -index * sWidth;
      $(slid + " ul").stop(true, false).animate({ "left": nowLeft }, 300);
      $(slid + " .btn span").removeClass("on").eq(index).addClass("on");
      $(slid + " .btn span").stop(true, false).animate({ "opacity": "0.4" }, 300)
        .eq(index).stop(true, false).animate({ "opacity": "1" }, 300);
    }
  };
});

幾個關鍵設計

ul 寬度必須撐開:因為 li 是 float 浮動,若 ul 沒有明確寬度就會換行,所以要用 sWidth * len 算出總寬。這是這類橫向捲動最常見的錯誤來源。

stop(true, false) 的作用:快速連續點擊時,前一次動畫尚未結束又啟動新的,會造成動畫堆疊、位移錯亂。stop 的第二個參數為 false 表示「清空佇列但不跳到最終狀態」,讓新動畫能從當前位置接手。

數字按鈕用 mouseenter 而非 click:滑過即切換,互動更即時;eq(0).trigger("mouseenter") 則是主動觸發第一次,讓初始狀態正確顯示。

可調整與可改進之處

這個版本隱藏了半透明背景條(.btnBg 設為 display:none),並把指示器改成顯示 1234 數字而非單純色塊。若不需要這些改動,把樣式改回即可。作者已在原始程式中註解清楚各項參數,調整自動播放間隔、動畫速度都很直接。

若要在新專案中使用,建議改用現代方案:CSS transform 搭配 transition 做位移(比 animate 改 left 效能好得多,因為能走 GPU 合成層),並加上 aria-label 與鍵盤操作支援,讓輪播對螢幕閱讀器與鍵盤使用者友善。市面上 Swiper、slick 等成熟套件已處理好這些細節,除非有特殊需求,否則不必自行造輪子。

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