JQuery插件:遮罩+數據加載中。。。(特點:遮你想遮,罩你想罩)


在很多項目中都會涉及到數據加載。數據加載有時可能會是2-3秒,為了給一個友好的提示,一般都會給一個【數據加載中。。。】的提示。今天就做了一個這樣的提示框。

先去jQuery官網看看怎么寫jQuery插件,然后就開始寫了。寫下來這么一個插件,稍作優化,就在項目中使用了。下面貼的是我寫這個插件時的測試圖:

這張圖模擬數據加載前提示框的展示,這個表格是一個寫在頁面上的。藍色的底紋就是遮罩層。

復制代碼
(function($){
    $.fn.extend({
        /**
         * 打開遮罩,並顯示一段文字。
         * @param  {String} msg    [顯示的文字]
         * @param  {String} imgsrc [圖片的位置]
         * @return {void}       
         */
        openMask:function(msg, imgsrc){
//            var loadDiv=$("body").find("._mask_loadDiv");
            var loadDiv=this.find("._mask_loadDiv");
            if(!loadDiv || !loadDiv[0]){    // add Mask 
                var loadDiv=$("<div class='_mask_loadDiv' style='position:absolute; z-index:99999; height:40px; background:#FFF; border:1px solid #ACE'></div>");
                
                if(!imgsrc){    // 指定默認的圖片
                    var scripts=document.getElementsByTagName("script");
                    for(var i=0; i<scripts.length; i++){
                        if(scripts[i].src.indexOf("mask")!=-1){
                            var scriptSrc=scripts[i].src;
                            var uri=scriptSrc.substring(0,scriptSrc.lastIndexOf("/"));
                            imgsrc=uri+"/images/mask_loading.gif";
                        }
                    }
                }

                var contentDiv=$("<div class='_mask_content' style='position:relative;text-align:center;' >");
                var fontsize=12;
                //loadDiv的寬度= msg的寬度+img的寬度
                var loadDiv_width=msg.length*fontsize+16+3;
                contentDiv.css("width",loadDiv_width);
                loadDiv.css("width",loadDiv_width);
                if(imgsrc){
                    contentDiv.append("<img src='"+imgsrc+"' alt='"+msg+"' style='width:16px; height:16px'>")
                            .append("<span style='font-size:"+fontsize+"px; margin-left:2px; vertical-align:text-top'>"+msg+"</span>");
                }
                this.append(loadDiv.append(contentDiv));
            //    $("body").append(loadDiv.append(contentDiv));
                /*
                loadDiv[0].style.top=this[0].offsetTop+(this[0].offsetHeight-loadDiv[0].offsetHeight)/2;
                loadDiv[0].style.left=this[0].offsetLeft+(this[0].offsetWidth-loadDiv[0].offsetWidth)/2;
                loadDiv[0].style.paddingTop=(loadDiv[0].offsetHeight-contentDiv[0].offsetHeight)/2;
                */
                loadDiv.css("top",this[0].offsetTop+(this[0].offsetHeight-loadDiv[0].offsetHeight)/2);
                loadDiv.css("left",this[0].offsetLeft+(this[0].offsetWidth-loadDiv[0].offsetWidth)/2);
                loadDiv.css("padding-top",(loadDiv[0].offsetHeight-contentDiv[0].offsetHeight)/2);
            }
            loadDiv.css("z-index",99999).css("display","block");
            return this;
        },
        closeMask:function(){
    //        var loadDiv=$("body").find("._mask_loadDiv");
            var loadDiv=this.find("._mask_loadDiv");
            if(loadDiv)
                loadDiv.css("display","none").css("z-index",-99999);
            return this;
        }
    });
})(jQuery);


/*



// 這個是遮罩層里信息展示框,這個會添加到 <body> 或者 target 元素中
<div class="_mask_loadDiv">
    <div class="_mask_content">
        <img src='imgsrc' alt='msg' >
        <span>msg</span>
    </div>
</div>


//這個是目標,要在它上顯示遮罩層
<div id="target">

</div>

// 只需要下面的代碼:
$("#target").openMask("數據加載中。。。");
// 隱藏對話框,只需要:
$("#target").closeMask();



*/
復制代碼

 

因為涉及到的CSS並不多,就沒有遵循HTML、JS、CSS分離的原則,而是將CSS都在這個JS 中寫了。

測試頁面代碼:

復制代碼
<html>
    <head>
        <script type="text/javascript" src="./jquery-mask/jquery-1.7.2.min.js"></script>
        <script type="text/javascript" src="./jquery-mask/jquery.mask.js"></script>
        <script type="text/javascript">
            var helloDiv;
            $(function(){
                var tbl=$("#tableContent");
                for(var i=0; i< 16;i++){
                    tbl.append('<tr><td width="25%">hello</td><td width="25%">world</td><td width="25%">jquery</td><td width="22%">mask</td></tr>');
                }
                helloDiv=$("#hello");
                helloDiv.openMask('數據加載中。。。');

            });

            function openMask(){
                helloDiv.openMask("數據加載中。。。。");
            }

            function closeMask(){
                helloDiv.closeMask("數據加載中。。。。");
            }
        </script>
        <body>
            <div id="hello" style="width:300px; height:400px; ">
            <table border="1" width="100%" id="tableContent">
            </table>    
            </div>
            <input value="open" type="button" onclick="openMask();"><br>
            <input type="button" value="close" onclick="closeMask();">
        </body>
    </head>
</html>
復制代碼

 

 寫這個插件的重點是:計算提示框的位置問題(top, left)、提示框層次問題(z-index)。

要理解這些屬性可以看看:CSS位置和布局的相關博客。

源碼參見:http://files.cnblogs.com/f1194361820/jquery-mask.zip


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM