jQuery弹出遮罩层实例程序代码

特点:代码量少,简单易用。没有格式限制,随意的代码填充。舍弃了在ie6/7/8下的完全兼容(背景不透明),仅保证信息内容可正常阅读。

缺点:考虑到在触屏手机上的效果,未添加对scroll事件的响应,否则有可能永远看不到弹出的内容(网页的缩放会影响scrollTop值)。

相关插件要么功能太强大体积也大,要么是固定的格式需要预设标题、内容、关闭按钮等。非固定样式又非常灵活的一时没找到,就把手头的一个有bug的原生javascript弹出层改动了一下,借助jQuery绕开了麻烦的兼容性问题,正好如今每个项目都用了jQuery,最终效果凑合着用还是可以的。更好的实现方法和能兼容手机的方法还望有高手多多指点,发现有问题也请多多批评指正。

HTML

 代码如下 复制代码

<div id="background"></div>
<div id="content">
    这里可以是随意的标签、随意的内容 - mming
</div>
<input id="btn" type="button" value="点我" />CSS

#background {position:absolute; z-index:998; top:0px; left:0px; background:rgb(50,50,50);background:rgba(0,0,0,0.5); display:none;}
#content {position:absolute; width:500px; z-index:999; padding:10px; background:#fff; border-radius:5px; display:none;}jQuery

$(function() {
    function conPosition() {
        var oBackground = document.getElementById("background");
        var dw = $(document).width();
        var dh = $(document).height();
        oBackground.style.width = dw+'px';
        oBackground.style.height = dh+'px';
        var oContent = document.getElementById("content");
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        var l = (document.documentElement.clientWidth - oContent.offsetWidth) / 2;
        var t = ((document.documentElement.clientHeight - oContent.offsetHeight) / 2) + scrollTop;
        oContent.style.left = l + 'px';
        oContent.style.top = t + 'px';
    }
    $("#btn").click(function() {
        $("#background, #content").show();
        conPosition();
    });
    $("#background").click(function() {$("#background, #content").hide();});
    //点击黑色背景隐藏弹出层,当然可以绑定在任意一个按钮上
    $(window).resize(function() {conPosition();});
    //$(window).scroll(function() {conPosition();});
    //开启内容跟随垂直滚动条(水平滚动条需要处理的问题更多,暂时没有考虑)
});

完整实例

 代码如下 复制代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>简单的jquery弹出遮罩层DEMO - mming 前端博客</title>
<meta name="keywords" content="jquery弹出层,jquery遮罩层" />
<meta name="description" content="特点:代码量少,简单易用。没有格式限制,随意的代码填充。舍弃了在ie6/7/8下的完全兼容,仅保证信息内容可正常显示。" />

