服务器之家:专注于服务器技术及软件下载分享
分类导航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服务器之家 - 编程语言 - JavaScript - jquery实现拖拽小方块效果

jquery实现拖拽小方块效果

2021-12-09 15:08赚钱养个快乐小智障 JavaScript

这篇文章主要为大家详细介绍了jquery实现拖拽小方块效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

今天来讲一个很常用的拖拽功能,主要是利用一点css,js和jquery,很简单但同时也很重要,掌握好才是最关键的!

下面来看下效果图:

这里可以看到,在鼠标hover的时候会有一个透明度的变化和一个盒子阴影!并且在右下角会实时的显示出小方块移动时横纵坐标的变化!

jquery实现拖拽小方块效果

可以看到有一个盒子阴影

jquery实现拖拽小方块效果

在鼠标单击按住的时候会变红,然后可以拖动小块随意移动

jquery实现拖拽小方块效果

我们在看代码之前可以先了解下jquery中的基本知识(选择器,css控制器什么的)和offset()clientX,clientY。思路很简单,就是分别监听鼠标的 mousedown,mouseup,mousemove这三个事件,通过判断div移动之前的坐标,div移动之后的坐标(offset获得)和鼠标移动前后的坐标(clientX和clientY获得)判断div在body里的具体坐标位置,然后设置div在body里左边和上边的距离(left,top)。代码思路在代码里很详细!希望大家好好理解!

附上代码:

?
1
2
3
4
5
6
7
8
9
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html>
<html lang="en">
 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>拖动图片</title>
  <style>
    html {
      height: 100%;
    }
    
    body {
      margin: 0px;
      padding: 0rem;
      border: 0rem;
      height: 100%;
      width: 100%;
      position: relative;
      /* 取消默认的输入事件,不然会一直出现‘I'一样的光标 */
      -webkit-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none;
    }
    
    #drag {
      height: 200px;
      width: 200px;
      background: teal;
      border: none;
      border-radius: 1rem;
      /* 一定要是absolute */
      position: absolute;
      opacity: 0.8;
    }
    
    #drag:hover {
      box-shadow: 0 8px 12px 0 rgba(16, 49, 231, 0.4);
      opacity: 1;
      cursor: pointer;
    }
    
    input {
      width: 12rem;
      height: 2rem;
      font-size: 1.5rem;
      border: 2px solid #aaa;
    }
    
    #input1 {
      display: block;
      position: absolute;
      bottom: 1.2rem;
      right: 1rem;
    }
    
    #input2 {
      display: block;
      position: absolute;
      bottom: 1.2rem;
      right: 16rem;
    }
  </style>
</head>
 
<body>
 
  <div id="drag">
 
  </div>
  <input type="text" id="input1" name="y" placeholder="y轴的坐标为">
  <input type="text" id="input2" name="x" placeholder="x轴的坐标为">
 
  <!-- 引入内部jquery,大家使用可以引入CDN -->
  <script src="jquery-3.5.1.min.js"></script>
  <script>
    var client_x = 0;
    var client_y = 0;
    var offset_x = 0;
    var offset_y = 0;
    var moving = false;
    // 利用jquery获得div这个元素
    var drag = $('#drag');
    // 添加监听事件
    drag.on({
      // 鼠标抬起事件
      mouseup: function(e) {
        moving = false;
        // 为div添加一个css样式
        $("#drag").css("background", 'teal');
      },
      // 鼠标按下事件
      mousedown: function(e) {
        moving = true;
        // this代表的是 div
        var offset = $(this).offset();
        // offset() 方法设置或返回被选元素相对于文档的偏移坐标
        offset_x = offset.left;
        offset_y = offset.top;
        // clientX 事件属性返回当事件被触发时鼠标指针相对于浏览器页面的水平坐标
        client_x = e.clientX;
        // clientX 事件属性返回当事件被触发时鼠标指针相对于浏览器页面的垂直坐标
        client_y = e.clientY;
        drag.css("background", 'rgb(179, 43, 19)');
      },
      // 鼠标移动事件
      mousemove: function(e) {
        if (moving) {
          // 为div添加一个css样式
          drag.css({
            left: offset_x + (e.clientX - client_x),
            top: offset_y + (e.clientY - client_y)
          });
          // 设置并显示input标签内x,y轴的坐标
          $(':input[name="x"]').val(offset_x + (e.clientX - client_x));
          $(':input[name="y"]').val(offset_y + (e.clientY - client_y));
          drag.css("cursor", "pointer");
        }
      }
    });
  </script>
</body>
 
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_45277161/article/details/110940129

延伸 · 阅读

精彩推荐