DIV CSS 佈局教程網

原生 JavaScript 實作圖片層疊輪播:立體堆疊的焦點圖
編輯:關於JavaScript     

一般輪播是左右平移,而層疊式輪播讓中間的圖最大最亮、兩側依序縮小並加上遮罩,視覺上有種照片牆的立體感。本文示範如何以原生 JavaScript 完成這樣的效果。

效果預覽

功能清單

  • 可自訂圖片尺寸
  • 每隔一段時間自動輪播
  • 每次動畫執行時,圖片的位置、寬高與層級都要一併變化
  • 滑鼠移入圖片時顯示該圖的詳細資訊
  • 點擊左右按鈕可前後翻動

HTML 與 CSS

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<style type="text/css">
* { margin: 0px; padding: 0px; font-family: "Microsoft YaHei"; }
ol, ul { list-style: none; }
cite, em, i { font-style: normal; }

#largerImages {
    position: relative; width: 1000px; margin: 0 auto;
    height: 520px; overflow: hidden;
}
#largerImages li {
    box-shadow: 1px 1px 12px rgba(200, 200, 200, 1);
    width: 368px; height: 368px; position: absolute; top: 10px;
    overflow: hidden; color: #fff;
}
#largerImages li .cover {
    background-color: #333; opacity: 0.5;
    height: 100%; width: 100%; display: block; position: absolute; top: 0px;
}
#largerImages img { border: 0px; width: 100%; height: 100%; }

#largerImages .previous { left: 13%; }
#largerImages .next { left: 53%; }
#largerImages .previous, #largerImages .next {
    cursor: pointer; position: absolute; z-index: 100; top: 25%;
    height: 60px; line-height: 60px; width: 30px; color: #fff; text-align: center;
}
#largerImages .previous span, #largerImages .next span {
    position: absolute; top: 0px; left: 0px; height: 100%; width: 100%;
    display: block; background-color: #000; opacity: 0.4;
}
#largerImages .previous em, #largerImages .next em {
    height: 100%; width: 100%; display: block; position: absolute;
    top: 0px; left: 0px; font-size: 26px; font-family: "宋體";
}
#largerImages li span, #largerImages li em {
    position: absolute; left: 0px; width: 100%; height: 30px; line-height: 30px;
    bottom: 0px; text-align: center; display: block; color: #fff;
}
#largerImages li span { background-color: #000; opacity: 0.5; font-size: 12px; }
</style>
</head>
<body>
<ul id="largerImages">
    <p class="previous"><span></span><em><</em></p>
    <p class="next"><span></span><em>></em></p>
    <li><i class="cover"></i>
        <a href="#"><img src="https://xiu.xiuimg.com/upload/xiu/9/90/qq-a_b_56.com_160106133546.jpg"></a>
        <p class="tab_name"><span></span><em>主播昵稱</em></p>
    </li>
    <li><i class="cover"></i>
        <a href="#"><img src="https://xiu.xiuimg.com/upload/xiu/96/58/dc15170219625_b_56.com_151201150445.jpg"></a>
        <p class="tab_name"><span></span><em>主播昵稱</em></p>
    </li>
    <li><i class="cover"></i>
        <a href="#"><img src="https://xiu.xiuimg.com/upload/xiu/9/90/qq-a_b_56.com_160106133546.jpg"></a>
        <p class="tab_name"><span></span><em>主播昵稱</em></p>
    </li>
    <li><i class="cover"></i>
        <a href="#"><img src="https://xiu.xiuimg.com/upload/xiu/96/58/dc15170219625_b_56.com_151201150445.jpg"></a>
        <p class="tab_name"><span></span><em>主播昵稱</em></p>
    </li>
    <li><i class="cover"></i>
        <a href="#"><img src="https://xiu.xiuimg.com/upload/xiu/9/90/qq-a_b_56.com_160106133546.jpg"></a>
        <p class="tab_name"><span></span><em>主播昵稱</em></p>
    </li>
</ul>
<script type="text/javascript" src="../../lib/seajs/sea.js"></script>
<script type="text/javascript" src="../../lib/base/1.0.x/base.js"></script>
<script type="text/javascript">
seajs.use(['lib/jquery/1.11.x/index.js', '_example/rotateBox/index.js'],
function ($, carousel) {
    carousel.init({
        wapper: $('#largerImages'),
        imgWidth: 450,    // 中間大圖寬
        imgHeight: 300,   // 中間大圖高
        spacing: {
            left: 60,     // 每張圖左側間距
            top: 30,      // 每張圖頂部間距
            width: 60,    // 每張往兩側遞減的寬
            height: 60    // 每張往兩側遞減的高
        }
    });
});
</script>
</body>
</html>

JavaScript

