美文网首页
鼠标拖拽4

鼠标拖拽4

作者: 虎三呀 | 来源:发表于2018-02-08 11:25 被阅读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");
                var box2 = document.getElementById("box2");
                var img1 = document.getElementById("img1");
                
                //开启box1的拖拽
                drag(box1);
                //开启box2的
                drag(box2);
                
                drag(img1);
                
                
                
                
            };
            
            /*
             * 提取一个专门用来设置拖拽的函数
             * 参数:开启拖拽的元素
             */
            function drag(obj){
                //当鼠标在被拖拽元素上按下时,开始拖拽  onmousedown
                obj.onmousedown = function(event){
                    
                    //设置box1捕获所有鼠标按下的事件
                    /*
                     * setCapture()
                     *  - 只有IE支持,但是在火狐中调用时不会报错,
                     *      而如果使用chrome调用,会报错
                     */
                    /*if(box1.setCapture){
                        box1.setCapture();
                    }*/
                    obj.setCapture && obj.setCapture();
                    
                    
                    event = event || window.event;
                    //div的偏移量 鼠标.clentX - 元素.offsetLeft
                    //div的偏移量 鼠标.clentY - 元素.offsetTop
                    var ol = event.clientX - obj.offsetLeft;
                    var ot = event.clientY - obj.offsetTop;
                    
                    
                    //为document绑定一个onmousemove事件
                    document.onmousemove = function(event){
                        event = event || window.event;
                        //当鼠标移动时被拖拽元素跟随鼠标移动 onmousemove
                        //获取鼠标的坐标
                        var left = event.clientX - ol;
                        var top = event.clientY - ot;
                        
                        //修改box1的位置
                        obj.style.left = left+"px";
                        obj.style.top = top+"px";
                        
                    };
                    
                    //为document绑定一个鼠标松开事件
                    document.onmouseup = function(){
                        //当鼠标松开时,被拖拽元素固定在当前位置   onmouseup
                        //取消document的onmousemove事件
                        document.onmousemove = null;
                        //取消document的onmouseup事件
                        document.onmouseup = null;
                        //当鼠标松开时,取消对事件的捕获
                        obj.releaseCapture && obj.releaseCapture();
                    };
                    
                    /*
                     * 当我们拖拽一个网页中的内容时,浏览器会默认去搜索引擎中搜索内容,
                     *  此时会导致拖拽功能的异常,这个是浏览器提供的默认行为,
                     *  如果不希望发生这个行为,则可以通过return false来取消默认行为
                     * 
                     * 但是这招对IE8不起作用
                     */
                    return false;
                    
                };
            }
            
            
        </script>
    </head>
    <body>
        
        我是一段文字
        
        <div id="box1"></div>
        
        <div id="box2"></div>
        
        <img src="img/an.jpg" id="img1" style="position: absolute;"/>
    </body>
</html>

相关文章

  • js实现拖拽

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

  • 鼠标拖拽4

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

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

  • 鼠标拖拽

  • 鼠标拖拽

  • 鼠标拖拽

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

  • 鼠标拖拽

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

  • 拖拽,碰撞检测

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

  • selenium常用元素操作(二)

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

  • JavaScript限定范围拖拽及自定义滚动

    学习笔记:拖拽div要发生三个事件: 鼠标按下onmousedown; 鼠标移动onmousemove; 鼠标松开...

网友评论

      本文标题:鼠标拖拽4

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