JavaScript拖动层Div代码

作者:632855323gjp 时间:2024-04-16 09:46:48 

效果图:(灰色区域可拖动)

JavaScript拖动层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>无标题文档</title>
<style type="text/css">
*{ margin:0; padding:0;} .div{ width:100px; height:100px; position:absolute;left:100px; top:100px; background:#ccc;}
</style>
</head>
<body>
<div class="div">
</div>
<script type="text/javascript">
 var div = document.getElementsByTagName('div')[0];
 var zIndex = 6;
 drag(div);
 div.ondblclick = function() {
 alert("ok");
 };
 function drag(oDrag) {
 var disX = dixY = 0;
 oDrag.onmousedown = function(event) {
  var event = event || window.event;
  disX = event.clientX - this.offsetLeft;
  disY = event.clientY - this.offsetTop;
  var oTemp = this.cloneNode(true);
  document.body.appendChild(oTemp);
  document.onmousemove = function(event) {
  var event = event || window.event;
  var iL = event.clientX - disX;
  var iT = event.clientY - disY;
  var maxL = document.documentElement.clientWidth - oDrag.offsetWidth;
  var maxT = document.documentElement.clientHeight - oDrag.offsetHeight;
  iL <= 0 && (iL = 0);
  iT <= 0 && (iT = 0);
  iL >= maxL && (iL = maxL);
  iT >= maxT && (iT = maxT);
  oTemp.style.zIndex = zIndex++;
  oTemp.style.opacity = "0.5";
  oTemp.style.filter = "alpha(opacity=50)";
  oTemp.style.left = iL + "px";
  oTemp.style.top = iT + "px";
  return false;
  };
  document.onmouseup = function() {
  document.onmousemove = null;
  document.onmouseup = null;
  oDrag.style.opacity = oTemp.style.opacity;
  var arr = {
   left: oTemp.offsetLeft,
   top: oTemp.offsetTop
  };
  oDrag.style.zIndex = oTemp.style.zIndex;
  oAnimate(oDrag, arr, 300,
  function() {
   document.body.removeChild(oTemp);
  });
  oDrag.releaseCapture && oDrag.releaseCapture()
  };

this.setCapture && this.setCapture();
  return false
 }
 }
 function oAnimate(obj, params, time, handler) {
 var node = typeof obj == "string" ? $(obj) : obj;
 var _style = node.currentStyle ? node.currentStyle: window.getComputedStyle(node, null);
 var handleFlag = true;
 for (var p in params) { (function() {
  var n = p;
  if (n == "left" || n == "top") {
   var _old = parseInt(_style[n]);
   var _new = parseInt(params[n]);
   var _length = 0,
   _tt = 10;
   if (!isNaN(_old)) {
   var count = _old;
   var length = _old <= _new ? (_new - _old) : (_old - _new);
   var speed = length / time * _tt;
   var flag = 0;
   var anim = setInterval(function() {
    node.style[n] = count + "px";
    count = _old <= _new ? count + speed: count - speed;
    flag += _tt;
    if (flag >= time) {
    node.style[n] = _new + "px";
    clearInterval(anim);
    if (handleFlag) {
     handler();
     handleFlag = false;
    }
    }
   },
   _tt);
   }

}
  })();
 }
 }
</script>
</body>
</html>

来源:http://www.qdfuns.com/notes/41982/f95a9c5f1097adc9c56fd1b1959857c7.html

标签:js,拖动
0
投稿

猜你喜欢

  • python脚本之一键移动自定格式文件方法实例

    2021-08-13 09:41:55
  • 在命令行用 sort 进行排序的方法

    2023-02-23 07:07:38
  • JavaScript字符串对象substr方法入门实例(用于截取字符串)

    2024-06-05 09:53:44
  • Python实现根据Excel生成Model和数据导入脚本

    2022-01-10 09:58:12
  • Oracle数据表分区的策略

    2010-07-28 12:59:00
  • python如何实现MK突变检验方法,代码复制修改可用

    2022-04-10 13:31:18
  • Python实现连接dr校园网示例详解

    2022-09-20 05:00:45
  • Python爬虫分析微博热搜关键词的实现代码

    2022-11-29 16:13:44
  • 使用Python为中秋节绘制一块美味的月饼

    2023-06-30 17:36:10
  • MySQL批量插入数据脚本

    2024-01-16 13:31:14
  • 如何利用python写GUI及生成.exe可执行文件

    2023-06-26 00:42:57
  • asp利用xmlhttp抓取特定网页内容例子

    2008-10-10 12:58:00
  • JSON.stringify转换JSON时日期时间不准确的解决方法

    2014-07-20 13:25:07
  • Python Selenium异常处理的实例分析

    2021-01-12 17:13:06
  • php使用curl获取https请求的方法

    2023-11-17 11:46:49
  • python关键字传递参数实例分析

    2023-08-24 04:28:34
  • Oracle故障处理Rman-06207&Rman-06214的方法

    2023-07-08 01:26:29
  • django框架模板中定义变量(set variable in django template)的方法分析

    2021-11-18 03:28:04
  • ASP.NET Core中的Configuration配置一

    2024-06-05 09:32:59
  • ajax+node+request爬取网络图片的实例(宅男福利)

    2024-05-05 09:21:11
  • asp之家 网络编程 m.aspxhome.com