This article explains the jquery mask layer with examples, including different styles of mask layer implementation, mask implementation of mask layer, etc. It is shared with everyone for your reference. The specific content is as follows
1. jQuery implements different styles of mask layers
1.1 Background translucent mask layer style
A black (or other) background is required and must be set to absolute positioning. The following is the css style used in the project:
/* 半透明的遮罩层 */ #overlay { background: #000; filter: alpha(opacity=50); /* IE的透明度 */ opacity: 0.5; /* 透明度 */ display: none; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; z-index: 100; /* 此处的图层要大于页面 */ display:none; }
1.2 jQuery to implement mask
/* 显示遮罩层 */ function showOverlay() { $("#overlay").height(pageHeight()); $("#overlay").width(pageWidth()); // fadeTo第一个参数为速度,第二个为透明度 // 多重方式控制透明度,保证兼容性,但也带来修改麻烦的问题 $("#overlay").fadeTo(200, 0.5); } /* 隐藏覆盖层 */ function hideOverlay() { $("#overlay").fadeOut(200); } /* 当前页面高度 */ function pageHeight() { return document.body.scrollHeight; } /* 当前页面宽度 */ function pageWidth() { return document.body.scrollWidth; }
1.3 Prompt Box
The purpose of the mask is to make it impossible to operate the content and highlight the prompt box. The prompt box can be made by referring to the above. The z-index can be higher than the mask layer. The main question is how to control the prompt box to be centered in the browser.
/* 定位到页面中心 */ function adjust(id) { var w = $(id).width(); var h = $(id).height(); var t = scrollY() + (windowHeight()/2) - (h/2); if(t < 0) t = 0; var l = scrollX() + (windowWidth()/2) - (w/2); if(l < 0) l = 0; $(id).css({left: l+'px', top: t+'px'}); } //浏览器视口的高度 function windowHeight() { var de = document.documentElement; return self.innerHeight || (de && de.clientHeight) || document.body.clientHeight; } //浏览器视口的宽度 function windowWidth() { var de = document.documentElement; return self.innerWidth || (de && de.clientWidth) || document.body.clientWidth } /* 浏览器垂直滚动位置 */ function scrollY() { var de = document.documentElement; return self.pageYOffset || (de && de.scrollTop) || document.body.scrollTop; } /* 浏览器水平滚动位置 */ function scrollX() { var de = document.documentElement; return self.pageXOffset || (de && de.scrollLeft) || document.body.scrollLeft; }
2. Jquery super simple mask layer implementation code
In development, in order to avoid secondary submission, the use of mask layers is becoming more and more common
After reading a lot of code, let me share with you what I think is the simplest way to implement the mask layer:
1. The style is set as follows:
CSS code:
<style type="text/css"> .mask { position: absolute; top: 0px; filter: alpha(opacity=60); background-color: #777; z-index: 1002; left: 0px; opacity:0.5; -moz-opacity:0.5; } </style>
Among them: opacity:0.5; is suitable for IE, -moz-opacit:0.5; is suitable for Firefox; you only need to add them, and it can be used in both Firefox and IE.
2. Specify the height and width of the layer
js code
<pre class="html" name="code"><script type="text/javascript"> //兼容火狐、IE8 //显示遮罩层 function showMask(){ $("#mask").css("height",$(document).height()); $("#mask").css("width",$(document).width()); $("#mask").show(); } //隐藏遮罩层 function hideMask(){ $("#mask").hide(); } </script>
3. Add the following code to , and then you can see the effect
html code
<div id="mask" class="mask"></div> <a href="javascript:;" onclick="showMask()" >点我显示遮罩层</a><br />
4. How to use
After ajax submits the form, add the showMask method, and after the data is returned, add hideMask()
Those who need it can add some prompt information on the mask layer according to their own needs
3. Release a JQuery mask layer implementation (mask)
Those who have used ExtJs may know that a lot of UI elements are integrated in ExtJs, which is very convenient for us to use. There are two methods, mask() and unmask(), which add a mask layer and a prompt message to the specified element to increase customer experience. When I was working on a project recently, I found that sometimes in order to use these two methods, I needed to introduce a relatively "large" Extjs, which I felt was a bit uneconomical, so I used jquery to implement a relatively simple mask and unmask method to achieve this effect. Everyone knows that jquery is an excellent javascript framework. It is not only small in size but also easy to use. Now I am gradually replacing all the codes or components implemented using Extjs in the system with Jquery. Okay, without further ado, let me introduce my code. These codes are modified based on the documentMask implemented by a friend on the Internet, making it more flexible and convenient to use.
(function(){ $.extend($.fn,{ mask: function(msg,maskDivClass){ this.unmask(); // 参数 var op = { opacity: 0.8, z: 10000, bgcolor: '#ccc' }; var original=$(document.body); var position={top:0,left:0}; if(this[0] && this[0]!==window.document){ original=this; position=original.position(); } // 创建一个 Mask 层,追加到对象中 var maskDiv=$('<div class="maskdivgen"> </div>'); maskDiv.appendTo(original); var maskWidth=original.outerWidth(); if(!maskWidth){ maskWidth=original.width(); } var maskHeight=original.outerHeight(); if(!maskHeight){ maskHeight=original.height(); } maskDiv.css({ position: 'absolute', top: position.top, left: position.left, 'z-index': op.z, width: maskWidth, height:maskHeight, 'background-color': op.bgcolor, opacity: 0 }); if(maskDivClass){ maskDiv.addClass(maskDivClass); } if(msg){ var msgDiv=$('<div style="position:absolute;border:#6593cf 1px solid; padding:2px;background:#ccca"><div style="line-height:24px;border:#a3bad9 1px solid;background:white;padding:2px 10px 2px 10px">'+msg+'</div></div>'); msgDiv.appendTo(maskDiv); var widthspace=(maskDiv.width()-msgDiv.width()); var heightspace=(maskDiv.height()-msgDiv.height()); msgDiv.css({ cursor:'wait', top:(heightspace/2-2), left:(widthspace/2-2) }); } maskDiv.fadeIn('fast', function(){ // 淡入淡出效果 $(this).fadeTo('slow', op.opacity); }) return maskDiv; }, unmask: function(){ var original=$(document.body); if(this[0] && this[0]!==window.document){ original=$(this[0]); } original.find("> div.maskdivgen").fadeOut('slow',0,function(){ $(this).remove(); }); } }); })();
The following is the usage example code for reference
<html> <head> <style> body{ font-size:12px; } </style> <script src="jquery-1.3.2.js" type="text/javascript"></script> <script type="text/javascript"> (function(){ $.extend($.fn,{ mask: function(msg,maskDivClass){ this.unmask(); // 参数 var op = { opacity: 0.8, z: 10000, bgcolor: '#ccc' }; var original=$(document.body); var position={top:0,left:0}; if(this[0] && this[0]!==window.document){ original=this; position=original.position(); } // 创建一个 Mask 层,追加到对象中 var maskDiv=$('<div class="maskdivgen"> </div>'); maskDiv.appendTo(original); var maskWidth=original.outerWidth(); if(!maskWidth){ maskWidth=original.width(); } var maskHeight=original.outerHeight(); if(!maskHeight){ maskHeight=original.height(); } maskDiv.css({ position: 'absolute', top: position.top, left: position.left, 'z-index': op.z, width: maskWidth, height:maskHeight, 'background-color': op.bgcolor, opacity: 0 }); if(maskDivClass){ maskDiv.addClass(maskDivClass); } if(msg){ var msgDiv=$('<div style="position:absolute;border:#6593cf 1px solid; padding:2px;background:#ccca"><div style="line-height:24px;border:#a3bad9 1px solid;background:white;padding:2px 10px 2px 10px">'+msg+'</div></div>'); msgDiv.appendTo(maskDiv); var widthspace=(maskDiv.width()-msgDiv.width()); var heightspace=(maskDiv.height()-msgDiv.height()); msgDiv.css({ cursor:'wait', top:(heightspace/2-2), left:(widthspace/2-2) }); } maskDiv.fadeIn('fast', function(){ // 淡入淡出效果 $(this).fadeTo('slow', op.opacity); }) return maskDiv; }, unmask: function(){ var original=$(document.body); if(this[0] && this[0]!==window.document){ original=$(this[0]); } original.find("> div.maskdivgen").fadeOut('slow',0,function(){ $(this).remove(); }); } }); })(); </script> </head> <body style="width:100%"> 测试 <div id="test" style="width:200px;height:100px; border:black 1px solid;"> </div> <a href="#" onclick="$('#test').mask('DIV层遮罩')">div遮罩</a> <a href="#" onclick="$('#test').unmask()">关闭div遮罩</a> <a href="#" onclick="$(document).mask('全屏遮罩').click(function(){$(document).unmask()})">全部遮罩</a> </body> </html>
The above is the complete introduction to the implementation of the mask layer in jquery. I hope it will be helpful to everyone's learning.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.