在页面上拖动的菜单

时间:2018-06-24 18:59:46

标签: javascript jquery html css

我正在尝试创建一个可以在网页上拖动的菜单。

我正在尝试创建一个div,显示菜单。当您点击菜单时,新的div会显示在顶部,并带有链接和隐藏按钮,以返回以再次关闭div。

我将两个div包裹在另一个已使其可拖动的div中,但是div不会响应被拖动。

当我检查菜单时,可以看到拖动功能正在响应。在检查器中,我可以看到div的位置发生了变化,但div却没有变化。

我在做什么错了?

#div.drag {
    position: absolute;
    z-index: 9;
    background-color: #f1f1f1;
    text-align: center;
    border: 1px solid #d3d3d3;
}

#mydivheader {
    padding: 10px;
    cursor: move;
    z-index: 10;
    background-color: #2196F3;
    color: #fff;
}
<div id="div_drag">
	 <div id="mydivheader">
	<a onclick="document.getElementById('div_name2').style.display='';return false;" href="">Menu</a>

	<div id="div_name2">

                              <a href="firstyear.html">+2018</a>
                              <br></br>
                              <a href="secondyear.html">+2017</a>
                              <br></br>
                              <a href="thirdyear.html">+2016</a>
                              <br></br>
                              <a href="fourthyear.html">+2015</a>
                              <br></br>

                              <a onclick="document.getElementById('div_name2').style.display='none';return false;" href="" 
                              >hide</a>
				</div>
		</div>	  
</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>

0 个答案:

没有答案