css样式
*{
padding: 0px;
margin: 0px;
}
#bigbox{
height: 1000px;
width: 100%;
background-color: black;
}
#box{
height: 140px;
width: 140px;
background-color: white;
margin: 5px;
position: absolute;
cursor: move;
}
利用JavaScript的脚本函数:
window.onload = init;
function init(){
var _box = document.getElementById(“box”);
_box.onmousedown = function(e){
var _x = e.clientX - _box.offsetLeft;
var _y = e.clientY - _box.offsetTop;
document.onmousemove = function(ev){
_box.style.left = ev.clientX - _x + “px”;
_box.style.top = ev.clientY - _y + “px”;
}
}
_box.onmouseup = function(){
document.onmousemove = null;
}
}
body标签内容:
<div id="bigbox">
<div id="box">
</div>
</div>