js 弹出div窗口 可移动 可关闭

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>无标题文档</title>
<script language="javascript">
function alertWin(str){
var msgw, msgh, bordercolor;
msgw = 300;//提示窗口的宽度
msgh = 100;//提示窗口的高度
titleheight = 25 //提示窗口标题高度
bordercolor = "#A480B2";//提示窗口的边框颜色
titlecolor = "#A480B2";//提示窗口的标题颜色

//根据自己需求注意宽度和高度的调整
var iWidth = document.documentElement.clientWidth;
var iHeight = document.documentElement.clientHeight;
//遮罩层
var bgObj = document.createElement("div");
bgObj.setAttribute("id", "bgObj");//设置ID
bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+iWidth+"px;height:"+Math.max(document.body.clientHeight, iHeight)+"px;filter:Alpha(Opacity=30);opacity:0.3;background-color:#000000;z-index:101;";
document.body.appendChild(bgObj);

//弹出窗口
var msgObj=document.createElement("div");
msgObj.setAttribute("id", "msgDiv");//可以用bgObj.id="msgDiv"的方法,是为div指定属性值
msgObj.setAttribute("align", "center");//为div的align赋值
msgObj.style.background = "white";//背景颜色为白色
msgObj.style.border = "1px solid " + bordercolor;//边框属性,颜色在上面已经赋值
msgObj.style.position = "absolute";//绝对定位
msgObj.style.left = (iWidth-msgw)/2 ;//从左侧开始位置
msgObj.style.top = (iHeight-msgh)/2;//从上部开始位置
msgObj.style.font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";//字体属性
msgObj.style.width = msgw + "px";//提示框的宽(上面定义过)
msgObj.style.height = msgh + "px";//提示框的高(上面定义过)
msgObj.style.textAlign = "center";//文本位置属性,居中。
msgObj.style.lineHeight = "25px";//行间距
msgObj.style.zIndex = "102";//层的z轴位置
document.body.appendChild(msgObj);

//弹出窗口标题
var title = document.createElement("div");//创建一个标题对象
title.setAttribute("id", "msgTitle");//为标题对象填加id
title.style.margin = "0";//浮动
title.style.padding = "3px";//浮动
title.style.background = titlecolor;//背景颜色
title.style.filter = "progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
title.style.opacity = "0.75";//透明
//title.style.border="1px solid " + bordercolor;//边框
title.style.height = "25px";//高度
title.style.font = "12px Verdana, Geneva, Arial, Helvetica, sans-serif";//字体属性
//title.style.color = "red";//文字颜色
title.style.cursor = "move";//鼠标样式
title.innerHTML="<table border=‘0‘ width=‘100%‘><tr><td align=‘left‘>2222</td><td align=\"right\"><a href=‘#‘ onclick=‘closeDiv()‘>x</a></td></tr></table>";

//设置窗口可移动

var moveX = 0;
var moveY = 0;
var moveTop = 0;
var moveLeft = 0;
var moveable = false;
var docMouseMoveEvent = document.onmousemove;
var docMouseUpEvent = document.onmouseup;
title.onmousedown = function() {
var evt = getEvent();
moveable = true;
moveX = evt.clientX;
moveY = evt.clientY;
moveTop = parseInt(msgObj.style.top);
moveLeft = parseInt(msgObj.style.left);
document.onmousemove = function() {
if (moveable){
var evt = getEvent();
var x = moveLeft + evt.clientX - moveX;
var y = moveTop + evt.clientY - moveY;
if ( x > 0 &&( x + msgw < iWidth) && y > 0 && (y + msgh < iHeight) ){
msgObj.style.left = x + "px";
msgObj.style.top = y + "px";
}
}
};
document.onmouseup = function (){
if (moveable) {
document.onmousemove = docMouseMoveEvent;
document.onmouseup = docMouseUpEvent;
moveable = false;
moveX = 0;
moveY = 0;
moveTop = 0;
moveLeft = 0;
}
};
}
//获得事件Event对象,用于兼容IE和FireFox
function getEvent() {
return window.event || arguments.callee.caller.arguments[0];
}

msgObj.appendChild(title);//在提示框中增加标题
var txt = document.createElement("p");
txt.style.margin = "1em 0";//文本浮动
txt.setAttribute("id", "msgTxt");//为p属性增加id属性
txt.innerHTML = str;//把传进来的值赋给p属性
msgObj.appendChild(txt);//把p属性增加到提示框里
}

