zoukankan      html  css  js  c++  java
  • onmousedown活用之碰撞效果

    通过绝对定位,在页面中随意位置设置两个div;

    也就是说div 是拖动的框,div1和div2是被触碰的框;

     1 <!DOCTYPE html>
     2 <html>
     3 
     4 <head>
     5     <meta charset="UTF-8">
     6     <title></title>
     7     <style type="text/css">
     8         #div {
     9             width: 100px;
    10             height: 100px;
    11             background: pink;
    12             position: absolute;
    13             top: 50px;
    14             left: 60px;
    15             cursor: pointer;
    16         }
    17         
    18         #div1 {
    19             width: 100px;
    20             height: 100px;
    21             border: 1px solid black;
    22             position: absolute;
    23             top: 300px;
    24             left: 100px;
    25         }
    26         
    27         #div2 {
    28             width: 100px;
    29             height: 100px;
    30             border: 1px solid blue;
    31             position: absolute;
    32             top: 200px;
    33             left: 600px;
    34         }
    35     </style>
    36 </head>
    37 
    38 <body>
    39     <div id="div"></div>
    40     <div id="div1"></div>
    41     <div id="div2"></div>
    42     
    43 </body>
    44 
    45 </html>

    所有的script是在写在body里面的,

     1 <script type="text/javascript">
     2         var Div = document.getElementById("div");
     3         var Div1 = document.getElementById("div1");
     4         var Div2 = document.getElementById("div2");
     5 
     6         //第一个固定框的上下左右的值
     7         var top1 = parseInt(getStyle(div1, "top"));
     8         var bottom1 = parseInt(getStyle(div1, "top")) + 100;
     9         var left1 = parseInt(getStyle(div1, "left"));
    10         var right1 = parseInt(getStyle(div1, "left")) + 100;
    11 
    12         //第二个固定框的上下左右的值
    13         var top2 = parseInt(getStyle(div2, "top"));
    14         var bottom2 = parseInt(getStyle(div2, "top")) + 100;
    15         var left2 = parseInt(getStyle(div2, "left"));
    16         var right2 = parseInt(getStyle(div2, "left")) + 100;
    17         //alert(top1);
    18         var red1 = "blue";
    19         var red2 = "red";
    20 
    21         Div.onmousedown = function(ev) {
    22             var o = event || ev;
    23             //获取到鼠标点击的位置距离div左侧和顶部边框的距离
    24             oX = o.clientX - parseInt(getStyle(Div, "left"));
    25             oY = o.clientY - parseInt(getStyle(Div, "top"));
    26             //当鼠标移动,把鼠标的偏移量给div
    27             document.onmousemove = function(ev) {
    28                 var o = event || ev;
    29                 //计算出鼠标在XY方向上移动的偏移量,把这个偏移量加给DIV的左边距和上边距,div就会跟着移动
    30                 Div.style.left = o.clientX - oX + "px";
    31                 Div.style.top = o.clientY - oY + "px";
    32 
    33                 var left = parseInt(getStyle(div, "left"));
    34                 var right = parseInt(getStyle(div, "left")) + 100;
    35                 var top = parseInt(getStyle(div, "top"));
    36                 var bottom = parseInt(getStyle(div, "top")) + 100;
    37 
    38 
    39                 //第鼠标框的bottom值小于第二个框的top1值
    40                 //第鼠标框的left值大于第二个框的right1值
    41                 //第鼠标框的top值大于第二个框的bottom1值
    42                 //第鼠标框的right值小于第二个框的left1值
    43                 //当这些都满足的时候,说明第鼠标框没有触碰第二个框,所以背景色不变
    44                 //否则,就是触动框了,背景色变
    45                 if (bottom < top1 || left > right1 || top > bottom1 || right < left1) {
    46                     Div1.style.background = "";
    47                 } else {
    48                     Div1.style.background = red1;
    49                 }
    50 
    51                 if (bottom < top2 || left > right2 || top > bottom2 || right < left2) {
    52                     Div2.style.background = "";
    53                 } else {
    54                     Div2.style.background = red2;
    55                 }
    56 
    57             }
    58 
    59 
    60             //当鼠标按键抬起,清除移动事件
    61             document.onmouseup = function() {
    62                 document.onmousedown = null;
    63                 document.onmousemove = null;
    64             }
    65 
    66         }
    67 
    68         //获取属性的数值
    69         function getStyle(obj, attr) {
    70             if (obj.currentStyle) {
    71                 //currentStyle获取样式的值,对应的是ie浏览器
    72                 return obj.currentStyle[attr];
    73             } else {
    74                 //同理,对应的是其他浏览器
    75                 return getComputedStyle(obj, false)[attr];
    76             }
    77         }
    78     </script>
  • 相关阅读:
    js基础
    linux 权限计算
    postman 测试http post的json请求
    Crontab 让linux定时执行shell脚本
    Java:扫描包含图片的文件夹,将符合分辨率格式的复制出来
    php引用其他目录的php文件
    电脑屏幕动图制作之-----GifCam
    通过Excel表创建sql脚本
    通过Navicat将Excel表中的数据导入到数据库
    需求设计之初造火箭?
  • 原文地址:https://www.cnblogs.com/WhiteM/p/6089055.html
Copyright © 2011-2022 走看看