Home >Web Front-end >JS Tutorial >Complete example of JS pop-up div layer that can be dragged and closed_javascript skills

Complete example of JS pop-up div layer that can be dragged and closed_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:14:00972browse

The example in this article describes the complete implementation method of JS pop-up div layer that can be dragged and closed. Share it with everyone for your reference. The specific implementation method is as follows:

Copy code The code is as follows:





JS pops up a draggable and closeable div layer

<script><br> window.onload = function ()<br> {<br> var oWin = document.getElementById("win");<br> var oBtn = document.getElementsByTagName("button")[0];<br> var oClose = document.getElementById("close");<br> var oH2 = oWin.getElementsByTagName("h2")[0];<br> var bDrag = false;<br> var disX = disY = 0;<br> oBtn.onclick = function ()<br> {<br> oWin.style.display = "block" <br> };<br> oClose.onclick = function ()<br> {<br> oWin.style.display = "none"<br> <br> };<br> oClose.onmousedown = function (event)<br> {<br> (event || window.event).cancelBubble = true;<br> };<br> oH2.onmousedown = function (event)<br> { <br> var event = event || window.event;<br> bDrag = true;<br> disX = event.clientX - oWin.offsetLeft;<br> disY = event.clientY - oWin.offsetTop; <br> this.setCapture && this.setCapture(); <br> return false<br> };<br> document.onmousemove = function (event)<br> {<br> if (!bDrag) return;<br> var event = event || window.event;<br> var iL = event.clientX - disX;<br> var iT = event.clientY - disY;<br> var maxL = document.documentElement.clientWidth - oWin.offsetWidth;<br> var maxT = document.documentElement.clientHeight - oWin.offsetHeight; <br> iL = iL < 0 ? 0 : iL;<br /> iL = iL > maxL ? maxL : iL; <br> iT = iT < 0 ? 0 : iT;<br /> iT = iT > maxT ? maxT : iT;<br> <br> oWin.style.marginTop = oWin.style.marginLeft = 0;<br> oWin.style.left = iL "px";<br> oWin.style.top = iT "px"; <br> return false<br> };<br> document.onmouseup = window.onblur = oH2.onlosecapture = function ()<br> {<br> bDrag = false; <br> oH2.releaseCapture && oH2.releaseCapture();<br> };<br> };<br> </script>

×





I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
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