Home > Web Front-end > JS Tutorial > js implements framed drag and drop effect

js implements framed drag and drop effect

小云云
Release: 2018-03-26 16:55:31
Original
1378 people have browsed it

This article mainly shares with you js to implement framed drag and drop effect, mainly in the form of code. I hope it can help everyone.

<!doctype html>
<html>
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
  <style>
  #box {
  width:100px;
  height:100px;
  background:#ff0099;
  position:absolute;
  
  }
  .box1 {
  border:1px solid #000000;
  position:absolute;
  
  }
  </style>
 </head>
 <body>
 <p id = &#39;box&#39;></p>
 <script>
 var box = document.getElementById(&#39;box&#39;);
     box.onmousedown = function(e){
  var box1 = document.createElement("p");
    document.body.appendChild(box1);
    box1.style.width = box.offsetWidth + &#39;px&#39;;
    box1.style.height = box.offsetHeight + &#39;px&#39;;
 box1.style.left = box.offsetLeft + &#39;px&#39;;
 box1.style.top = box.offsetTop + &#39;px&#39;;
    box1.className = &#39;box1&#39;;
    e = e || event;
 //计算鼠标在盒子中的位置;
 var x = e.pageX - box.offsetLeft;
 var y = e.pageY - box.offsetTop;
 document.onmousemove = function(e){


  e = e || event;
  //计算盒子在页面上的坐标;
  var xx = e.pageX - x;
  var yy = e.pageY - y;
  box1.style.left = xx + &#39;px&#39;;
  box1.style.top = yy + &#39;px&#39;;
 document.onmouseup = function(){
 box.style.left = box1.offsetLeft + &#39;px&#39;;
 box.style.top = box1.offsetTop + &#39;px&#39;;
 document.body.removeChild(box1);
 document.onmousemove = &#39;null&#39;;
 
 
 
 }
 
 return false;
 }
 
 
 }
 
 
 </script>
 </body>
</html>
Copy after login

The above is the detailed content of js implements framed drag and drop effect. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template