元素拖拽
關于元素拖拽,注意:這里先不考慮瀏覽器兼容問題 <div class = "container" ><div id="move" style="position:absolute; width:100px; height:100px; background:greenyellow"></div>
</div>
<script type="text/javascript">var dragging=null,tLeft,tTop,moveElem=document.getElementById("move");document.addEventListener("mousedown", function (event) {if(event.target==moveElem){dragging=true;var target=event.target;tLeft=event.clientX-target.offsetLeft;tTop=event.clientY-target.offsetTop;}});document.addEventListener("mouseup", function (e) {dragging=false;});document.addEventListener("mousemove", function (e) {if(dragging){var moveX= e.clientX-tLeft,moveY= e.clientY-tTop;moveElem.style.left=moveX+"px";moveElem.style.top=moveY+"px";}});
</script> offsetLeft:指元素左邊距離其包含元素的距離
offsetTop:指元素上邊距離其包含元素的距離
clientX:鼠標按下時的x坐標
clientY:鼠標按下時的y坐標
offsetTop:指元素上邊距離其包含元素的距離
clientX:鼠標按下時的x坐標
clientY:鼠標按下時的y坐標
轉載于:https://www.cnblogs.com/sumyn/p/7641512.html
總結
- 上一篇: docker容器相关
- 下一篇: 湖南集训day3