JS实现弹出浮动窗⼝(⽀持⿏标拖动关闭)实例详解
本⽂实例讲述了JS实现弹出浮动窗⼝。分享给⼤家供⼤家参考。具体如下:
这⾥介绍的JS弹出浮动窗⼝,⽀持⿏标拖动和关闭,点击链接⽂字后弹出层窗⼝,也称作是弹出式对话框吧。
关于⼀些参数说明:
bodycontent:要在窗⼝中显⽰的内容
title:窗⼝的标题
removeable:窗⼝是否能拖动
注意:内容窗体的⾼度是height-30px,请计算好要显⽰的内容⾼度和宽度。
注:在⽕狐或chrome下效果最佳,IE8下可能有些⼩问题。
点击此处查看运⾏效果:
运⾏效果图如下:
炒金针菇的做法具体代码:
<html>
<head>
<title>Js弹出浮动窗⼝,⽀持⿏标拖动和关闭</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
/**
关于⼀些参数说明:
*bodycontent:要在窗⼝显⽰的内容,dom对象
*title:窗⼝标题,字符串类型
*removeable:窗⼝能否拖动,布尔类型
*注意:内容窗体的⾼度是height-30px,请计算好你要显⽰的内容的⾼度和宽度。弹出窗的id为"223238909",所以你的页⾯不要再取值为"223238909"的id了,以防js执⾏出错*/ function createdialog(width,height,bodycontent,title,removeable){
ElementById("www_jb51_net")==null){
/*创建窗⼝的组成元素*/
var dialog = ateElement("div");
var dialogtitlebar= ateElement("div");
var dialogbody = ateElement("div");
var dialogtitleimg = ateElement("span");
var dialogtitle = ateElement("span");
var dialogclose = ateElement("span");
var closeaction = ateElement("button");
/*为窗⼝设置⼀个id,id如此怪异是为了尽量避免与其他⽤户取的id相同⽽出错*/
dialog.id = "223238909";
/*组装对话框标题栏,按从⾥到外的顺序组装*/
dialogtitle.innerHTML = title;
dialogtitlebar.appendChild(dialogtitleimg);
dialogtitlebar.appendChild(dialogtitle);
dialogtitlebar.appendChild(dialogclose);
dialogclose.appendChild(closeaction);
/*组装对话框主体内容*/
if(bodycontent!=null){
bodycontent.style.display = "block";
dialogbody.appendChild(bodycontent);
}
/*组装成完整的对话框*/
dialog.appendChild(dialogtitlebar);
dialog.appendChild(dialogbody);
/*设置窗⼝组成元素的样式*/
var templeft,temptop,tempheight//窗⼝的位置(将窗⼝放在页⾯中间的辅助变量)
var dialogcssText,dialogbodycssText;//拼出dialog和dialogbody的样式字符串
templeft = (document.body.clientWidth-width)/2;
temptop = (document.body.clientHeight-height)/2;
tempheight= height-30;
dialogcssText= "position:absolute;background:#65c294;padding:1px;border:4px;top:"+temptop+"px;left:"+templeft+"px;height:"+height+"px;width:"+width+"px;";
有关艺术的作文dialogbodycssText = "width:100%;background:#ffffff;"+"height:" + tempheight + "px;";
dialog.style.cssText = dialogcssText;
dialogtitlebar.style.cssText = "height:30px;width:100%;background:url(images/titlebar.png) repeat;cursor:move;";
dialogbody.style.cssText  = dialogbodycssText;
dialogtitleimg.style.cssText = "float:left;height:20px;width:20px;background:url(images/40.gif);"+"display:block;margin:4px;line-height:20px;";
dialogtitle.style.cssText = "font-size:16px;float:left;display:block;margin:4px;line-height:20px;";
dialogclose.style.cssText  = "float:right;display:block;margin:4px;line-height:20px;";
怎样网上赚钱
closeaction.style.cssText = "height:20px;width:24px;border-width:1px;"+"background-image:url(images/close.png);cursor:pointer;";
/*为窗⼝元素注册事件*/
var dialogleft = parseInt(dialog.style.left);
var dialogtop = parseInt(p);
var ismousedown = false;//标志⿏标是否按下
/*关闭按钮的事件*/
veChild(dialog);
}
/*实现窗⼝的移动,这段代码很典型,⽹上很多类似的代码*/
入党志愿书 格式if(removeable == true){
var ismousedown = false;
var dialogleft,dialogtop;
var downX,downY;
dialogleft = parseInt(dialog.style.left);
dialogtop = parseInt(p);
ismousedown = true;
downX = e.clientX;
downY = e.clientY;
}
if(ismousedown){
p = e.clientY - downY + dialogtop + "px";
dialog.style.left = e.clientX - downX + dialogleft + "px";
}
}
/*松开⿏标时要重新计算当前窗⼝的位置*/
dialogleft = parseInt(dialog.style.left);
dialogtop = parseInt(p);
ismousedown = false;
}
}
return dialog;
}//end if(if的结束)
}
</script>
<style>
table{background:#b2d235;}
button{font-size:12px;height:23;background:#ece9d8;border-width:1;} #linkurl,#linkname,#savelink{width:100px;}
</style>
</head>
<body>
<!-- 显⽰窗⼝的地⽅ -->
<div id="here"></div><a id="clickhere" href="#">点击⽣成窗⼝</a> <!-- 要嵌⼊到窗⼝的内容 -->
<div id="login" >
<form action="#" method="post" onSubmit="return false;">
<table width="400" height="95">
<tr>
<td width="78">链接⽂字</td>
<td width="168"><input name="link.name" type="text"/></td>
<td width="138" id="linktext"></td>
</tr>
<tr>
<td>链接地址</td>
<td><input name="link.url" type="text"/></td>
郑少秋<td id="linkurl"></td>
</tr>
<tr>
<td></td>
<td><button type="submit" >添加</button></td>  <td id="savelink"></td>
</tr>
</table>
</form>
杨丽菁个人资料</div>
<script type="text/javascript">
var here = ElementById("here");
var login = ElementById("login");
var clickhere = ElementById("clickhere"); lick = function(){
here.appendChild(createdialog(400,95+30,login,"欢迎光临",true));
}
</script>
</body>
</html>
希望本⽂所述对⼤家的javascript程序设计有所帮助。