美文网首页
鼠标拖拽1

鼠标拖拽1

作者: 虎三呀 | 来源:发表于2018-02-08 11:24 被阅读0次
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            
            #box1{
                width: 100px;
                height: 100px;
                background-color: red;
                position: absolute;
            }
            
            #box2{
                width: 100px;
                height: 100px;
                background-color: yellow;
                position: absolute;
                
                left: 200px;
                top: 200px;
            }
            
        </style>
        
        <script type="text/javascript">
            
            window.onload = function(){
                /*
                 * 拖拽box1元素
                 *  - 拖拽的流程
                 *      1.当鼠标在被拖拽元素上按下时,开始拖拽  onmousedown
                 *      2.当鼠标移动时被拖拽元素跟随鼠标移动 onmousemove
                 *      3.当鼠标松开时,被拖拽元素固定在当前位置   onmouseup
                 */
                
                //获取box1
                var box1 = document.getElementById("box1");
                //为box1绑定一个鼠标按下事件
                //当鼠标在被拖拽元素上按下时,开始拖拽  onmousedown
                box1.onmousedown = function(event){
                    event = event || window.event;
                    //div的偏移量 鼠标.clentX - 元素.offsetLeft
                    //div的偏移量 鼠标.clentY - 元素.offsetTop
                    var ol = event.clientX - box1.offsetLeft;
                    var ot = event.clientY - box1.offsetTop;
                    
                    
                    //为document绑定一个onmousemove事件
                    document.onmousemove = function(event){
                        event = event || window.event;
                        //当鼠标移动时被拖拽元素跟随鼠标移动 onmousemove
                        //获取鼠标的坐标
                        var left = event.clientX - ol;
                        var top = event.clientY - ot;
                        
                        //修改box1的位置
                        box1.style.left = left+"px";
                        box1.style.top = top+"px";
                        
                    };
                    
                    //为document绑定一个鼠标松开事件
                    document.onmouseup = function(){
                        //当鼠标松开时,被拖拽元素固定在当前位置   onmouseup
                        //取消document的onmousemove事件
                        document.onmousemove = null;
                        //取消document的onmouseup事件
                        document.onmouseup = null;
                    };
                };
                
                
                
                
            };
            
            
        </script>
    </head>
    <body>
        <div id="box1"></div>
        
        <div id="box2"></div>
    </body>
</html>


相关文章

  • js实现拖拽

    ①鼠标按下+鼠标移动 → 拖拽②鼠标松开 → 无拖拽③鼠标偏移 → 拖拽距离 js实现 ① onmousedown...

  • 鼠标拖拽1

  • 拖拽,碰撞检测

    1. 拖拽 1.1 拖拽原理 鼠标拖拽效果的实现,就是在鼠标摁下和移动的时候,修改元素的定位值的效果。 1.1.1...

  • (三)图形的绘制与编辑功能【一个小时】

    1.选择工具 ~从左往右拖拽鼠标:完全框选才被选中 ~从右下往左上拖拽鼠标:接触即被选中 ~左键单击:左键单击选中...

  • web前端-原生鼠标拖拽效果

    鼠标拖拽效果分为3个事件 鼠标按下事件onmousedown, 事件源是点击的对象, 就是我们要拖拽的对象 鼠标移...

  • 鼠标拖拽

  • 鼠标拖拽

  • 鼠标拖拽

    一、效果一个可以在屏幕上任意拖动的红色爱心 二、代码 三、知识点总结3.1、获取dom对象的css伪元素通过win...

  • 鼠标拖拽

    基本思路如下: CSS部分 js部分 源码:

  • selenium常用元素操作(二)

    一、鼠标操作 selenium的ActionChains类实现鼠标的移动右键,鼠标悬停,拖拽,双击等模拟鼠标的操作...

网友评论

      本文标题:鼠标拖拽1

      本文链接:https://www.haomeiwen.com/subject/qdtttftx.html