Jquery遮罩外掛程式,想罩哪就罩哪!

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   color   ar   os   java   

原文:Jquery遮罩外掛程式,想罩哪就罩哪!

一  前言

    在項目開發時發現沒有一個用起來 爽一點的遮罩外掛程式,看起來覺得不難 好吧那就利用空閑時間,自己折騰一個吧,也好把jquery再溫習一下,

需要的功能如下

  1 可以全屏遮 用於提交資料時

  2  局部遮,用於重複提交,只遮提交按鈕,此功能當時在CSDN的登入中看到過,當時覺得還不錯

      3  遮罩上的提示文字可自己配置,因為操作的業務情境不一樣,提示的資訊肯定也會不一樣

      4  遮罩圖片可配置,

      5 資訊提示層大小可配置

大致功能就如上幾點,然後就是折騰js了,蹭蹭蹭~~~~ 然後就出來了~ 先上效果 

 

全域遮罩效果 

 

局部遮罩

 

 

二 源碼相關

需要瞭解的知識點大致如下

    1 z-index屬性

    2 position屬性

    3 瀏覽器視窗與document 高寬的計算

    4 jquery 外掛程式格式

    5 css 濾鏡效果

 

不多說了上代碼!為了減少引入的檔案索性將css直接寫在js中了

/***************************Desc:提交操作時遮罩*Argument:type=0 全屏遮 1局部遮*Author:Zery-Zhang*Date:2014-09-18*Version:1.0.0**************************/; (function ($) {     $.fn.jqLoading =function(option) {        var defaultVal = {            backgroudColor: "#ECECEC",//背景色            backgroundImage: "../image/loading.gif",//背景圖片            text: "玩命載入中....",//文字             width: 150,//寬度            height: 60,//高度            type:0 //0全部遮,1 局部遮                    };        var opt = $.extend({}, defaultVal, option);        if (opt.type == 0) {            //全屏遮            openLayer();        } else {            //局部遮(當前對象應為需要被遮擋的對象)            openPartialLayer(this);        }                //銷毀對象        if (option === "destroy") {            closeLayer();        }                //設定背景層高        function height () {            var scrollHeight, offsetHeight;            // handle IE 6            if ($.browser.msie && $.browser.version < 7) {                scrollHeight = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);                offsetHeight = Math.max(document.documentElement.offsetHeight, document.body.offsetHeight);                if (scrollHeight < offsetHeight) {                    return $(window).height();                } else {                    return scrollHeight;                }                // handle "good" browsers            }            else if ($.browser.msie && $.browser.version == 8) {                return $(document).height() - 4;            }            else {                return $(document).height();            }        };                //設定背景層寬        function width() {            var scrollWidth, offsetWidth;            // handle IE            if ($.browser.msie) {                scrollWidth = Math.max(document.documentElement.scrollWidth, document.body.scrollWidth);                offsetWidth = Math.max(document.documentElement.offsetWidth, document.body.offsetWidth);                if (scrollWidth < offsetWidth) {                    return $(window).width();                } else {                    return scrollWidth;                }                // handle "good" browsers            }            else {                return $(document).width();            }        };                /*==========全部遮罩=========*/        function openLayer() {            //背景遮罩層            var layer = $("<div id=‘layer‘></div>");            layer.css({                zIndex:9998,                position: "absolute",                height: height() + "px",                width: width() + "px",                background: "black",                top: 0,                left: 0,                filter: "alpha(opacity=30)",                opacity: 0.3                          });                      //圖片及文字層            var content = $("<div id=‘content‘></div>");            content.css({                textAlign: "left",                position:"absolute",                zIndex: 9999,                height: opt.height + "px",                width: opt.width + "px",                top: "50%",                left: "50%",                verticalAlign: "middle",                background: opt.backgroudColor,                borderRadius:"8px",                fontSize:"13px"            });                        content.append("<img style=‘vertical-align:middle;margin:"+(opt.height/4)+"px; 0 0 5px;margin-right:5px;‘ src=‘" + opt.backgroundImage + "‘ /><span style=‘text-align:center; vertical-align:middle;‘>" + opt.text + "</span>");            $("body").append(layer).append(content);            var top = content.css("top").replace(‘px‘,‘‘);            var left = content.css("left").replace(‘px‘,‘‘);            content.css("top",(parseFloat(top)-opt.height/2)).css("left",(parseFloat(left)-opt.width/2));                       return this;        }        //銷毀對象        function closeLayer() {            $("#layer,#content,#partialLayer").remove();            return this;        }                /*==========局部遮罩=========*/        function openPartialLayer(obj) {                     var eheight = $(obj).css("height");//元素帶px的高寬度            var ewidth = $(obj).css("width");            var top = $(obj).offset().top; // 元素在文檔中位置 捲軸不影響            var left = $(obj).offset().left;            var layer = $("<div id=‘partialLayer‘></div>");            layer.css({                style: ‘z-index:9998‘,                position: "absolute",                height: eheight,                width: ewidth,                background: "black",                top: top,                left: left,                filter: "alpha(opacity=60)",                opacity: 0.6,                borderRadius:"3px",                dispaly: "block"            });            $("body").append(layer);            return this;        }    };    })(jQuery)

 

外掛程式用法如下

    <input  type="button" id="btnOpen" value="開啟" />    <input type="button" id="btnClose" value="關閉" />    <script type="text/javascript">        $(function () {            $("#btnOpen").on("click", function () {                //全域                //$(this).jqLoading();                //局部                $(this).jqLoading({ type: 1 });                         });            $("#btnClose").on("click", function () {                $(this).jqLoading("destroy");                          });        })        </script>

 

 

三  總結

  以上代碼只為自己練習所用,如果碰巧能被人所用,那純屬巧合~

我個人很享受,由構思到成品 這一過程,並一直努力將自己的一些想法,用代碼慢慢實現。

 

:http://files.cnblogs.com/zery/jqLoding.rar

 

預告: 表動態增加 外掛程式 

  下一篇將介紹自己在實際項目中用到的表格動態增加,刪除,取資料,綁定資料 ,資料校正等 先上個圖

  

 

如果您覺得本文有給您帶來一點收穫,不妨點個推薦,為我的付出支援一下,謝謝~

如果希望在技術的道路上能有更多的朋友,那就關注下我吧,讓我們一起在技術的路上奔跑

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.