js 弹窗的实现
原理:
1. 点击按钮,触发窗口显示,遮罩层显示,并设置窗口的位置
2. 为弹出的窗口绑定鼠标滚动事件和视窗改变事件
3.点击关闭按钮,弹窗消失,遮罩层消失
html 代码:
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="./css/tanchuang.css" />
<script src="./js/tanchuang.js"></script>
</head>
<body style="height:3000px;">
<input type="button" id="btn" value="触发按钮" /><br/>
<div id="login">
<div class="header">
<h3 id="move">用户登录窗口</h3>
<a href="javascript:;" class="close" id="close">X</a>
</div>
<div class="body">
<h3>购物之前必须先登录</h3>
<ul>
<li><span>用户名:</span><input type="text" name="user" /></li>
<li><span>密码:</span><input type="text" name="userPwd" /></li>
<li class="act"><input type="submit" value="登录" class="log"/> <input type="submit" value="注册" class="zhuce"/></li>
</ul>
</div>
</div>
<div class="mask" id="mask"></div>
</body>
</html>
css 代码:
* {
margin:;
padding:;
list-style-type:none;
font-family:微软雅黑;
font-size:14px;
}
a {
text-decoration:none;
color:#ccc;
}
h3 {
font-size:1.2em;
}
#btn{
position:absolute;
left:50%;
top:300px;
}
#login {
width:600px;
height:400px;
border:1px solid #ccc;
background:#fff;
/* position:fixed; */
position:absolute;
z-index:;
display:none;
}
#login .header {
height:40px;
border-bottom:1px solid #ccc;
}
#login .header h3{
float:left;
margin:10px 0px 0px 10px;
}
#login .header .close {
float:right;
margin:10px 10px 0px 0px;
font-size:1.2em;
}
#login .body {
width:300px;
margin:80px auto;
}
#login .body h3 {
margin-bottom:20px;
text-align:center;
}
#login .body ul li {
margin:20px;
}
#login .body ul li span {
display:inline-block;
width:80px;
text-align:right;
padding-right:20px;
}
#login .body ul li.act {
text-align:center;
}
.log {
background:#ff8800;
color:white;
border:none;
padding:5px 10px;
-moz-border-radius: 5px; /* Firefox */
-webkit-border-radius: 5px; /* Safari 和 Chrome */
border-radius: 5px; /* Opera 10.5+, 以及使用了IE-CSS3的IE浏览器 */
cursor:pointer;
cursor: hand;
}
.zhuce {
background:#337;
color:white;
border:none;
padding:5px 10px;
-moz-border-radius: 5px; /* Firefox */
-webkit-border-radius: 5px; /* Safari 和 Chrome */
border-radius: 5px; /* Opera 10.5+, 以及使用了IE-CSS3的IE浏览器 */
cursor:pointer;
cursor: hand;
}
.mask {
width:100%;
height:100%;
position:fixed;
left:0px;
top:0px;
background:#000;
opacity:0.3;
filter:alpha(opacity:30);
z-index:;
display:none;
}
js 代码:
window.onload = function () {
// 获取对象属性的兼容 obj.currentStyle[attr] 为ie的方式,getComputedStyle(obj,false)[attr]为谷歌的方式
function getStyle(obj,attr){
// if(obj.currentStyle){
// return obj.currentStyle[attr];
// }else{
// return getComputedStyle(obj,false)[attr];
// }
return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj,false)[attr] ;
};
// 参数为两个时,是获取对象的属性值,为三个时,是设置对象的属性值
function css(obj,attr,value){
// if(arguments.length == 2){
// return getStyle( obj , attr );
// }else{
// obj.style[attr] = value;
// }
return arguments.length == 2 ? getStyle( obj , attr ) : obj.style[attr] = value ;
};
// 构造弹窗
function pop_window(){
this.Obtn = document.querySelector('#btn');
this.Oclose = document.querySelector('#close');
this.Ologin = document.querySelector('#login');
this.Omask = document.querySelector('#mask');
};
// 绑定弹窗事件
pop_window.prototype.bind = function(){
var that = this ;
this.Obtn.onclick = function(){
css(that.Ologin,'display','block');
css(that.Omask,'display','block');
that.setposition();
};
this.Oclose.onclick = function(){
css(that.Ologin,'display','none');
css(that.Omask,'display','none');
};
window.onresize = document.body.onscroll = function(){
css(that.Ologin,'display','block');
css(that.Omask,'display','block');
that.setposition();
};
};
// 设置弹窗的位置:设置left 和 top 值即可决定弹窗的位置
pop_window.prototype.setposition = function(){
var OlogWidth = css(this.Ologin,'width');
var OlogHeight = css(this.Ologin,'height');
var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
var wind_Width = document.body.clientWidth||document.documentElement.clientWidth;
var wind_Height = window.innerHeight;
css(this.Ologin,'left',(parseInt(wind_Width)-parseInt(OlogWidth))/2 + 'px' );
css(this.Ologin,'top',( (parseInt(wind_Height)-parseInt(OlogHeight) )/2 + parseInt(scrollTop) ) + 'px' );
}
var opop = new pop_window();
opop.bind();
}
运行结果:弹出一个居中对齐的窗口

