JQuery外掛程式:遮罩+資料載入中。。。(特點:遮你想遮,罩你想罩)

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   使用   java   ar   

在很多項目中都會涉及到資料載入。資料載入有時可能會是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; background-color:#ACE;">            <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>
test.html

 

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

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

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

 

JQuery外掛程式:遮罩+資料載入中。。。(特點:遮你想遮,罩你想罩)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.