如何使java脚本完成的元素更改永久化

时间:2018-02-24 17:26:37

标签: javascript jquery html

我的问题是如何在Web浏览器中永久地对java脚本所做的元素进行更改。

我在html中使用这个html代码写了一个代码我可以改变html元素的大小和位置,你可以在这个gif中看到

enter image description here

因为我改变了html元素的位置和大小,所以是否可以使该html元素的新位置和大小永久化,我该怎么做呢?

这是我的代码: -



<!DOCTYPE html>
<html>
<style>
#mydiv {
    position: absolute;
    z-index: 9;
    background-color: #f1f1f1;
    text-align: center;
    border: 1px solid #d3d3d3;
    resize: both;
    overflow: auto;
}

#mydivheader {
    padding: 10px;
    cursor: move;
    z-index: 10;
    background-color: lightgreen;
    color: #fff;
}
</style>
<body>

<h1>header div element</h1>

<p>Click and hold the mouse button down while moving the DIV element</p>

<div id="mydiv">
  <div id="mydivheader">move</div>
  <p>drag</p>
  <p>to</p>
  <p>Move</p>
  <p>this</p>
  <p>DIV</p>
</div>

<script>
//Make the DIV element draggagle:
dragElement(document.getElementById(("mydiv")));

function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  if (document.getElementById(elmnt.id + "header")) {
    /* if present, the header is where you move the DIV from:*/
    document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
  } else {
    /* otherwise, move the DIV from anywhere inside the DIV:*/
    elmnt.onmousedown = dragMouseDown;
  }

  function dragMouseDown(e) {
    e = e || window.event;
    // get the mouse cursor position at startup:
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    // call a function whenever the cursor moves:
    document.onmousemove = elementDrag;
  }

  function elementDrag(e) {
    e = e || window.event;
    // calculate the new cursor position:
    pos1 = pos3 - e.clientX;
    pos2 = pos4 - e.clientY;
    pos3 = e.clientX;
    pos4 = e.clientY;
    // set the element's new position:
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  }

  function closeDragElement() {
    /* stop moving when mouse button is released:*/
    document.onmouseup = null;
    document.onmousemove = null;
  }
}
</script>

</body>
</html>
&#13;
&#13;
&#13;

永久&#34;我的意思是当你重新加载页面时它将保持其位置

0 个答案:

没有答案