利用 Microsoft AJAX Library 的客戶端 PageRequestManager 事件:beginRequest(回發前)與 pageLoaded(回發後)來觸發動畫。
Type.registerNamespace("ScriptLibrary");
ScriptLibrary.BorderAnimation = function(startColor, endColor, duration) {
this._startColor = startColor;
this._endColor = endColor;
this._duration = duration;
}
ScriptLibrary.BorderAnimation.prototype = {
animatePanel: function(panelElement) {
var s = panelElement.style;
s.borderColor = this._startColor;
window.setTimeout(function() { s.borderColor = this._endColor; }, this._duration);
}
}
ScriptLibrary.BorderAnimation.registerClass('ScriptLibrary.BorderAnimation', null);
var panelUpdatedAnimation = new ScriptLibrary.BorderAnimation('blue', 'gray', 1000);
var postbackElement;
Sys.Application.add_load(ApplicationLoadHandler);
function ApplicationLoadHandler() {
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);
}
function beginRequest(sender, args) {
postbackElement = args.get_postBackElement();
}
function pageLoaded(sender, args) {
var updatedPanels = args.get_panelsUpdated();
if (typeof(postbackElement) === "undefined") return;
if (postbackElement.id.toLowerCase().indexOf('animate') > -1) {
for (var i=0; i < updatedPanels.length; i++)
panelUpdatedAnimation.animatePanel(updatedPanels[i]);
}
}
if(typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
beginRequest、pageLoaded 事件。pageLoaded 遍歷更新的 Panel 執行邊框顏色漸變動畫(藍→灰,1 秒)。animatePanel 使用 setTimeout 實現簡單色彩轉換。可擴展為更豐富動畫(淡入淡出、滑動等),核心在於攔截部分更新面板的生命週期事件。