本文將分享如何使用 JavaScript 實現圖片 DIV 的縱向滑動展示效果,透過封裝一個 GlideView 類別,讓滑鼠移到不同區塊時能平滑展開或收合,涉及操作 div 層的相關技巧。
整體以物件導向方式封裝:透過 Class.create 建立類別,GlideView 的 initialize 方法負責初始化容器與各滑動項,Set 方法設定目標位置,Move 方法以 setTimeout 逐步逼近目標,GetStep 則計算每次移動的步長。
var $$ = function (id) { return "string" == typeof id ? document.getElementById(id) : id; };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; 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 || {}); }, Set: function(index) { if (index < 0) { this.Each(function(oList, oText, i){ oList._target = this._height * i; }) } else { this.Each(function(oList, oText, i){ oList._target = (i <= index) ? this._height_min * i : this._height_min * (i - 1) + this._height_max; }) } 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); }};
使用時只需建立實例並傳入容器 id、總高度、標籤名稱與最大展開高度即可:var gv = new GlideView("idGlideView", 314, "div", 280, "");。透過滑鼠移入觸發 Set,再交由 Move 以緩動方式平滑過渡,即可實現縱向滑動展開的視覺效果。希望本文所述的做法能對大家的 JavaScript 程式設計有所幫助。