//添加关闭功能
function closeDiv() {
var msgTitelObject = document.getElementById("msgDiv");
document.body.removeChild(msgTitelObject);
var bgObj = document.getElementById("bgObj");
document.body.removeChild(bgObj);

}

</script>
</head>
<body>
<input type="button" value="xx" onclick="alertWin(‘消息主题‘);" />
</body>
</html>

原文地址:https://www.cnblogs.com/yangpeng-jingjing/p/8331022.html

时间: 2024-08-07 08:20:58

js 弹出div窗口 可移动 可关闭的相关文章

创建一个弹出DIV窗口

创建一个弹出DIV窗口 摘自:   http://www.cnblogs.com/TivonStone/archive/2012/03/20/2407919.html 创建一个弹出DIV窗口可能是现在网站/网页制作中最常碰到的问题之一.传统的 JavaScript弹窗已经不适合目前网站的设计理念了,理由有二:首先,不友好——生硬的弹出对话框且伴随着“哐”的一声对用户体验是个很大的挑战: 其次,兼容性不够强——有相当多的浏览器屏蔽了这种JS的Alert()方法.于是,一个良好用户体验的网站需要一种

js弹窗 js弹出DIV,并使整个页面背景变暗

1.首先写一个遮罩层div,然后再写一个弹窗的div <!-- 遮罩层 --> <div id="cover" style="background: #000; position: absolute; left: 0px; top: 0px; width: 100%; filter: alpha(opacity=30); opacity: 0.3; display: none; z-index: 2 "> </div> <!

JS 弹出模态窗口解决方案

最近在项目中使用弹出模态窗口,功能要求: (1)模态窗口选择项目 (2)支持选择返回事件处理 在IE中有showModalDialog 方法,可以很好的解决该问题,但是在Chrome中和FF中就有问题了,它们不支持showModalDialog方法.因此需要采用不同的处理方法. IE中的窗口打开函数option参数定义之间必须用分号分隔,Chrome是用逗号分隔,使用时需要注意这点. var option = ""; //居中显示, option = "dialogWidth

js 弹出对话框的方法总结

原文:http://www.cnblogs.com/xiaofengfeng/archive/2012/10/20/2732784.html <!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.o

JavaScript特效实例008-关闭弹出的窗口时,刷新父窗口

实例008                  关闭弹出的窗口时,刷新父窗口 实例说明 关闭弹出的窗口时,同时刷新父窗口,一般用来使父窗口获取最新的数据. 技术要点 本实例主要应用window.open()语句打开新窗口,并在新窗口中应用opener属性,该属性返回一个引用,用于指定打开本窗口的窗口对象. 语法: window.opener window.opener.方法 window.opener.属性 功能:返回的是一个窗口对象.opener属性与打开该窗口的父窗口相联系,当访问子窗口中op

Jquery+div+css实现弹出登录窗口

基本思路先隐藏(dispaly:none)再显示,半透明蒙版层通过 z-index:9998; z-index:9999; 值越大越在前面 index.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http:

JS弹出窗口代码大全(详细整理)

1.弹启一个全屏窗口 复制代码代码如下: <html> <body http://www.jb51.net','脚本之家','fullscreen');">; <b>www.jb51.net</b> </body> </html> 2.弹启一个被F11化后的窗口 复制代码代码如下: <html> <body 'http://www.jb51.net','脚本之家','channelmode');"

div+js 弹出层

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>jQuery弹出层效果</title> <style> .black_overlay{ display: none; position: absolute; top: 0%; left: 0%; width: 100%; height: 100%; background

Js弹出窗口代码,window.open方法

<html><script language=JavaScript> <!--function click() {if (event.button==2) {window.open('#','popwindows',"toolbar=no,menubar=no,width=200,height=200")}}document.onmousedown=click//--></script><head><meta http-