DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JS 非圖片動態loading效果實現代碼
JS 非圖片動態loading效果實現代碼
編輯:關於JavaScript     
代碼如下:
首先實現該功能的js對象LoadingMsg:
復制代碼 代碼如下:
var Class = {
create: function() {
return function() { this.init.apply(this,arguments); }
}
}
var LoadingMsg = Class.create();
LoadingMsg.prototype = {
init: function(spanId, spanMsg) {
this.intervalID = -10000;
this.spanId = spanId;
this.spanMsg = spanMsg;
this.timespan = 1000;
this.pointNum = 3;
this.initPointMsg = "...";
},
Loading: function() {
var maxLength = this.spanMsg.length + this.pointNum;
var currentSpanMsg = document.getElementById(this.spanId).innerHTML;
if (currentSpanMsg.length < maxLength) {
document.getElementById(this.spanId).innerHTML += ".";
}
else {
document.getElementById(this.spanId).innerHTML = this.spanMsg;
}
},
Start: function() {
document.getElementById(this.spanId).innerHTML = this.spanMsg + this.initPointMsg;
var callObj = this;
this.intervalID = setInterval(function() { callObj.Loading(); }, this.timespan);
},
End: function() {
document.getElementById(this.spanId).innerHTML = "";
clearInterval(this.intervalID);
}
}

關鍵點:
如果把
復制代碼 代碼如下:
var callObj = this;
this.intervalID = setInterval(function() { callObj.Loading(); }, this.timespan);

寫成:
復制代碼 代碼如下:
this.intervalID = setInterval(this.Loading, this.timespan);

在執行Loading方法時則會報找不到this.spanMsg的錯誤。
因為在setInterval裡的第一個參數裡的this是windows對象,而不是LoadingMsg對象。windows.setInterval嘛。
應用該方法:
復制代碼 代碼如下:
<body>
<input type="button" value="Start" onclick="javascript:StartLoading();" />
<span id="spanId" style="color:Red"></span>
<br />
<input type="button" value="End" onclick="javascript:EndLoading();" />
<br /><br />
<script type="text/javascript">
var loadingMsgObj = new LoadingMsg("spanId","loading");
function StartLoading() {
loadingMsgObj.Start();
}
function EndLoading() {
loadingMsgObj.End();
}
</script>
</body>

來源於prototype.js裡經典創建Js對象的
復制代碼 代碼如下:
var Class = {
create: function() {
return function() { this.init.apply(this,arguments); }
}
}
var LoadingMsg = Class.create();

在Class.create()的時候做了2件事,1個是創建了LoadingMsg的對象,即var LoadingMsg = function() {};
另外一件事就是調用LoadingMsg的init方法,初始化LoadingMsg裡的靜態私有變量,相當於c#裡的構造函數作用。
如果你覺得這很裝逼的話,如果你更喜歡簡單樸實的女孩子的話,也可以改寫LoadingMsg對象:
復制代碼 代碼如下:
var LoadingMsg = function() { };
LoadingMsg.prototype = {
init: function(spanId, spanMsg) {
this.intervalID = -10000;
this.spanId = spanId;
this.spanMsg = spanMsg;
this.timespan = 1000;
this.pointNum = 3;
this.initPointMsg = "...";
},
Loading: function() {
var maxLength = this.spanMsg.length + this.pointNum;
var currentSpanMsg = document.getElementById(this.spanId).innerHTML;
if (currentSpanMsg.length < maxLength) {
document.getElementById(this.spanId).innerHTML += ".";
}
else {
document.getElementById(this.spanId).innerHTML = this.spanMsg;
}
},
Start: function(spanId, spanMsg) {
this.init(spanId, spanMsg);
document.getElementById(this.spanId).innerHTML = this.spanMsg + this.initPointMsg;
var callObj = this;
this.intervalID = setInterval(function() { callObj.Loading(); }, this.timespan);
},
End: function() {
document.getElementById(this.spanId).innerHTML = "";
clearInterval(this.intervalID);
}
}

所不同的是把init的過程調到Start時執行,因此調用的時候也就變成了
復制代碼 代碼如下:
   var loadingMsgObj = new LoadingMsg();
function StartLoading() {
  loadingMsgObj.Start("spanId", "loading");
}

從面向對象的習慣上來說,我個人還是傾向第一種寫法,在實例化對象的時候即傳入參數,而不是執行對象方法的時候傳入參數。
另外關於setInterval方法傳參數時,如果參數是簡單的string,可以
setInterval("DisplayXYZ('xyz')",1000);
如果參數是對象,
則可以setInterval(function(){DisplayXYZ(obj);},1000);
LoadingMsg還是主要用在Ajax中,應用於執行時間可能較長的場景,在發送請求後loadingMsgObj.Start(),在成功獲取響應時loadingMsgObj.End()。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved