DIV CSS 佈局教程網

JavaScript 實現圖片 DIV 豎向滑動展開特效
編輯:關於JavaScript     

這是一個經典的「手風琴式」豎向滑動菜單:滑鼠移入某個項目時,該項目展開、其餘收合,移出後回到平均分布狀態。以下為完整實作。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>圖片滑動展示效果</title>
<script type="text/javascript">
var $$ = function (id) { return "string" == typeof id ? document.getElementById(id) : id; };

// 統一事件物件,補齊 IE 與標準瀏覽器的差異
function Event(e) {
    var oEvent = document.all ? window.event : e;
    if (document.all) {
        if (oEvent.type == "mouseout") {
            oEvent.relatedTarget = oEvent.toElement;
        } else if (oEvent.type == "mouseover") {
            oEvent.relatedTarget = oEvent.fromElement;
        }
    }
    return oEvent;
}

function addEventHandler(oTarget, sEventType, fnHandler) {
    if (oTarget.addEventListener) {
        oTarget.addEventListener(sEventType, fnHandler, false);
    } else if (oTarget.attachEvent) {
        oTarget.attachEvent("on" + sEventType, fnHandler);
    } else {
        oTarget["on" + sEventType] = fnHandler;
    }
}

var Class = {
    create: function () {
        return function () { this.initialize.apply(this, arguments); };
    }
};

Object.extend = function (destination, source) {
    for (var property in source) { destination[property] = source[property]; }
    return destination;
};

var GlideView = Class.create();
GlideView.prototype = {
    // 容器物件、容器高度、項目標籤、展開後最大高度
    initialize: function (obj, iHeight, sTag, iMaxHeight, options) {
        var oContainer = $$(obj), oThis = this, len = 0;
        this.SetOptions(options);
        this.Step = Math.abs(this.options.Step);
        this.Time = Math.abs(this.options.Time);
        this._list = oContainer.getElementsByTagName(sTag);
        len = this._list.length;
        this._count = len;
        this._height = parseInt(iHeight / len);                             // 收合時每項高度
        this._height_max = parseInt(iMaxHeight);                            // 展開項高度
        this._height_min = parseInt((iHeight - this._height_max) / (len - 1)); // 其餘項高度
        this._timer = null;
        this.Each(function (oList, oText, i) {
            oList._target = this._height * i;   // 自訂屬性存放目標 top
            oList.style.top = oList._target + "px";
            oList.style.position = "absolute";
            addEventHandler(oList, "mouseover", function () {
                oThis.Set.call(oThis, i);
            });
        });
        // 容器樣式
        oContainer.style.height = iHeight + "px";
        oContainer.style.overflow = "hidden";
        oContainer.style.position = "relative";
        // 移出容器時回到預設狀態
        addEventHandler(oContainer, "mouseout", function (e) {
            var o = Event(e).relatedTarget;
            if (oContainer.contains ? !oContainer.contains(o)
                : oContainer != o && !(oContainer.compareDocumentPosition(o) & 16)) {
                oThis.Set.call(oThis, -1);
            }
        });
    },
    SetOptions: function (options) {
        this.options = {
            Step: 20,        // 滑動變化率
            Time: 3,         // 滑動延時(毫秒)
            TextTag: "",     // 說明容器標籤
            TextHeight: 0    // 說明容器高度
        };
        Object.extend(this.options, options || {});
    },
    // index < 0 代表移出,全部平均分布
    Set: function (index) {
        if (index < 0) {
            this.Each(function (oList, oText, i) {
                oList._target = this._height * i;
                if (oText) { oText._target = this._height_text; }
            });
        } else {
            this.Each(function (oList, oText, i) {
                oList._target = (i <= index)
                    ? this._height_min * i
                    : this._height_min * (i - 1) + this._height_max;
                if (oText) { oText._target = (i == index) ? 0 : this._height_text; }
            });
        }
        this.Move();
    },
    // 以逐步逼近的方式移動,避免直接跳變
    Move: function () {
        clearTimeout(this._timer);
        var bFinish = true;
        this.Each(function (oList, oText, i) {
            var iNow = parseInt(oList.style.top), iStep = this.GetStep(oList._target, iNow);
            if (iStep != 0) {
                bFinish = false;
                oList.style.top = (iNow + iStep) + "px";
            }
        });
        if (!bFinish) {
            var oThis = this;
            this._timer = setTimeout(function () { oThis.Move(); }, this.Time);
        }
    },
    GetStep: function (iTarget, iNow) {
        var iStep = (iTarget - iNow) / this.Step;
        if (iStep == 0) return 0;
        if (Math.abs(iStep) < 1) return (iStep > 0 ? 1 : -1);
        return iStep;
    },
    Each: function (fun) {
        for (var i = 0; i < this._count; i++) {
            fun.call(this, this._list[i], (this.Showtext ? this._text[i] : null), i);
        }
    }
};
</script>
<style type="text/css">
#idGlideView { height:314px; width:325px; margin:0 auto; }
#idGlideView div { width:325px; height:314px; }
</style>
</head>
<body>
<div id="idGlideView">
  <div style="background-color:#006699;">滑鼠移到這裡試試看!</div>
  <div style="background-color:#FF9933;">滑鼠移到這裡試試看!</div>
</div>
<SCRIPT>
var gv = new GlideView("idGlideView", 314, "div", 280);
</SCRIPT>
</body>
</html>

這段程式碼有幾個值得借鏡的設計:GetStep 用「距離除以步長係數」的方式計算每次位移量,並在差值小於 1 時強制補 1 像素,避免無限逼近造成 timer 永遠停不下來;mouseout 的判斷則用 relatedTarget 確認滑鼠是否真的離開了容器,而不是只在子元素之間移動。若要遷移到現代專案,可以改用 CSS transition 搭配 transform: translateY,程式碼量能少一大半。

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