<style type="text/css">
* {margin:0; padding:0;}
body {font-size:12px; color:#222; font-family:Verdana,Arial,Helvetica,sans-serif; background:#f0f0f0;}
.clearfix:after {content: "."; display: block; height: 0; clear: both; visibility: hidden;}
.clearfix {zoom:1;}
ul,li {list-style:none;}
img {border:0;}

.wrapper {width:600px; margin:0 auto; padding-bottom:50px;}
.ad {width:468px; margin:10px auto 0;}
.ad li {padding-top:5px;}

h1 {height:50px; line-height:50px; font-size:22px; font-weight:normal; font-family:"Microsoft YaHei",SimHei;}

.download {display:inline-block; height:34px; line-height:34px; padding:0 10px; background:#333; font-size:14px; font-weight:bold; text-align:center; color:#fff; border:1px solid #999; margin-top:10px;}
.fenxiang {height:16px; padding:20px 0 10px; margin-bottom:10px;}

.shuoming {margin-top:20px; border:1px solid #ccc; padding-bottom:10px;}
.shuoming dt {height:30px; line-height:30px; font-weight:bold; text-indent:10px;}
.shuoming dd {line-height:20px; padding:5px 20px;}

.tongji {display:none;}

.wrapper {width:600px;}
#background {position:absolute; z-index:998; top:0px; left:0px; background:rgb(50,50,50);background:rgba(0,0,0,0.5); display:none;}
#content {position:absolute; width:500px; z-index:999; padding:10px; background:#fff; border-radius:5px; display:none;}
</style>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
 function conPosition() {
  var oBackground = document.getElementById("background");
  var dw = $(document).width();
  var dh = $(document).height();
  oBackground.style.width = dw+'px';
  oBackground.style.height = dh+'px';

  var oContent = document.getElementById("content");
  var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

  var l = (document.documentElement.clientWidth - oContent.offsetWidth) / 2;
  var t = ((document.documentElement.clientHeight - oContent.offsetHeight) / 2) + scrollTop;

  oContent.style.left = l + 'px';
  oContent.style.top = t + 'px';
 }
 
 $("#btn").click(function() {
  $("#background, #content").show();
  conPosition();
 });
 
 $("#background").click(function() {$("#background, #content").hide();});
 $(window).resize(function() {conPosition();});
 //$(window).scroll(function() {conPosition();});
});
</script>
</head>

<body>
<div id="background"></div>
<div id="content">
 这里可以是随意的标签、随意的内容 - mming
</div>

<div class="wrapper">
 <h1><a href="www.111cn.net">手机主题</a></h1>
 
 <input id="btn" type="button" value="点我" />

</div>
 
</body>
</html>

时间: 2024-08-01 23:11:41

jQuery弹出遮罩层实例程序代码的相关文章

jQuery弹出遮罩层效果完整示例_jquery

本文实例讲述了jQuery弹出遮罩层效果.分享给大家供大家参考,具体如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head&

简单的jQuery弹出遮罩层例子

特点:代码量少,简单易用.没有格式限制,随意的代码填充.舍弃了在ie6/7/8下的完全兼容(背景不透明),仅保证信息内容可正常阅读. 缺点:考虑到在触屏手机上的效果,未添加对scroll事件的响应,否则有可能永远看不到弹出的内容(网页的缩放会影响scrollTop值). 相关插件要么功能太强大体积也大,要么是固定的格式需要预设标题.内容.关闭按钮等.非固定样式又非常灵活的一时没找到,就把手头的一个有bug的原生javascript弹出层改动了一下,借助jQuery绕开了麻烦的兼容性问题,正好如今

jQuery点击按钮弹出遮罩层且内容居中特效_jquery

本文为大家分享了jQuery点击按钮弹出遮罩层且内容居中的特效,下面来看最终实现的效果: 由于是测试的程序,所以我未加关闭的按钮.一.主体程序 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>弹出居中遮罩</title> <meta name="viewport" content="width=devic

jquery弹出遮掩层效果【附实例代码】_jquery

找了个别人写的遮掩层进行改善,感觉效果还可以. 效果图: 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> &

js 弹出框插件实例与代码使用方法

js 弹出框插件实例与代码使用方法 使用方法: 1. 调用jquery库以及zxxbox插件文件,如下代码: <script type="text/网页特效" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script><script type="text/javascript" src="/study/js/jq

jQuery弹出div层过2秒自动消失_jquery

下面给大家分享一段代码关于jquery弹出div层并自动消失的实现代码,废话不多说了,具体代码如下所示:  var HuiFang={ m_tishi :null,//全局变量 判断是否存在div, //提示div 等待2秒自动关闭 Funtishi: function (content, url) { if (HuiFang.m_tishi == null) { HuiFang.m_tishi = '<div class="xiaoxikuang none" id="

jQGrid Table操作列中点击【操作】按钮弹出按钮层的实现代码_jquery

在使用JqGrid时,Table中最后一列是操作列,在操作列中每一行都一个操作按钮,该操作按钮类似下拉菜单,如下图: 在点击Table中[操作]一列时需要弹出一个Div层,该Div层中包含一堆按钮,用于对Table行进行操作,如下: 解决步骤如下: 1.首先,在colModel中的列上添加属性edittype:'select'和方法formatter:groupGrid.formatOptions,如下图: 方便复制,代码如下: {label:'操作',name: 'operations',in

jquery弹出div对话框实例代码

使用说明: myHiddenDiv表示要弹出来的整体div popup-body中的内容替换为你需要的内容 openStaticPopup();表示弹出div,锁定界面 $.closePopupLayer('myStaticPopup');表示关闭div,解锁界面 <h2></h2>弹出div的标题 openStaticPopup中的width表示弹出div的宽度 其实就是细节上的调整 实例  代码如下 复制代码 <!DOCTYPE HTML PUBLIC "-//

兼容主流浏览器的jQuery+CSS 实现遮罩层的简单代码_jquery

在页面点击"注册",出现一层有不透明度的黑色遮罩:遮罩层的上方是注册框:此时无法点击页面上除注册框外的其他元素:点击注册框上的"随便逛逛",遮罩层消失. 预览地址: http://jsfiddle.net/p2x3c7df/embedded/result/ 要点: 1.注册框始终水平.垂直居中,包括鼠标滚轮上下滚动页面.缩放页面和调整浏览器窗口大小时 主要由CSS控制,注册框的宽度和高度都已经确定( 620*420px ),首先使用position:fixed来使它