可拖动弹出层

弹出层的位置是当前窗口的正中央,而不是页面的正中央,页面高度较大时有用。
样式可以在引用页自行添加。

页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE></TITLE>
<script src="popD.js"></script>
</HEAD>

<BODY>

最底层内容
<div id="main" style="display:none;">
<div id="title" style="width:100%;background-color:red;">title</div>
弹出层<input type="button" value="隐藏" id="hid" onclick="closeD();">
</div>
最底层内容
<input type="button" value="显示" id="show" onclick="popD('main','title',300,200);">
<TABLE style="height:1000px;">
<TR>
<TD></TD>
</TR>
</TABLE>

最底层内容<input type="button" value="显示" id="show" onclick="popD('main','title',300,200);">
</BODY>
</HTML>



popD.js


var mainDiv;
var bgDiv;
function popD(mainId, titleId, w, h) {
mainDiv = document.getElementById(mainId);
var bw = document.body.scrollWidth;
var bh = document.body.scrollHeight;
var ch = document.body.clientHeight;
var top = document.body.scrollTop;
bh = bh < ch ? ch : bh;
bgDiv = document.createElement("div");
document.body.appendChild(bgDiv);
bgDiv.style.width = bw + "px";
bgDiv.style.height = bh + "px";
bgDiv.style.opacity = "0.5";
bgDiv.style.backgroundColor = "#D1EEEE";
bgDiv.style.position = "absolute";
bgDiv.style.left = "0";
bgDiv.style.top = "0";
bgDiv.style.zIndex = "98";
mainDiv.style.display = "";
mainDiv.style.backgroundColor = "green";
mainDiv.style.width = w + "px";
mainDiv.style.height = h + "px";
mainDiv.style.position = "absolute";
mainDiv.style.zIndex = "99";
mainDiv.style.left = (bw - w) / 2 + "px";
mainDiv.style.top = top + (ch - h) / 2 + "px";
mainDiv.style.display = "";
var x0 = 0, y0 = 0, x1 = 0, y1 = 0;
var moveable = false;
var tDiv = document.getElementById(titleId);
tDiv.onmousedown = function () {
tDiv.style.cursor = "move";
if (event.button == 1) {
tDiv.setCapture();
x0 = event.clientX;
y0 = event.clientY;
x1 = parseInt(mainDiv.offsetLeft);
y1 = parseInt(mainDiv.offsetTop);
moveable = true;
}
};
tDiv.onmouseup = function () {
if (moveable) {
tDiv.releaseCapture();
moveable = false;
}
};
tDiv.onmousemove = function () {
if (moveable) {
var x = x1 + event.clientX - x0;
var y = y1 + event.clientY - y0;
if (x + w < bw && x > 0) {
mainDiv.style.left = x;
}
if (y + h < bh && y > 0) {
mainDiv.style.top = y;
}
}
};
}
function closeD() {
bgDiv.style.display = "none";
mainDiv.style.display = "none";
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值