您好,欢迎访问一九零五行业门户网

jQuery遮罩层实现方法实例详解(附遮罩层插件)_jquery

本文实例分析了jquery遮罩层实现方法。分享给大家供大家参考,具体如下:
1 背景半透明遮罩层样式
需要一个黑色(当然也可以其他)背景,且须设置为绝对定位,以下是项目中用到的css样式:
/* 半透明的遮罩层 */#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;}
2 jquery实现遮罩
/* 显示遮罩层 */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;}
3 提示框
遮罩的目的无非让人无法操作内容,突出提示框,而提示框可参考上面的制作,z-index比遮罩层更高便可。主要问题是,如何控制提示框在浏览器居中。
/* 定位到页面中心 */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;}
补充:
jquery简单遮罩层插件:
jquery代码:
(function ($) { $.fn.showmask = function (options) { var defaults = { top: 150, left: 200 } var options = $.extend(defaults, options); $(html).append('
操作正在进行中,请耐心等待......
') _this_ = $(#ui-mask); _this_.height($(document).height()) _this_.show(); }; $.fn.hidemask = function (options) { _this_ = $(#ui-mask); _this_.remove(); };})(jquery);
css样式:
#ui-mask{ background-color: #666; position: absolute; z-index: 9999; left: 0; top: 0; display: none; width: 100%; height: 100%; opacity: 0.5; filter: alpha(opacity=50); -moz-opacity: 0.5;}#ui-mask-div img{ width: 50px; height: 50px; float: left;}#ui-mask-div span{ height: 50px; line-height: 50px; display: block; float: left; color: red; font-weight: bold; margin-left: 5px;}
使用方法:
function btn_save(){ $(this).showmask(); $.post('url',null,function(d,s){ $(this).hidemask(); });}
希望本文所述对大家jquery程序设计有所帮助。
其它类似信息

推荐信息