鼠标拖拽2

<!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");
                var box2 = document.getElementById("box2");
                //为box1绑定一个鼠标按下事件
                //当鼠标在被拖拽元素上按下时,开始拖拽  onmousedown
                box1.onmousedown = function(event){
                    
                    //设置box1捕获所有鼠标按下的事件
                    /*
                     * setCapture()
                     *  - 只有IE支持,但是在火狐中调用时不会报错,
                     *      而如果使用chrome调用,会报错
                     */
                    /*if(box1.setCapture){
                        box1.setCapture();
                    }*/
                    box1.setCapture && box1.setCapture();
                    
                    
                    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;
                        //当鼠标松开时,取消对事件的捕获
                        box1.releaseCapture && box1.releaseCapture();
                    };
                    
                    /*
                     * 当我们拖拽一个网页中的内容时,浏览器会默认去搜索引擎中搜索内容,
                     *  此时会导致拖拽功能的异常,这个是浏览器提供的默认行为,
                     *  如果不希望发生这个行为,则可以通过return false来取消默认行为
                     * 
                     * 但是这招对IE8不起作用
                     */
                    return false;
                    
                };
                
                
                
            };
            
            
        </script>
    </head>
    <body>
        
        我是一段文字
        
        <div id="box1"></div>
        
        <div id="box2"></div>
    </body>
</html>

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • gitDemo远程操作演练 1.将本地仓库与远程仓库进行合并 git@github.com:longxianwen...
    coderWen阅读 3,304评论 0 1
  • 友谊的小船说翻就翻。
    今天我们不学习阅读 503评论 0 0
  • 一.BLE简介 1.BLE是BluetoothLowEnergy的缩写,又叫蓝牙4.0,主要用于实现移动智能终端与...
    风过才懂阅读 10,825评论 0 0
  • 雪儿离婚了。 谁也没想到雪儿会离婚。看上去,她的婚姻很幸福,老公虽然说经常出差,不常回家,可是收入挺高的...
    a子诺a阅读 3,680评论 0 0
  • 想接触人,却害怕人群。 在人群中感到眩晕,却喜欢看人来人往。 在人多的城市感到安心, 虽然不想和具体的人产生实质的...
    奚所以阅读 2,994评论 0 0

友情链接更多精彩内容