js带有虚线框的拖动效果代码实例

分享一段代码实例,它实现了元素拖动效果。

在拖动的时候具有一个虚线框在移动,当松开鼠标虚线框消失,原来的元素会被放置于此。

代码实例如下:

     
     
[HTML] 纯文本查看 复制代码 运行代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!doctype html>
< html >
< head >
< meta  charset = "utf-8" >
< title >带有虚线框的拖动效果</ title >
< style >
* {
   padding: 0;
   margin: 0;
}
#box1 {
   width: 100px;
   height: 100px;
   text-align:center;
   line-height:100px;
   background: yellow;
   position: absolute;
   cursor: move;
}
#box2 {
   width: 100px;
   height: 100px;
   border: 1px dashed #000000;
   position: absolute;
   cursor: move;
}
</ style >
< script >
window.onload = function() {
   var box1 = document.getElementById("box1");
   box1.onmousedown = function(event) {
     var e = event || window.event;
     var disX = e.clientX - box1.offsetLeft;
     var disY = e.clientY - box1.offsetTop;
     var maxL = document.documentElement.clientWidth - box1.offsetWidth;
     var maxT = document.documentElement.clientHeight - box1.offsetHeight;
     //创建一个新的div
     var box2 = document.createElement("div");
     box2.id = "box2";
     box2.style.left = box1.offsetLeft + "px";
     box2.style.top = box1.offsetTop + "px";
     document.documentElement.appendChild(box2);
     document.onmousemove = function (event) {
       var e = event || window.event;
       var l = e.clientX - disX;
       var t = e.clientY - disY;
       if (l <= 0) {
         l = 0;
       }
       if (t <= 0) {
         t = 0;
       }
       if (l >= maxL) {
         l = maxL;
       }
       if (t >= maxT) {
         t = maxT
       }
       box2.style.left = l + "px";
       box2.style.top = t + "px";
     };
     box2.onmouseup = function() {
       box1.style.left = box2.style.left;
       box1.style.top = box2.style.top;
       document.documentElement.removeChild(box2);
       box2.onmousemove = null;
       box2.onmouseup = null;
     };
   }
}
</ script >
</ head >
< body >
< div  id = "box1" >拖动块</ div >
</ body >
</ html >

上面的代码实现了我们的要求,下面介绍一下它的实现过程。

一.代码注释:

(1).window.onload = function() {},文档内容完全加载完毕再去执行函数中的代码。

(2).var box1 = document.getElementById("box1"),获取id属性值为box1的元素对象。

(3).box1.onmousedown = function(event) {},为box1元素注册onmousedown事件处理函数。

(4).var e = event || window.event,兼容所有浏览器的时间对象。

(5).var disX = e.clientX - box1.offsetLeft,获取当前鼠标指针距离元素左侧的距离。

(6).var disY = e.clientY - box1.offsetTop,获取当前鼠标指针距离元素上侧的距离。(7).var maxL = document.documentElement.clientWidth - box1.offsetWidth,元素可以向右拖动的最大值。

(8).var maxT = document.documentElement.clientHeight - box1.offsetHeight,元素可以向上拖动的最大值。

(9).var box2 = document.createElement("div"),创建虚线框div元素。

(10). box2.id = "box2",设置虚线框div元素的id属性值。

(11).box2.style.left = box1.offsetLeft + "px",设置box2元素的left属性值

(12).box2.style.top = box1.offsetTop + "px",设置box2的top属性值。

(13). document.documentElement.appendChild(box2),将此box2添加的页面。

(14).document.onmousemove = function (event) {

  var e = event || window.event;

  var l = e.clientX - disX;

  var t = e.clientY - disY;

  if (l <= 0) {

    l = 0;

  }

  if (t <= 0) {

    t = 0;

  }

  if (l >= maxL) {

    l = maxL;

  }

  if (t >= maxT) {

    t = maxT

  }

  box2.style.left = l + "px";

  box2.style.top = t + "px";

},将box2元素的可以拖动范围限定在浏览器客户区。

之所以将onmousemove 事件处理函数注册在document上是为了防止快速拖动导致鼠标脱离元素,失去拖动效果。

(15).box2.onmouseup = function() {

  box1.style.left = box2.style.left;

  box1.style.top = box2.style.top;

  document.documentElement.removeChild(box2);

  box2.onmousemove = null;

  box2.onmouseup = null;

},当鼠标松开的时候,将box1的位置设置为当前box2的位置。

然后删除box2,并解绑事件处理函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值