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

作者:代码家园 时间:2024-04-19 09:50:56 

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


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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层
0
投稿

猜你喜欢

  • JavaScript 日期联动选择器

    2010-08-01 10:18:00
  • Python可视化神器pyecharts之绘制箱形图

    2021-08-04 03:40:53
  • 在Python中操作时间之mktime()方法的使用教程

    2023-02-07 11:38:58
  • 基于Bootstrap实现图片轮播效果

    2024-04-28 10:19:50
  • python2和python3在处理字符串上的区别详解

    2021-10-07 03:29:31
  • ORACLE常见错误代码的分析与解决(三)

    2024-01-17 19:17:57
  • Python实现Mysql数据统计及numpy统计函数

    2024-01-18 13:15:07
  • Python列表list常用内建函数实例小结

    2021-08-07 02:17:15
  • mysql+Spring数据库隔离级别与性能分析

    2024-01-15 00:46:58
  • Python实现线性判别分析(LDA)的MATLAB方式

    2022-07-21 02:38:34
  • ASP 改良版MD5、SHA256多重加密类(二次及多次)

    2012-11-30 20:26:47
  • python3 pandas 读取MySQL数据和插入的实例

    2024-01-22 06:21:33
  • mysql字符集和数据库引擎修改方法分享

    2024-01-21 22:02:49
  • PHP截取指定图片大小的方法

    2023-09-11 13:20:21
  • web标准常见问题集合

    2013-12-25 15:51:19
  • 理解python中装饰器的作用

    2022-01-29 07:58:01
  • Linux下编译安装MySQL-Python教程

    2021-05-03 05:05:40
  • python多线程扫描端口示例

    2022-06-28 23:18:58
  • Update 语句

    2009-06-22 12:52:00
  • Tensorflow2.10实现图像分割任务示例详解

    2021-08-31 13:14:47
  • asp之家 网络编程 m.aspxhome.com