define(function (require, exports, module) {
    'use strict';
    var $ = require('lib/jquery/1.11.x/index.js');

    var carousel = {
        _initData: false,   // 動畫是否進行中,避免重複觸發

        init: function (options) {
            var t = this;
            t._wapper = options.wapper;
            t._grids = t._wapper.find('li');
            t._gridsWidth = options.imgWidth;
            t._gridsHeight = options.imgHeight;
            t._spacing = options.spacing;

            // 取中間那張作為焦點圖
            t._middle = t._grids.length % 2 == 0
                ? t._grids.length / 2
                : parseInt(t._grids.length / 2);

            // 用來暫存各張圖的幾何參數,供翻頁動畫取值
            t._arr = { left: [], top: [], zIndex: [], width: [], height: [] };

            // 每 10 秒自動往前翻一張
            setInterval(function () { $('.previous').click(); }, 10000);

            t._largerImages();   // 初始堆疊定位
            t._reposition();     // 記錄參數並綁定翻頁
            t._mouseEnter(t._grids);
        },

        // 初始堆疊:中間最大最上層,往兩側遞減並加遮罩
        _largerImages: function () {
            var t = this;
            var front = t._middle;    // 左側遞減計數
            var avtive = t._middle;   // 右側遞減計數
            var last = t._grids.length;

            t._grids.each(function (i, img) {
                if (i == t._middle) {
                    t._grids.eq(i).css({
                        zIndex: 99, top: 0, left: t._spacing.left * i,
                        height: t._gridsHeight, width: t._gridsWidth
                    });
                } else if (i < t._middle) {
                    t._grids.eq(i).css({
                        zIndex: i,
                        top: t._spacing.top * front,
                        left: t._spacing.left * i,
                        height: t._gridsHeight - t._spacing.height * front,
                        width: t._gridsWidth - t._spacing.width * front
                    });
                    front--;
                } else {
                    last--;
                    t._grids.eq(last).css({
                        zIndex: i,
                        top: t._spacing.top * avtive,
                        left: t._spacing.left * last + t._spacing.width * avtive,
                        height: t._gridsHeight - t._spacing.height * avtive,
                        width: t._gridsWidth - t._spacing.width * avtive
                    });
                    avtive--;
                }
            });
        },

        // 翻頁:把各屬性存進陣列,再讓每張圖動畫到「下一個位置」的參數
        _reposition: function () {
            var t = this;

            t._grids.each(function (i, img) {
                t._arr.left.push(t._grids.eq(i).position().left);
                t._arr.top.push(t._grids.eq(i).position().top);
                t._arr.width.push(t._grids.eq(i).width());
                t._arr.height.push(t._grids.eq(i).height());
                t._arr.zIndex.push(t._grids.eq(i).css('z-index'));
            });

            // 往前翻
            $('.previous').bind('click', function () {
                if (!t._initData && t._arr.left.length != 0) {
                    t._initData = true;
                    var grids = t._wapper.find('li');
                    for (var i = 1; i < grids.length; i++) {
                        grids.eq(i).animate({
                            zIndex: t._arr.zIndex[i - 1],
                            left: t._arr.left[i - 1],
                            top: t._arr.top[i - 1],
                            width: t._arr.width[i - 1],
                            height: t._arr.height[i - 1]
                        }, 200, function () {
                            t._initData = false;
                            grids.find('i').addClass('cover');
                            grids.eq(t._middle + 1).find('i').removeClass('cover');
                        });
                    }
                    // 第一張移到最後
                    grids.eq(0).animate({
                        left: t._arr.left[grids.length - 1],
                        top: t._arr.top[grids.length - 1],
                        width: t._arr.width[grids.length - 1],
                        height: t._arr.height[grids.length - 1],
                        zIndex: t._arr.zIndex[grids.length - 1]
                    }, 200, function () {
                        $(this).appendTo(t._wapper);
                    });
                }
            });

            // 往後翻
            $('.next').bind('click', function () {
                if (!t._initData && t._arr.left.length != 0) {
                    t._initData = true;
                    var grids = t._wapper.find('li');
                    for (var i = 0; i < grids.length - 1; i++) {
                        grids.eq(i).animate({
                            left: t._arr.left[i + 1],
                            top: t._arr.top[i + 1],
                            width: t._arr.width[i + 1],
                            height: t._arr.height[i + 1],
                            zIndex: t._arr.zIndex[i + 1]
                        }, 200, function () { t._initData = false; });
                    }
                    // 最後一張移到最前
                    grids.eq(grids.length - 1).animate({
                        left: t._arr.left[0],
                        top: t._arr.top[0],
                        width: t._arr.width[0],
                        height: t._arr.height[0],
                        zIndex: t._arr.zIndex[0]
                    }, 200, function () {
                        $(this).prependTo(t._wapper);
                        grids.find('i').addClass('cover');
                        grids.eq(t._middle - 1).find('i').removeClass('cover');
                    });
                }
            });
        },

        // 滑鼠移入顯示資訊條
        _mouseEnter: function (grids) {
            grids.each(function () {
                $(this).mouseenter(function () {
                    $(this).find('.tab_name').animate({ bottom: 0, opacity: 'show' }, 200);
                });
                $(this).mouseleave(function () {
                    $(this).find('.tab_name').animate({ bottom: -50, opacity: 'hide' }, 200);
                });
            });
        }
    };
    return carousel;
});

兩個值得注意的設計細節

_arr 陣列的妙用:与其為每張圖各寫一套目標座標,不如把「位置 0 的參數、位置 1 的參數……」全部快照下來。翻頁時每張圖只要往前(或往後)取一格索引的參數即可,程式碼量大幅減少,圖片數量變化時也不必改動邏輯。

_initData 鎖:動畫進行中若使用者連續點擊,會導致位置錯亂。用一個布林旗標在動畫開始時上鎖、在 callback 中解鎖,是最簡單有效的解法。同一個模式在前面介紹的自訂滾動條中也可見到。

實務上若要更平順的堆疊變換,建議改用 CSS transition 搭配 transform(translate/scale)取代動畫寬高——動 width/height 會觸發重排,效能明顯較差;transform 則可交由合成器處理,在行動裝置上差距尤其明顯。

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