<style>
*{
padding: 0;
margin: 0;
}
a{
font-size: 40px;
display: block;
text-align: center;
}
#box{
width: 400px;
height: 300px;
border: 1px solid red;
position: absolute;
left: 50%;
top: 50%;
margin-left: -200px;
margin-top: -150px;
z-index: 99;
display: none;
}
p{
position: relative;
height: 50px;
line-height: 50px;
text-align: center;
background-color: goldenrod;
cursor: all-scroll;
}
span{
position: absolute;
right: 10px;
color: #fff;
cursor: pointer;
}
#box div{
line-height: 250px;
text-align: center;
background-color: #fff;
}
#zheDang{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #000;
opacity: 0.4;
z-index: 9;
display: none;
}
</style>
<a id="a" href="http://www.baidu.com">点击弹出内容</a>
<div id="box">
<p id="title">拖拽区
<span id="close">X</span>
</p>
<div>内容区</div>
</div>
<div id="zheDang"></div>
<script>
function my$(id){
return document.getElementById(id);
}
my$("a").onclick = function(e){
my$("zheDang").style.display = "block";
my$("box").style.display = "block";
return false;
}
my$("close").onclick = function(){
my$("zheDang").style.display = "none";
my$("box").style.display = "none";
}
my$("title").onmousedown = function(e){
var spaceX = evt.getClientX(e) - my$("box").offsetLeft - 200;
var spaceY = evt.getClientY(e) - my$("box").offsetTop - 150;
document.onmousemove = function(e){
var x = evt.getClientX(e) - spaceX;
var y = evt.getClientY(e) - spaceY;
my$("box").style.left = x + "px";
my$("box").style.top = y + "px";
}
}
document.onmouseup = function(){
document.onmousemove = null;
}
var evt = {
getEvent: function(e){
return window.event ? window.event : e;
},
getScrollLeft: function(){
return window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
},
getScrollTop: function(){
return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
},
getClientX: function(e){
return this.getEvent(e).clientX;
},
getClientY: function(e){
return this.getEvent(e).clientY;
},
getPageX: function(e){
return this.getEvent(e).pageX ? this.getEvent(e).pageX : this.getClientX(e) + this.getScrollLeft();
},
getPageY: function(e){
return this.getEvent(e).pageY ? this.getEvent(e).pageY : this.getClientY(e) + this.getScrollTop();
}
}
</script>