DIV CSS 佈局教程網

自製 jQuery 圖片輪播外掛 ImageScroll:參數、程式碼與调用範例
編輯:JQuery特效代碼     

以下是一個結構單純、易於改造的輪播外掛。它把每個 a 標籤當作一張投影片,自動讀取連結、alt 文字與圖片路徑,再動態組出圖層、遮罩、標題與數字導覽按鈕。

參數說明

參數名稱說明預設值
delay圖片切換間隔,單位毫秒。2000
maskOpacity遮罩層透明度,1 為不透明,0 為全透明。0.6
numOpacity數字按鈕區透明度。0.6
maskBgColor遮罩層背景色。#000
textColor標題文字顏色。#fff
numColor數字按鈕字體顏色。#fff
numBgColor數字按鈕背景色。#000
alterColor選中項數字按鈕的字體顏色。#fff
alterBgColor選中項數字按鈕的背景色。#999

外掛程式碼

(function($){
    $.fn.ImageScroll = function(options) {
        var defaults = {
            delay: 2000,
            maskOpacity: 0.6,
            numOpacity: 0.6,
            maskBgColor: "#000",
            textColor: "#fff",
            numColor: "#fff",
            numBgColor: "#000",
            alterColor: "#fff",
            alterBgColor: "#999"
        };
        options = $.extend(defaults, options);

        var _this = $(this).css("display", "none");
        var _links = [], _texts = [], _urls = [];
        var _list = _this.find("a");
        var _timer;
        var _index = 0;

        // 先收集所有投影片資料
        _list.each(function(){
            var temp = $(this).find("img:eq(0)");
            _links.push($(this).attr("href"));
            _texts.push(temp.attr("alt"));
            _urls.push(temp.attr("src"));
        });

        if (_list.length <= 0) return;
        else _this.html("");

        var _width = _this.width();
        var _height = _this.height();
        var _numCount = _list.length;
        var _numColumn = Math.ceil(_numCount / 2);

        // 圖片層
        var _img = $("<a/>").css({
            "display": "block", "position": "absolute",
            "top": "0px", "left": "0px", "z-index": "2",
            "width": _width + "px", "height": _height + "px",
            "background": "url(" + _urls[0] + ")"
        }).appendTo(_this);

        // 遮罩層
        var _mask = $("<div/>").css({
            "position": "absolute", "left": "0px", "bottom": "0px",
            "z-index": "3", "width": _width + "px", "height": "46px",
            "opacity": options.maskOpacity,
            "background-color": options.maskBgColor
        }).appendTo(_this);

        // 數字按鈕區
        var _num = $("<div/>").css({
            "position": "absolute", "right": "0px", "bottom": "0px",
            "z-index": "5", "width": _numColumn * 22,
            "opacity": options.numOpacity, "height": "44px"
        }).appendTo(_this);

        // 標題文字層
        var _text = $("<div/>").css({
            "position": "absolute", "left": "0px", "bottom": "0px",
            "z-index": "4", "padding-left": "10px",
            "height": "44px", "line-height": "44px",
            "color": options.textColor
        }).html(_texts[0]).appendTo(_this);

        // 產生數字按鈕
        for (var i = 0; i < _numCount; i++) {
            $("<a/>").html(i + 1)
            .css({
                "float": "left", "width": "20px", "height": "20px",
                "text-align": "center",
                "background-color": options.numBgColor,
                "margin": "0px 2px 2px 0px",
                "cursor": "pointer", "line-height": "20px",
                "color": options.numColor
            })
            .mouseover(function() { if (_timer) clearInterval(_timer); }) // 滑入暫停
            .mouseout(function() { _timer = setInterval(alter, options.delay); }) // 滑出續播
            .click(function(){ numClick($(this)); })
            .appendTo(_num);
        }

        var _tempList = _num.find("a");

        function alter() {
            if (_index > _numCount - 1) _index = 0;
            _tempList.eq(_index).click();
        }

        function numClick(obj) {
            var i = _tempList.index(obj);
            _tempList.css({
                "background-color": options.numBgColor,
                "color": options.numColor
            });
            obj.css({
                "background-color": options.alterBgColor,
                "color": options.alterColor
            });
            _img.attr({ "href": _links[i], "target": "_blank" })
                .css({ "opacity": "0", "background": "url(" + _urls[i] + ")" })
                .animate({ "opacity": "1" }, 500);
            _text.html(_texts[i]);
            _index = i + 1;
        }

        setTimeout(alter, 10);
        _timer = setInterval(alter, options.delay);
        _this.css("display", "block");
    };
})(jQuery);

HTML 與呼叫

<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="js/jquery.ImageScroll.js" type="text/javascript"></script>

<style type="text/css">
body { font-family: Verdana, Arial, sans-serif; font-size: 12px; }
#scroll { position: relative; width: 450px; height: 300px; }
</style>

<div id="scroll">
    <a href="https://www.example.com"><img src="images/1.jpg" alt="風景圖片一" /></a>
    <a href="https://www.example.com"><img src="images/2.jpg" alt="風景圖片二" /></a>
    <a href="https://www.example.com"><img src="images/3.jpg" alt="風景圖片三" /></a>
</div>

<script>
$("#scroll").ImageScroll();
</script>

容器必須設定 position: relative 與固定寬高,因為外掛內部所有元素都是 absolute 定位。

帶參數呼叫

<script>
$("#scroll").ImageScroll({ delay: 500, maskBgColor: "#ff0000" });
</script>

可以再加強的地方

這個外掛的可配置性偏基礎,若要用在正式專案,建議補上幾個部分:

  • 圖片預載:目前直接以背景圖載入,首張之外的圖片可能來不及載完就切換,可搭配 Image 物件預先載入。
  • 用 data 屬性取代 alt:alt 原意是替代文字,用來放標題在無障礙上並不合適,建議改讀 data-title。
  • 暫停/繼續控制:滑入暫停已實作,但缺少明確的播放控制 API。
  • 無障礙:數字按鈕目前是 a 標籤但無 href,鍵盤無法聚焦,建議加上 tabindex 與 aria-label。
  • 相容性:舊版 IE 在 opacity 上需要额外的 filter 處理,若不再支援舊瀏覽器可忽略。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved