Home >Web Front-end >JS Tutorial >jQuery mask layer implementation method
This article mainly introduces jQuery mask layer examples in detail, which has certain reference value. Interested friends can refer to it. I hope it can help everyone.
1.1 Background translucency The mask layer style
requires a black (or other) background 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
Mask The purpose of the mask is just 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; }
Related recommendations:
How to prevent the page from scrolling after the mask layer
What is the js special effects mask layer
The above is the detailed content of jQuery mask layer implementation method. For more information, please follow other related articles on the PHP Chinese website!