On the Web page often encountered the need to wait for a long time, such as export reports. In order to prevent users to click on other actions or multiple clicks of the same function, you need to use mask layer to cover the page or operation area, prevent the user to do the next step, but also improve the interface, let the user know that the operation is executing.
$.fn.extend ({/** * adds mask layer to element * @param message {String} [optional] mask layer display content */mask:function (message) {var $target = this,fixed = False,targetstatic = True;if (Object.prototype.toString.call (message)!== ' [Object String] ' | |!message) {// If the message is empty or not a string, the default message is indicated. Message = ' Please wait ... ';} if ($target. length = = 0) {$target = $ (' body '),} else {if ($target. length > 1) {$target = $target. First ();} if ($target [0] = = = Window | | $target [0] = = = document) {$target = $ (' body ');}} if ($target [0] = = = document.body) {fixed = true;} If the target element already has a matte layer, get the matte layer var old = $target. Data (' Rhui.mask '), if (old) {old. $content. HTML (message); Center ($target, old.$ content, fixed); return;} If the masked element is static, change the element to Relativeif ($target. css (' position ') = = = ' static ') {targetstatic = true; $target. css (' position ') , ' relative ');} var $content, $overlay; if (fixed) {$overlay = $ (' <div class= "Rhui-mask" style= "position:fixed;" ></div> '); $content = $ (' <div class= "rhui-mask-content" style= "position:fixed;" > ' + message + ' </div> ');} else {$overlay = $ (' <div class= "Rhui-mask" ></div> ") $content = $ (' <div class= ' rhui-mask-content" > ' + Message + ' </div> ');} $overlay. AppendTo ($target); $content. AppendTo ($target);//Display Mask layer $overlay.show (); $content. Show ();//Let the Matte Layer Center Center ($ Target, $content, fixed);//Add mask layer information to $target$target.data (' Rhui.mask ', {fixed:fixed, $overlay: $overlay, $content: $ Content,targetstatic:targetstatic});/** * To center the content of the mask layer * @param $target obscured elements * @param $content masking layer content elements * @param fix Whether the ed matte layer is fixed displays */function Center ($target, $content, fixed) {var $window, height = $content. Outerheight (True), width = $con Tent.outerwidth (True); if (fixed) {//If the mask layer is fixed, let the mask layer be centered in window $window = $ (window); $content. css ({left: ($window. Width () -width)/2,top: ($window. Height ()-height)/2});} else {//Let Mask Layer Center $content.css in $target ({left: ($target. Width ()-width)/2,top: ($target. Height ()-height)/2});}}},/** * Cancels mask Layer */unmask:function () {var $target; if (this.length = = = 0) {$target = $ (' body ');} ELSE {$target = This.first (); if ($target [0] = = = Window | | $target [0] = = = document) {$target = $ (' body ');}} var data = $target. Data (' Rhui.mask '); if (!data) {return;} Restore the Position property of the target element if (data.targetstatic) {$target. css (' position ', ' static ');} Data. $overlay. Remove ();d ata. $content. Remove (); $target. Removedata (' Rhui.mask ');}});
Plug-in styles are controlled by the Rhui-mask and Rhui-mask-content classes, Rhui-mask is the mask layer style, and rhui-mask-content is the hint content style of the matte layer.
/* Mask layer style */.rhui-mask {Position:absolute;top:0;right:0;bottom:0;left:0;z-index:9000;display:block;margin:0;paddin G:0;border-style:none;background-color: #777; Opacity:0.3;zoom:1;filter:alpha (opacity=30);} /* Mask layer display content style */.rhui-mask-content {position:absolute;z-index:9999;display:block;margin:0;padding:15px 20px;border: 2PX solid RGB (109, 157, 215); Background-color: #fff; color:blue;letter-spacing:2px;font-weight:bold;font-size:15px; cursor:wait;}
Effect
Page Call complete code
<! DOCTYPE html>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
jquery masking Layer Plug-in