以下是js代码:就是第一行惹的祸,代码本身没有问题!!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//ZH-CN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>可以拖动的对话框</title>
<link type="text/css" rel="stylesheet" href="DragableDialog.css"/>
<script type="text/javascript">
<!--
function $(obj){
return document.getElementById(obj);
}
function dialog(){
this.drag = function(obj){
var x, y;
var _this = this;
obj.parentNode.style.zIndex = -1;
var isIE = (document.all) ? true : false;
obj.onmousedown = function(ev){
if(_this._activeDialog.activeDialog && _this._activeDialog.activeDialog != this){
_this._activeDialog.activeDialog.parentNode.style.zIndex = -1
}
_this._activeDialog.activeDialog = this;
obj.parentNode.style.zIndex = 1;
//obj.parentNode.setAttribute("z-index", zindex);
ev = ev || window.event;
x = ev.clientX - obj.parentNode.offsetLeft;
y = ev.clientY - obj.parentNode.offsetTop;
if(isIE){
document.attachEvent("onmousemove", mousemove_handle);
document.attachEvent("onmouseup", mouseup_handle);
}else{
document.addEventListener("mousemove", mousemove_handle, true);
document.addEventListener("mouseup", mouseup_handle, true);
}
};
function mousemove_handle(ev){
ev = ev || window.event;
obj.parentNode.style.top = ev.clientY - y;
obj.parentNode.style.left = ev.clientX - x;
}
function mouseup_handle(){
//obj.parentNode.style.zIndex = -1;
if(isIE){
document.detachEvent("onmousemove", mousemove_handle);
document.detachEvent("onmouseup", mouseup_handle);
}else{
document.removeEventListener("mousemove", mousemove_handle, true);
document.removeEventListener("mouseup", mouseup_handle, true);
}
}
};
}
window.onload = function(){
dialog.prototype._activeDialog = {};
var t1 = new dialog();
t1.drag($("head1")); var t2 = new dialog();
t2.drag($("head2"));
}
//-->
</script>
</head>
<body>
<form>
<div id="dialog1" class="dialog">
<div id="head1" class="head">
<label>对话框1</label>
<a href="#"> X</a>
</div>
<div id="body1" class="body">
<div id="input1" class="input">
<input type="submit" value="确定"/>
<input name="button" type="button" value="重置"/>
</div>
</div>
</div>
<div id="dialog2" class="dialog">
<div id="head2" class="head">
<label>对话框2</label>
<a href="#"> X</a>
</div>
<div id="body2" class="body">
<div id="input2" class="input">
<input type="submit" value="确定"/>
<input name="button" type="button" value="重置"/>
</div>
</div>
</div>
</form>
</body>
</html>
以下是css:
.dialog{
position:absolute;
border:1px solid black;
width:250px;
height:170px;
}
.head{
position:absolute;
width:250px;
height:20px;
background-color:#3366CC;
left: 0px;
top: 0px;
}
.body{
position:absolute;
width:250px;
height:150px;
background-color:#233451;
top: 19px;
left: 0px;
}
.input{
position:absolute;
left: 73px;
top: 107px;
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//ZH-CN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>可以拖动的对话框</title>
<link type="text/css" rel="stylesheet" href="DragableDialog.css"/>
<script type="text/javascript">
<!--
function $(obj){
return document.getElementById(obj);
}
function dialog(){
this.drag = function(obj){
var x, y;
var _this = this;
obj.parentNode.style.zIndex = -1;
var isIE = (document.all) ? true : false;
obj.onmousedown = function(ev){
if(_this._activeDialog.activeDialog && _this._activeDialog.activeDialog != this){
_this._activeDialog.activeDialog.parentNode.style.zIndex = -1
}
_this._activeDialog.activeDialog = this;
obj.parentNode.style.zIndex = 1;
//obj.parentNode.setAttribute("z-index", zindex);
ev = ev || window.event;
x = ev.clientX - obj.parentNode.offsetLeft;
y = ev.clientY - obj.parentNode.offsetTop;
if(isIE){
document.attachEvent("onmousemove", mousemove_handle);
document.attachEvent("onmouseup", mouseup_handle);
}else{
document.addEventListener("mousemove", mousemove_handle, true);
document.addEventListener("mouseup", mouseup_handle, true);
}
};
function mousemove_handle(ev){
ev = ev || window.event;
obj.parentNode.style.top = ev.clientY - y;
obj.parentNode.style.left = ev.clientX - x;
}
function mouseup_handle(){
//obj.parentNode.style.zIndex = -1;
if(isIE){
document.detachEvent("onmousemove", mousemove_handle);
document.detachEvent("onmouseup", mouseup_handle);
}else{
document.removeEventListener("mousemove", mousemove_handle, true);
document.removeEventListener("mouseup", mouseup_handle, true);
}
}
};
}
window.onload = function(){
dialog.prototype._activeDialog = {};
var t1 = new dialog();
t1.drag($("head1")); var t2 = new dialog();
t2.drag($("head2"));
}
//-->
</script>
</head>
<body>
<form>
<div id="dialog1" class="dialog">
<div id="head1" class="head">
<label>对话框1</label>
<a href="#"> X</a>
</div>
<div id="body1" class="body">
<div id="input1" class="input">
<input type="submit" value="确定"/>
<input name="button" type="button" value="重置"/>
</div>
</div>
</div>
<div id="dialog2" class="dialog">
<div id="head2" class="head">
<label>对话框2</label>
<a href="#"> X</a>
</div>
<div id="body2" class="body">
<div id="input2" class="input">
<input type="submit" value="确定"/>
<input name="button" type="button" value="重置"/>
</div>
</div>
</div>
</form>
</body>
</html>
以下是css:
.dialog{
position:absolute;
border:1px solid black;
width:250px;
height:170px;
}
.head{
position:absolute;
width:250px;
height:20px;
background-color:#3366CC;
left: 0px;
top: 0px;
}
.body{
position:absolute;
width:250px;
height:150px;
background-color:#233451;
top: 19px;
left: 0px;
}
.input{
position:absolute;
left: 73px;
top: 107px;
}
解决方案 »
- 获取textarea文本框的值
- 一个显示和收藏表格的js代码! 为什么只有IE上可用?Chrome Firefox Safari都实现不了?什么问题?
- Extjs Tab控件网页相对布局问题
- 在IE下能用但到了Mozilla中就不能用了,不知道应该怎么改?
- 哭了,这是什么错,找了一下午,没有结果
- 高手帮忙,关于用javascipt生成树的怪问题
- Javascript中如何取得IMG标签距离文档窗口坐标(0,0)的距离(象素)?
- 用ymPrompt 这个的时候,选择的确定还是取消,这个tp值是怎么传来的呢
- JQuery怎么操作外部的css文件,普通方法试着不行
- 请各位大神看一下为什么reg_msg没有定义
- IE下JavaScript数组问题
- 请教大师,不同目录下XML失效怎么办
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">这个声明就可以用了,很奇怪!!
function mousemove_handle(ev){
ev = ev || window.event;
obj.parentNode.style.position="absolute";
obj.parentNode.style.top = ev.clientY - y+"px";
obj.parentNode.style.left = ev.clientX - x+"px";
} 样式:
.head{
position:absolute;
width:250px;
height:20px;
background-color:#3366CC;
left: 0px;
top: 0px;
cursor:move;
}
这样就能看出,鼠标在什么位置时能够移动