Advertisement
GBH007

Moveable window

Mar 9th, 2022
1,400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.41 KB | None | 0 0
  1. <html>
  2.   <body style="margin: 0">
  3.     <style>
  4.       .window {
  5.         display: block;
  6.         position: absolute;
  7.         background: lightcoral;
  8.         width: 500px;
  9.         height: 300px;
  10.         top: 100px;
  11.         left: 100px;
  12.       }
  13.       .window-head {
  14.         background: lightseagreen;
  15.         height: 20px;
  16.       }
  17.       .window-content {
  18.         background: lightgreen;
  19.         overflow: scroll;
  20.         width: 100%;
  21.         height: 100%;
  22.       }
  23.       .window-head-title {
  24.         float: left;
  25.         background-color: aqua;
  26.         width: calc(100% - 20px);
  27.         height: 20px;
  28.         cursor: grab;
  29.         user-select: none;
  30.       }
  31.       .window-head-actions {
  32.         float: right;
  33.         background-color: lightsteelblue;
  34.         width: 20px;
  35.         height: 20px;
  36.         cursor: pointer;
  37.       }
  38.     </style>
  39.     <div class="window" id="bind-0">
  40.       <div class="window-head">
  41.         <div class="window-head-title" id="bind-1">Title</div>
  42.         <div class="window-head-actions" id="bind-2">
  43.           <svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">
  44.             <line x1="1" y1="1" x2="9" y2="9" stroke="black" />
  45.             <line x1="1" y1="9" x2="9" y2="1" stroke="black" />
  46.           </svg>
  47.         </div>
  48.       </div>
  49.       <div class="window-content" id="bind-3"></div>
  50.     </div>
  51.     <script>
  52.       let moveOn = false;
  53.       let bind0 = document.getElementById("bind-0");
  54.       let bind1 = document.getElementById("bind-1");
  55.       let bind2 = document.getElementById("bind-2");
  56.       let bind3 = document.getElementById("bind-3");
  57.       function addText(text) {
  58.         bind3.innerText = bind3.innerText + "\n" + text;
  59.       }
  60.       bind2.onclick = () => {
  61.         addText("close " + new Date().toLocaleString());
  62.       };
  63.  
  64.       bind1.onmouseup = (event) => {
  65.         moveOn = false;
  66.       };
  67.       bind1.onmouseleave = (event) => {
  68.         moveOn = false;
  69.       };
  70.       bind1.onmousedown = (event) => {
  71.         moveOn = true;
  72.       };
  73.       bind1.onmousemove = (event) => {
  74.         if (!moveOn) {
  75.           return;
  76.         }
  77.         console.log(moveOn);
  78.         let pos = bind1.getBoundingClientRect();
  79.         bind0.style.top = event.clientY - (pos.bottom - pos.top) / 2 + "px";
  80.         bind0.style.left = event.clientX - (pos.right - pos.left) / 2 + "px";
  81.         console.log(event);
  82.       };
  83.     </script>
  84.   </body>
  85. </html>
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement