拖动隐藏div
<html>
<head>
<title>_xWin</title>
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script language=JScript>![](https://www.cnblogs.com/Images/dot.gif)
<!--
//可以打包为js文件;
var x0=0,y0=0,x1=0,y1=0;
var offx=6,offy=6;
var moveable=false;
var hover='orange',normal='slategray';//color;
var index=10000;//z-index;
var xx;
//开始拖动;
function startDrag(obj)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(event.button==1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//锁定标题栏;
obj.setCapture();
//定义对象;
var win = obj.parentNode;
var sha = win.nextSibling;
//记录鼠标和层位置;
x0 = event.clientX;
y0 = event.clientY;
x1 = parseInt(win.style.left);
y1 = parseInt(win.style.top);
//记录颜色;
normal = obj.style.backgroundColor;
//改变风格;
obj.style.backgroundColor = hover;
win.style.borderColor = hover;
obj.nextSibling.style.color = hover;
sha.style.left = x1 + offx;
sha.style.top = y1 + offy;
moveable = true;
}
}
//拖动;
function drag(obj)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(moveable)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var win = obj.parentNode;
var sha = win.nextSibling;
win.style.left = x1 + event.clientX - x0;
sha.style.left = parseInt(win.style.left) + offx;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((y1 + event.clientY - y0)>0 && (y1 + event.clientY - y0)<600)
{
win.style.top = y1 + event.clientY - y0;
sha.style.top = parseInt(win.style.top) + offy;
}
}
}
//停止拖动;
function stopDrag(obj)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(moveable)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var win = obj.parentNode;
var sha = win.nextSibling;
var msg = obj.nextSibling;
win.style.borderColor = normal;
obj.style.backgroundColor = normal;
msg.style.color = normal;
sha.style.left = obj.parentNode.style.left;
sha.style.top = obj.parentNode.style.top;
obj.releaseCapture();
moveable = false;
}
}
//获得焦点;
function getFocus(obj)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(obj.style.zIndex!=index)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
index = index + 2;
var idx = index;
obj.style.zIndex=idx;
obj.nextSibling.style.zIndex=idx-1;
}
}
//最小化;
function min(obj)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
var win = obj.parentNode.parentNode;
var sha = win.nextSibling;
var tit = obj.parentNode;
var msg = tit.nextSibling;
var flg = msg.style.display=="none";
if(flg)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
win.style.height = parseInt(msg.style.height) + parseInt(tit.style.height) + 2*2;
sha.style.height = win.style.height;
msg.style.display = "block";
obj.innerHTML = "0";
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
win.style.height = parseInt(tit.style.height) + 2*2;
sha.style.height = win.style.height;
obj.innerHTML = "2";
msg.style.display = "none";
}
}
//关闭;
function cls(obj)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
var win = obj.parentNode.parentNode.parentNode;
//var sha = win.nextSibling;
win.style.visibility = "hidden";
//sha.style.visibility = "hidden";
}
//显示/隐藏;
function ShowHide()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if (xx!=null)
if (xx.style.visibility == "hidden")
xx.style.visibility = "visible";
else if (xx.style.visibility == "visible")
xx.style.visibility = "hidden";
}
//创建一个对象;
function xWin(id,w,h,l,t,tit,msg)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
index = index+2;
this.id = id;
this.width = w;
this.height = h;
this.left = l;
this.top = t;
this.zIndex = index;
this.title = tit;
this.message = msg;
this.obj = null;
this.bulid = bulid;
this.bulid();
xx = document.getElementById('allx');
xx.style.visibility = "visible";
![](/Images/OutliningIndicators/InBlock.gif)
}
//初始化;
function bulid()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
var str = ""
+ "<div id='allx'><div id='xMsg'" + this.id + " "
+ "style='"
+ "z-index:" + this.zIndex + ";"
+ "width:" + this.width + ";"
+ "height:" + this.height + ";"
+ "left:" + this.left + ";"
+ "top:" + this.top + ";"
+ "background-color:" + normal + ";"
+ "color:" + normal + ";"
+ "font-size:11px;"
+ "font-family:Verdana;"
+ "position:absolute;"
+ "cursor:default;"
+ "border:2px solid " + normal + ";"
+ "' "
+ "onmousedown='getFocus(this)'>"
+ "<div "
+ "style='"
+ "background-color:" + normal + ";"
+ "width:" + (this.width-2*2) + ";"
+ "height:20;"
+ "color:white;"
+ "' "
+ "onmousedown='startDrag(this)' "
+ "onmouseup='stopDrag(this)' "
+ "onmousemove='drag(this)' "
+ "ondblclick='min(this.childNodes[1])'"
+ ">"
+ "<span style='width:" + (this.width-2*14-4) + ";padding-left:3px;'>" + this.title + "</span>"
+ "<span style='width:14;border-width:0px;color:white;font-family:webdings;' onclick='min(this)'>0</span>"
+ "<span style='width:14;border-width:0px;color:white;font-family:webdings;' onclick='cls(this)'>r</span>"
+ "</div>"
+ "<div style='"
+ "width:100%;"
+ "height:" + (this.height-20-4) + ";"
+ "background-color:white;"
+ "line-height:14px;"
+ "word-break:break-all;"
+ "padding:3px;"
+ "'>" + this.message + "</div>"
+ "</div>"
+ "<div id='xshadow' style='"
+ "width:" + this.width + ";"
+ "height:" + this.height + ";"
+ "top:" + this.top + ";"
+ "left:" + this.left + ";"
+ "z-index:" + (this.zIndex-1) + ";"
+ "position:absolute;"
+ "background-color:black;"
+ "filter:alpha(opacity=40);"
+ "'>by wildwind</div></div>";
document.getElementById('msgbox').innerHTML = str;
}
//-->
</script>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script language='JScript'>![](https://www.cnblogs.com/Images/dot.gif)
<!--
function initialize()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
var a = new xWin("1",460,400,200,200,"是要这个效果吗?","<br>这里有几点需要注意的地方:<br><br>1、xx这个变量是你自己添加的吧?想法是对的,但是放错了地方,你只在cls()这个方法里面给xx赋值,这就造成了这个对象在窗口第一次出现时由于没有被赋值(关闭按钮还没有被点击),导致无法获取,所以应该把对xx赋值这个工作放在窗口第一次初始化完成的地方,也就是xWin函数中,this.build语句之后。当然也可能你已经试过放在这里,但是发现根本不起作用,这就涉及到下一个要注意的问题了。<br><br>2、Build函数的最后,你是通过insertAdjacentHTML()方法把生成的代码放在了网页全部html标签之后,因此生成的代码中的对象在页面中使用getElementById()方法根本访问不到,所以才会不起作用,怎么改呢?方法很多,我是这样处理的,在页面body的关闭标签之前插入一个div,然后用innerHTML()方法写入。<br><br>3、对于代码,我进行了一点修正,因为发现你的cls()方法当中把xWin和他的阴影一并隐藏了,但是在ShowHide()方法中却只把xWin显示出来,这就导致关闭(实质上是hidden)后再打开的窗体已经没有了阴影,当然这也不算什么大问题,我在xWin和他的阴影外面套了一个allx的div,再把这个对象赋给xx,然后就可以一并隐藏一并显示了,更方便一些。<br><br>4、主要就是这几个地方,这个效果还是很不错的,不过程序结构还可以再优化一些,代码比较多,我也没有细看,稍微改了一下,可能不是最好的方法。大家继续提建议啊。希望对楼主能有帮助。");
![](/Images/OutliningIndicators/InBlock.gif)
}
window.onload = initialize;
//-->
</script>
</head>
<body onselectstart='return false' oncontextmenu='return false' scroll='no'>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
<DIV class=textSheet
style="BORDER-RIGHT: #3E8529 1px solid; BORDER-TOP: #639517 1px solid; BORDER-LEFT: #3E8529 1px solid; BORDER-BOTTOM: #3E8529 1px solid">
![](/Images/OutliningIndicators/None.gif)
<table width="242" height="160" border="1" ID="Table2">
![](/Images/OutliningIndicators/None.gif)
<td align="center" valign="middle" style="cursor:hand;font-size:32px;color: #808080" onMouseMove="bgColor='#ffff99'" onmouseout="bgColor='#FFFFFF'" onclick="ShowHide()">显示隐藏</td>
</table>
</DIV>
<div id="msgbox"></div>
</body>
</html>
![](/Images/OutliningIndicators/None.gif)