Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 可拖动分区。如何在更改页面和重新加载后保持位置_Javascript_Jquery_Html - Fatal编程技术网

Javascript 可拖动分区。如何在更改页面和重新加载后保持位置

Javascript 可拖动分区。如何在更改页面和重新加载后保持位置,javascript,jquery,html,Javascript,Jquery,Html,我已经为我的页面创建了一个可拖动的菜单div 菜单可以在不同页面之间切换。我的问题是,当我转到一个新页面时,menu div跳回原来的位置 如果当访问者更改页面时,menu div能够保持在它被拖动到的相同位置,那就太好了 我在网上找到了其他类似的主题,但没有一个与我的脚本代码对应。我需要什么代码来做广告?如何使用本地存储实现这一点 <script> dragElement(document.getElementById(("div_drag")));

我已经为我的页面创建了一个可拖动的菜单div

菜单可以在不同页面之间切换。我的问题是,当我转到一个新页面时,menu div跳回原来的位置

如果当访问者更改页面时,menu div能够保持在它被拖动到的相同位置,那就太好了

我在网上找到了其他类似的主题,但没有一个与我的脚本代码对应。我需要什么代码来做广告?如何使用本地存储实现这一点

        <script>        
dragElement(document.getElementById(("div_drag")));

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;
    e.preventDefault();
    // 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;
    e.preventDefault();
    // 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>

dragElement(document.getElementById((“div_drag”));
功能牵引装置(elmnt){
变量pos1=0,pos2=0,pos3=0,pos4=0;
if(document.getElementById(elmnt.id+“header”)){
/*如果存在,则标题是从中移动DIV的位置:*/
document.getElementById(elmnt.id+“header”).onmousedown=dragMouseDown;
}否则{
/*否则,请从DIV内的任何位置移动DIV:*/
elmnt.onmousedown=dragMouseDown;
}
功能下拉列表(e){
e=e | | window.event;
e、 预防默认值();
//在启动时获取鼠标光标位置:
pos3=e.clientX;
pos4=e.clientY;
document.onmouseup=关闭DrageElement;
//每当光标移动时调用函数:
document.onmousemove=elementDrag;
}
功能元素拖动(e){
e=e | | window.event;
e、 预防默认值();
//计算新光标位置:
pos1=pos3-e.clientX;
pos2=pos4-e.clientY;
pos3=e.clientX;
pos4=e.clientY;
//设置元素的新位置:
elmnt.style.top=(elmnt.offsetTop-pos2)+“px”;
elmnt.style.left=(elmnt.offsetLeft-pos1)+“px”;
}
函数closeDrageElement(){
/*释放鼠标按钮时停止移动:*/
document.onmouseup=null;
document.onmousemove=null;
}
}

您可以使用cookie来解决此问题,请参见此处:

只需将位置值保存到本地存储或cookie中,然后在“加载/重新加载默认值”回退页面上从那里加载它们