js 弹窗的实现的更多相关文章
- cefsharp重写默认js弹窗(alert/confirm/prompt)
1.设置js弹窗控制器 webView.JsDialogHandler = this; //js弹窗控制 this表示本类对象,所以本类要实现IJsDialogHandler接口 2.实现IJsDi ...
- js弹窗
常用人JS弹窗,lhgDialog 4.20
- js弹窗登录效果(源码)--web前端
1.JS弹窗登录效果 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...
- 一行js弹窗代码就能设计漂亮的弹窗广告
接到一个设计需求,要求xmyanke在网站右侧挂一个弹窗广告宣传最近的活动,找了半天都没看到合适的,自己鼓捣了一行js弹窗代码就能设计漂亮的弹窗广告,来瞧一下,欢迎拍砖提意见,js弹窗广告代码如下: ...
- js弹窗 js弹出DIV,并使整个页面背景变暗
1.首先写一个遮罩层div,然后再写一个弹窗的div <!-- 遮罩层 --> <div id="cover" style="background: # ...
- (转)js弹窗&返回值(window.open方式)
本文转载自:http://hi.baidu.com/z57354658/item/5d5e26b8e9f42fa7ebba93d4 js弹窗&返回值(window.open方式) test.h ...
- asp.net 后台使用js弹窗失效问题
1.这些事件输出来前后都变成JS代码了,看到到这样的代码的了.会变成<script>alert('合同号XXX已存在')</script>首先后台调试一下看看Page.Clie ...
- JS弹窗带遮蔽的功能
很不错的JS原生自定义弹窗,很实用! function myAlert(str,click,useCancel){ var overflow=""; var $hidder=nul ...
- 原生Js弹窗插件|web弹出层组件|对话框
wcPop.js 是一款基于原生javascript开发的前端 web版 弹窗组件,遵循原生 H5/css3/JS 的书写规范,简单实用.拿来即用(压缩后仅10KB).已经兼容各大主流浏览器.内含多种 ...
随机推荐
- Java里数组的三种初始化方式
静态初始化 除了用new关键字来产生数组以外,还可以直接在定义数组的同时就为数组元素分配空间并赋值. // 静态初始化 int[] iStaticArr = { 5, 2, 0 }; LOLHero[ ...
- [UE4]GameMode和GameInstance
1.GameMode与场景的生命周期是相同的.使用OpenLevel切换到另外一个场景,第一个场景的GameMode就会被销毁,然后场景第二个场景的GameMode 2.GameInstance与进程 ...
- [UE4]Task的定义与使用
在Task蓝图里面可以像普通蓝图一样添加函数.变量. 也可以通过使用“set blackboard value as”设置黑板变量,使用“get blackboard value as”获得黑板变量值 ...
- 数据迁移_老集群RAC迁移数据恢复到新集群RAC
数据迁移_老集群RAC迁移数据恢复到新集群RAC 作者:Eric 微信:loveoracle11g 1.把老集群RAC备份的数据远程拷贝到新集群RAC [root@old-rac-node1 ~]# ...
- BCGcontrolBar(八) Ribbon图标变换
点击前 点击后 CBCGPRibbonButton *pRibbonBtn=NULL; pRibbonBtn=DYNAMIC_DOWNCAST(CBCGPRibbonButton,m_pFrame-& ...
- JPA查询
Pojo: UserDetails EntityManager: entityManager 1. Ceate Criteria CriteriaBuilder builder = entityMan ...
- Spark2.X分布式弹性数据集
跑一下这个结果 参考代码 package com.spark.test import org.apache.spark.sql.SparkSession import org.apache.spark ...
- Spark2.X环境准备、编译部署及运行
下载地址 :https://www.apache.org/dyn/closer.lua/spark/spark-2.2.0/spark-2.2.0.tgz 我们把spark放在节点2上 解压 下面我们 ...
- es6基础(6)--数组扩展
//数组扩展 { let arr=Array.of(3,4,6,7,9,11);//可以是空 console.log('arr=',arr);//[3,4,6,7,9,11] } { //Array. ...
- day6作业(元组,字典,集合)
默写: 1.元组 字典 集合 列表 各自的特点 2.字典添加 删除 修改 循环 必做: 1.餐厅提供了五种不同的菜,使用元组来存储他们,并循环打印出所有菜名,要求用户输入新加的菜名,加入到菜单中,并重 ...