JavaScrip--事件应用

事件应用

拖拽

拖拽原理

三大事件
+ onmousedown : 选择元素
+ onmousemove : 移动元素
+ onmouseup   : 释放元素

var oDiv = document.getElementById('div1');

oDiv.onmousedown = function(){
    oDiv.onmousemove = function(){//鼠标移动事件是在鼠标按下事件之后

    }

    oDiv.onmouseup = function (){//鼠标抬起之后要清除鼠标按下事件
        oDiv.onmousemove= null;
        oDiv.onmouseup = null;
    }
}

- 鼠标和Div的相对距离不变

var oDiv = document.getElementById('div1');

oDiv.onmousedown = function(ev){
    var ev = ev || window.event;

    //鼠标和Div的相对距离 
    var disX = ev.clientX - oDiv.offsetLeft;
    var disY = ev.clientY - oDiv.offsetTop;

    oDiv.onmousemove = function(ev){
        var ev = ev || window.event;
        oDiv.style.left = ev.clientX - disX + 'px';
        oDiv.style.top = ev.clientY - disY + 'px';
    }

    oDiv.onmouseup = function (){
        oDiv.onmousemove= null;
        oDiv.onmouseup = null;
    }
}
把拖拽加到document上

如果拖得太快, 会移出div, 可以把onmousemove事件加在document上

注意:移出事件也是在doucment

var oDiv = document.getElementById('div1');

oDiv.onmousedown = function(ev){
    var ev = ev || window.event;
    var disX = ev.clientX - oDiv.offsetLeft;
    var disY = ev.clientY - oDiv.offsetTop;
    document.onmousemove = function(ev){
        var ev = ev || window.event;
        oDiv.style.left = ev.clientX - disX + 'px';
        oDiv.style.top = ev.clientY - disY + 'px';
    }

    oDiv.onmouseup = function (){
        document.onmousemove= null;
        oDiv.onmouseup = null;
    }
}
拖拽的问题

如果拖拽文字或者图片, 由于浏览器的默认行为, 不能正常的拖拽

解决办法: 阻止浏览器默认行为




    
    Document



    



拖拽的封装

两个步骤:

  1. 将拖拽代码全部放进drag函数中
  2. 把需要变化的变量改成参数
function drag(obj){
    obj.onmousedown = function(ev){
        var ev = ev || window.event;
        var disX = ev.clientX - obj.offsetLeft;
        var disY = ev.clientY - obj.offsetTop;
        document.onmousemove = function(ev){
            var ev = ev || window.event;
            obj.style.left = ev.clientX - disX + 'px';
            obj.style.top = ev.clientY - disY + 'px';
            return false;

        }

        obj.onmouseup = function (){
            document.onmousemove= null;
            obj.onmouseup = null;
        }
        return false;
    }

}
限制拖拽的范围

在窗口内拖动

function drag(obj){
    obj.onmousedown = function(ev){
        var ev = ev || window.event;


        var disX = ev.clientX - obj.offsetLeft;
        var disY = ev.clientY - obj.offsetTop;
        document.onmousemove = function(ev){
            var ev = ev || window.event;


            var left  = ev.clientX - disX;
            var top = ev.clientY - disY;
            if(left<0){
                left=0;
            }
            else if(left>document.documentElement.clientWidth-obj.offsetWidth){
                left = document.documentElement.clientWidth-obj.offsetWidth;
            }
            if(top<0){
                top=0;
            }
            else if(top>document.documentElement.clientHeight-obj.offsetHeight){
                top = document.documentElement.clientHeight-obj.offsetHeight;
            }

            obj.style.left =  left + 'px';
            obj.style.top = top + 'px';
            return false;

        }

        obj.onmouseup = function (){
            document.onmousemove= null;
            obj.onmouseup = null;
        }
        return false;
    }

}
磁性吸附效果

比如说以前的一些音乐播放器, 播放列表和歌词是两个分开的面板, 可以分开可以合并
合并的时候不需要精确的拖拽,而是拖拽到两个面板距离很近时自动合并

function drag(obj){
    obj.onmousedown = function(ev){
        var ev = ev || window.event;


        var disX = ev.clientX - obj.offsetLeft;
        var disY = ev.clientY - obj.offsetTop;
        document.onmousemove = function(ev){
            var ev = ev || window.event;


            var left  = ev.clientX - disX;
            var top = ev.clientY - disY;
            if(left<100){//距离可视区域范围的四边小于50px,则元素将直接吸附对应的边上
                left=0;
            }
            else if(left>document.documentElement.clientWidth-obj.offsetWidth){
                left = document.documentElement.clientWidth-obj.offsetWidth;
            }
            if(top<0){
                top=0;
            }
            else if(top>document.documentElement.clientHeight-obj.offsetHeight){
                top = document.documentElement.clientHeight-obj.offsetHeight;
            }

            obj.style.left =  left + 'px';
            obj.style.top = top + 'px';
            return false;

        }

        obj.onmouseup = function (){
            document.onmousemove= null;
            obj.onmouseup = null;
        }
        return false;
    }

}

碰撞检测

js中我们使用的是判断两个物体位置是否重合来判断两个物体是否发生了碰撞

常用的方法是使用九宫格法

JavaScrip--事件应用_第1张图片
碰撞检测.png

拖动div, 如果检测到碰撞, 则改变图片




    
    Document



    

你可能感兴趣的:(JavaScrip--事件应用)