DIV CSS 佈局教程網

自製 JavaScript 載入等待圖示插件(loading.gif)
編輯:關於JavaScript     

實務上常遇到這種情境:某個業務流程分成數個階段,每個階段的資料還沒取回來之前,要先顯示一張 loading.gif 佔位;資料到了就把它收起來。與其在每個頁面重寫一遍,不如封裝成一個可重複使用的物件,全專案共用。

<button onclick="show()">show</button>
<button onclick="hide()">hide</button>

<script>
// 建立載入物件
var obj = new loadingImg();

function show(){ obj.show(); }
function hide(){ obj.hide(); }

// 載入圖示元件
function loadingImg(mySetting){
  var that = this;

  // 參數防呆:非物件就重置為空物件
  if (mySetting == "" || mySetting == undefined || typeof mySetting != "object"){
    mySetting = {};
  }

  // 用時間戳當作 DOM id,避免同頁多實例衝突
  var targetID = new Date().getTime();

  this.setting = {
    targetConater : "",                    // 掛載容器(jQuery 選擇器字串)
    imgUrl        : "../img/loading.gif",  // 圖片路徑
    imgWidth      : "32px",                // 圖片寬度
    imgClass      : "",                    // 自訂 class
    "targetID"    : targetID,
    beforeShow    : function(plugin){ },                       // 顯示前回呼
    afterShow     : function(plugin, targetID){ }              // 顯示後回呼
  };

  // 合併使用者設定
  this.setting = $.extend(this.setting, mySetting);

  this.getScreenWidth  = function(){ return document.documentElement.clientWidth; };
  this.getScreenHeight = function(){ return document.documentElement.clientHeight; };

  this.show = function(){ $("#" + that.setting.targetID).show(); };
  this.hide = function(){ $("#" + that.setting.targetID).hide(); };

  this.init = function(){
    if (typeof that.setting.beforeShow == "function"){
      that.setting.beforeShow(that);
    }

    var targetHTML = '';

    if (that.setting.targetConater != "" && this.setting.targetConater != undefined){
      // 指定容器:直接放進去
      targetHTML = '<img src="' + that.setting.imgUrl + '" class="' + that.setting.imgClass
                 + '" id="' + that.setting.targetID + '" style="display:none;">';
      $(that.setting.targetConater).html(targetHTML);
    } else {
      // 未指定容器:鋪滿整個視窗並置中
      targetHTML = '<img src="' + that.setting.imgUrl + '" class="' + that.setting.imgClass + '">';
      targetHTML = '<div id="' + that.setting.targetID + '"'
                 + ' style="display:none;position:absolute;top:50%;left:50%;'
                 + 'height:' + that.getScreenHeight() + ';width:' + that.getScreenWidth() + '">'
                 + targetHTML + '</div>';
      $("body").append(targetHTML);
    }

    // 使用者有指定寬度才套用
    if (that.setting.imgWidth != "" && that.setting.imgWidth.indexOf("px") > 0){
      $("#" + targetID).css("width", that.setting.imgWidth);
    }

    if (typeof that.setting.afterShow == "function"){
      that.setting.afterShow(that, targetID);
    }
  };

  this.init();
}
</script>

設計要點與可改進之處

這個封裝有幾個值得沿用的思路:用時間戳產生唯一 id、把可變參數集中到 setting 物件並用 $.extend 合併、提供 beforeShow / afterShow 兩個回呼讓呼叫端能插入自訂邏輯。

不過原始程式有幾個 bug 值得指出,實務上使用時建議修正:

一是 $("#" + targetID) 這兩行用了未定義的 targetID 變數,應該是 that.setting.targetID,否則寬度設定不會生效。

二是「鋪滿視窗」的做法用 top:50%; left:50% 搭配明確的寬高,並不會真正置中,反而會從中心點向右下延伸。若要置中,容器應改用 position:fixed; top:0; left:0; width:100%; height:100%,再用 flex 或 translate 讓圖片居中。

三是 position:absolute 在頁面可捲動時會隨內容滾走,遮罩效果應該用 position:fixed 並加上 z-index 才對。

封裝成物件、把可變項抽成設定、預留回呼——這些習慣本身就是值得學的部分;再把定位與作用域的小問題修掉,就是一個可放心用於正式專案的元件了。希望對你的 JavaScript 開發有所幫助。

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