一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語(yǔ)言 - JavaScript - js教程 - js實(shí)現(xiàn)碰撞檢測(cè)

js實(shí)現(xiàn)碰撞檢測(cè)

2022-01-11 16:03搬磚大法 js教程

這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)碰撞檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js實(shí)現(xiàn)碰撞檢測(cè)的具體代碼,供大家參考,具體內(nèi)容如下

js實(shí)現(xiàn)碰撞檢測(cè)

js實(shí)現(xiàn)碰撞檢測(cè)

代碼:

?
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
<!DOCTYPE html>
<html lang="en">
 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  div {
    position: absolute;
    top: 0px;
    right: 0px;
    bottom: 0px;
    left: 0px;
    margin: auto;
    width: 300px;
    height: 300px;
    background-color: green;
  }
  
  span {
    position: absolute;
    top: 0px;
    left: 0px;
    display: block;
    width: 100px;
    height: 100px;
    background-color: rgb(10, 151, 233);
  }
</style>
 
<body>
  <div></div>
  <span></span>
  <script>
    var div = document.getElementsByTagName('div')[0];
    var span = document.getElementsByTagName('span')[0];
    span.onmousedown = function(e) {
      // 事件對(duì)象兼容
      e = window.event || e;
      // 添加全局捕獲
      if (span.setCapture) {
        span.setCapture();
      }
      // 鼠標(biāo)按下獲取鼠標(biāo)距離頁(yè)面左側(cè)和頂部距離
      var x = e.clientX;
      var y = e.clientY;
      // 元素距離頁(yè)面左側(cè)和頂部距離
      var elex = span.offsetLeft;
      var eley = span.offsetTop;
      // 鼠標(biāo)距離元素距離 =鼠標(biāo)距離頁(yè)面距離 -元素距離頁(yè)面距離
      var X = x - elex;
      var Y = y - eley;
      document.onmousemove = function(e) {
        // 鼠標(biāo)移動(dòng) 獲取鼠標(biāo)距離頁(yè)面距離
        // 事件對(duì)象兼容
        e = window.event || e;
        var movex = e.clientX;
        var movey = e.clientY;
        // 元素的left和top值 =鼠標(biāo)距離頁(yè)面距離 -鼠標(biāo)距離元素距離
        var leftx = movex - X;
        var lefty = movey - Y;
        /*----------------------------------------------------------*/
        // 碰撞檢測(cè)
        // 1.左側(cè)安全距離 =大盒子距離頁(yè)面左側(cè)距離 -小盒子占位寬
        var safeleft = div.offsetLeft - span.offsetWidth;
        // 2.右側(cè)安全距離 大盒子距離頁(yè)面左側(cè)距離 +大盒子占位寬
        var saferight = div.offsetLeft + div.offsetWidth;
        // 3.上側(cè)安全距離 =大盒子距離頁(yè)面頂部距離 -小盒子占位高
        var safetop = div.offsetTop - span.offsetHeight;
        // 4. 下側(cè)安全距離 = 大盒子距離頁(yè)面頂部距離 + 大盒子占位高
        var safebottom = div.offsetTop + div.offsetHeight;
 
        if (leftx < safeleft || leftx > saferight || lefty < safetop || lefty > safebottom) {
          div.style.background = 'green';
        } else {
          div.style.background = 'red';
        }
 
        /*----------------------------------------------------------*/
 
        // 邊界值
        // 左
        if (leftx <= 0) {
          leftx = 0;
        }
        // 上
        if (lefty <= 0) {
          lefty = 0;
        }
        // 右
        var rightx = document.documentElement.clientWidth - span.offsetWidth;
        if (leftx >= rightx)
          leftx = rightx;
        // 下
        var righty = document.documentElement.clientHeight - span.offsetHeight;
        if (lefty >= righty) {
          lefty = righty;
        }
 
 
        span.style.left = leftx + 'px';
        span.style.top = lefty + 'px';
      }
      document.onmouseup = function() {
 
          document.onmousemove = null;
          if (span.releaseCapture) {
            span.releaseCapture();
          }
 
 
        }
        // 阻止默認(rèn)事件
      return false;
    }
  </script>
</body>
 
</html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。 

原文鏈接:https://blog.csdn.net/are_gh/article/details/113244417

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 好深快点再快点好爽视频 | 亚洲国产精品自在在线观看 | 毛片在线播放a | 国内精品福利丝袜视频_速 国内精品91久久久久 | 99年水嫩漂亮粉嫩在线播放 | 国产精品久久久久jk制服 | 波多野结衣一区免费作品 | 美女被灌浣肠失禁视频 | 99r视频| 久久精品亚洲热综合一本 | ova巨公主催眠1在线观看 | 日韩欧美一区二区三区中文精品 | 免费免费啪视频在线观播放 | 91久久精品国产一区二区 | 2020国产精品视频 | 久久91精品国产91 | 国产精品亚洲片在线观看麻豆 | 女教师雪白老汉 | 国产精品夜夜爽张柏芝 | 国产欧美成人不卡视频 | 久久久久久久久a免费 | 亚洲国产免费 | 日本手机在线视频 | 精品香蕉99久久久久网站 | 日本免费全黄一级裸片视频 | 我将她侵犯1~6樱花动漫在线看 | 国产一区私人高清影院 | 97久久天天综合色天天综合色hd | 欧美日韩亚洲国内综合网香蕉 | 成人国产午夜在线视频 | 欧美日韩中文字幕久久伊人 | 国产成+人+综合+亚洲欧美丁香花 | 精品欧美一区二区三区久久久 | 女bbbxxx毛片视频 | 日本老妇乱子伦中文视频 | 香蕉精品高清在线观看视频 | 亚洲精品国产精品精 | 欧美色图亚洲 | 99re精品在线 | 日本高清免费中文字幕不卡 | 精品欧美一区二区三区在线观看 |