H5随便拖拽
<style>
*{
margin: 0px;
padding: 0px;
}
div{
float: left;
width: 100px;
height: 100px;
}
p{
height: 20px;
">saddlebrown;
}
#div1{
border: 1px solid red
}
#div2{
border: 1px solid blue
}
#div3{
border: 1px solid black;
}
</style>
<body>
<div id="div1">
<p draggable="true">拖拽我</p>
<p draggable="true">也拖拽我</p>
</div>
<div id="div2"></div>
<div id="div3"></div>
<script>
var obj = null;//当前被拖拽的元素
document.ondragstart = function (e) {
// console.log('开始')
e.target.style.opacity = 0.5;
obj = e.target
}
document.ondragend = function (e) {
e.target.style.opacity = 1
}
//浏览器会默认阻止ondrop事件 需要在ondragover中阻止默认行为
document.ondragover = function (param) {
param.preventDefault()
}
document.ondrop = function (e) {
e.target.appendChild(obj)
}
</script>