JS弹出可拖拽可关闭的div层完整实例

作者: 关于计算机  发布:2019-09-06

本文实例讲述了JS弹出可拖拽可关闭的div层完整实现方法。分享给大家供大家参考。具体实现方法如下:

JS弹出可拖拽可关闭的div层完整实例,jsdiv

本文实例讲述了JS弹出可拖拽可关闭的div层完整实现方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ";
<html xmlns=";
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS弹出可拖拽可关闭的div层</title>
<style>
html,body{height:100%;overflow:hidden;}
body,div,h2{margin:0;padding:0;}
body{font:12px/1.5 Tahoma;}
center{padding-top:10px;}
button{cursor:pointer;}
#win{position:absolute;top:50%;left:50%;width:400px;height:200px;background:#fff;border:4px solid #f90;margin:-102px 0 0 -202px;display:none;}
h2{font-size:12px;height:18px;text-align:right;background:#FC0;border-bottom:3px solid #f90;padding:5px;cursor:move;}
h2 span{color:#f90;cursor:pointer;background:#fff;border:1px solid #f90;padding:0 2px;}
</style>
<script>
window.onload = function ()
{
 var oWin = document.getElementById("win");
 var oBtn = document.getElementsByTagName("button")[0];
 var oClose = document.getElementById("close");
 var oH2 = oWin.getElementsByTagName("h2")[0];
 var bDrag = false;
 var disX = disY = 0;
 oBtn.onclick = function ()
 {
  oWin.style.display = "block"
 };
 oClose.onclick = function ()
 {
  oWin.style.display = "none"
 
 };
 oClose.onmousedown = function (event)
 {
  (event || window.event).cancelBubble = true;
 };
 oH2.onmousedown = function (event)
 { 
  var event = event || window.event;
  bDrag = true;
  disX = event.clientX - oWin.offsetLeft;
  disY = event.clientY - oWin.offsetTop; 
  this.setCapture && this.setCapture(); 
  return false
 };
 document.onmousemove = function (event)
 {
  if (!bDrag) return;
  var event = event || window.event;
  var iL = event.clientX - disX;
  var iT = event.clientY - disY;
  var maxL = document.documentElement.clientWidth - oWin.offsetWidth;
  var maxT = document.documentElement.clientHeight - oWin.offsetHeight; 
  iL = iL < 0 ? 0 : iL;
  iL = iL > maxL ? maxL : iL;  
  iT = iT < 0 ? 0 : iT;
  iT = iT > maxT ? maxT : iT;
 
  oWin.style.marginTop = oWin.style.marginLeft = 0;
  oWin.style.left = iL + "px";
  oWin.style.top = iT + "px"; 
  return false
 };
 document.onmouseup = window.onblur = oH2.onlosecapture = function ()
 {
  bDrag = false;   
  oH2.releaseCapture && oH2.releaseCapture();
 };
};
</script>
</head>
<body>

<div id="win"><h2><span id="close">×</span></h2></div>
<center><button>弹出层</button></center>
</body>
</html>

希望本文所述对大家的javascript程序设计有所帮助。

本文实例讲述了JS弹出可拖拽可关闭的div层完整实现方法。分享给大家供大家参考。具体实现方法...

复制代码 代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ";
<html xmlns=";
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS弹出可拖拽可关闭的div层</title>
<style>
html,body{height:100%;overflow:hidden;}
body,div,h2{margin:0;padding:0;}
body{font:12px/1.5 Tahoma;}
center{padding-top:10px;}
button{cursor:pointer;}
#win{position:absolute;top:50%;left:50%;width:400px;height:200px;background:#fff;border:4px solid #f90;margin:-102px 0 0 -202px;display:none;}
h2{font-size:12px;height:18px;text-align:right;background:#FC0;border-bottom:3px solid #f90;padding:5px;cursor:move;}
h2 span{color:#f90;cursor:pointer;background:#fff;border:1px solid #f90;padding:0 2px;}
</style>
<script>
window.onload = function ()
{
 var oWin = document.getElementById("win");
 var oBtn = document.getElementsByTagName("button")[0];
 var oClose = document.getElementById("close");
 var oH2 = oWin.getElementsByTagName("h2")[0];
 var bDrag = false;
 var disX = disY = 0;
 oBtn.onclick = function ()
 {
  oWin.style.display = "block"
 };
 oClose.onclick = function ()
 {
  oWin.style.display = "none"
 
 };
 oClose.onmousedown = function (event)
 {
  (event || window.event).cancelBubble = true;
 };
 oH2.onmousedown = function (event)
 { 
  var event = event || window.event;
  bDrag = true;
  disX = event.clientX - oWin.offsetLeft;
  disY = event.clientY - oWin.offsetTop; 
  this.setCapture && this.setCapture(); 
  return false
 };
 document.onmousemove = function (event)
 {
  if (!bDrag) return;
  var event = event || window.event;
  var iL = event.clientX - disX;
  var iT = event.clientY - disY;
  var maxL = document.documentElement.clientWidth - oWin.offsetWidth;
  var maxT = document.documentElement.clientHeight - oWin.offsetHeight; 
  iL = iL < 0 ? 0 : iL;
  iL = iL > maxL ? maxL : iL;  
  iT = iT < 0 ? 0 : iT;
  iT = iT > maxT ? maxT : iT;
 
  oWin.style.marginTop = oWin.style.marginLeft = 0;
  oWin.style.left = iL + "px";
  oWin.style.top = iT + "px"; 
  return false
 };
 document.onmouseup = window.onblur = oH2.onlosecapture = function ()
 {
  bDrag = false;   
  oH2.releaseCapture && oH2.releaseCapture();
 };
};
</script>
</head>
<body>

<div id="win"><h2><span id="close">×</span></h2></div>
<center><button>弹出层</button></center>
</body>
</html>

希望本文所述对大家的javascript程序设计有所帮助。

您可能感兴趣的文章:

  • JS基于面向对象实现的拖拽库实例
  • jquery方法+js一般方法+js面向对象方法实现拖拽效果
  • js实现拖拽效果
  • div拖拽插件——JQ.MoveBox.js(自制JQ插件)
  • js 表格排序(编辑+拖拽+缩放)
  • 使用js实现的简单拖拽效果
  • js拖拽一些常见的思路方法整理
  • JS组件Bootstrap Table表格行拖拽效果实现代码
  • JS组件Bootstrap Table表格多行拖拽效果实现代码
  • JavaScript 拖拽翻页效果代码
  • js实现使用鼠标拖拽切换图片的方法
  • JS基于面向对象实现的拖拽功能示例

本文由今晚买四不像发布于关于计算机,转载请注明出处:JS弹出可拖拽可关闭的div层完整实例

关键词: