// 设置HTML标签中的css样式。
//用全局选择器(*)把HTML中的默认样式去掉。
//设置一个拖拽用的div盒子。使用绝对定位。
css代码:
<style type="text/css">
*{
margin:0;
padding:0;
list-style: none;
}
#box{
width:100px;
height:100px;
background-color: hotpink;
position:absolute;
cursor: move;
}
</style>
HTML代码:
//HTML中创建一个需要被拖拽的盒子
<body>
<div id="box"></div>
</body>
JS代码
<script type="text/javascript">
box.onmousedown = function(e){ //onmousedown 鼠标按下时的事件。因为HTML中,box已被设置为ID名,所以这里可以直接调用。
//function中的"e"是要用鼠标和键盘事件时所需参数。
var e = e || event; //获取event对象
var X = e.clientX - box.offsetLeft; /