事件机制(高级)

2018-11-18  本文已影响0人  致自己_cb38

1、事件

click
dbclick
mouseover
mouseout
mouseenter
mouseleave
onload
change
blur
input
submit
focus
scroll
resize

2、 如果对一个标签设置了点击事件之后,没有效果?

3、document是整个文档的父级。

event对象是事件发生的时候产生的一个对象。但是FF不支持。

4、获取一个兼容的event对象。

5、封装的鼠标点击位置的坐标获取方法。

function getPos(ev){
    var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft ||   document.body.scrollLeft;
    var scrollTop = window.pageYOffset || document.documentElement.scrollTop ||     document.body.scrollTop;
    return {
        left:scrollLeft+ev.clientX,
        top:scrollTop + ev.clientY
    };
}

6、事件流

事件冒泡:事件冒泡就是内层元素的事件会随着文档流向上冒泡,如果上层具有相同的事件,那么这个事件也会响应。直到冒泡到顶层对象document。

event.cancelBubble = true; //停止冒泡

eg:仿select

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        div{
            width:400px;
            height:200px;
            background: #ccc;
            display: none;
        }
    </style>
</head>
<body>
    <button>选择</button>
    <div></div>
</body>
<script>
    var btn = document.getElementsByTagName('button')[0];
    var div = document.getElementsByTagName('div')[0];
    btn.onclick = function(ev){
        var ev = ev||event;
        div.style.display = 'block';
        ev.cancelBubble = true;
    };
    document.onclick = function(){
        div.style.display = 'none';
    };
</script>
</html>

7、 键盘事件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        div{
            width:50px;
            height:50px;
            background: green;
            position: absolute;
        }
        span{
            display: block;
            background: red;
            height:20px;
            width:20px;
            border-radius: 20px;
            display:none;
            position: absolute;
        }
    </style>
</head>
<body>
    <div></div>
<script>
    var div = document.querySelector('div');
    var is_left = false;
    var is_right = false;
    var is_top = false;
    var is_bottom = false;
    document.onkeydown = function(ev){
        var ev = ev || event;
        switch(ev.keyCode){
            case 37:
                is_left = true;
                break;
            case 38:
                is_top = true;
                break;
            case 39:
                is_right = true;
                break;
            case 40:
                is_bottom = true;
                break;
            case 32:
                var span = document.createElement('span');
                //子弹的初始位置
                span.style.display = 'block';
                span.style.left =( div.offsetLeft + div.offsetWidth/2) - 10 + 'px'
                span.style.top = div.offsetTop - 20 + 'px'
                document.body.appendChild(span);
                break;
            default:
                break;
        }
    };
    document.onkeyup = function(){
        is_left = is_bottom = is_top = is_right = false;
    }
    setInterval(function(){
        //按键移动
        if(is_left){
            div.style.left = div.offsetLeft - 10 + 'px';
        }
        if(is_top){
            div.style.top = div.offsetTop - 10 + 'px';
        }
        if(is_right){
            div.style.left = div.offsetLeft + 10 + 'px';
        }
        if(is_bottom){
            div.style.top = div.offsetTop + 10 + 'px';
        }
        var s = document.getElementsByTagName('span');
        //子弹移动
        for(var i =0;i<s.length;i++){
            s[i].style.top = s[i].offsetTop - 2 + 'px';
        }
    },30);
</script>
</body>
</html>

eg:只能输入数字的输入框

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    电话号码:<input type="text">
</body>
<script>
    var oin = document.querySelector('input');
    oin.onkeydown = function(ev){
        var ev = ev||event;
        if((ev.keyCode<48 || ev.keyCode>57) && ev.keyCode!=8){
            return false;
        }
    };
</script>
</html>

8、鼠标右键的菜单事件 on contextmenu

9、return false 或preventDefault()能够阻止事件的默认行为。

10、拖拽事件

复合事件:鼠标按下——鼠标移动——鼠标放开
eg:

var  d = document.querySelector('div');
    d.onmousedown = function(ev){
        var ev = ev || event;
        var x = ev.clientX - d.offsetLeft;
        var y = ev.clientY-d.offsetTop;
        d.onmousemove = function(ev){
            var ev = ev || event;
            d.style.left = ev.clientX - x + 'px';
            d.style.top = ev.clientY - y + 'px';
        };
        d.onmouseup = function(){
            d.onmousemove = function(){};
        };
    };

11、事件绑定:给元素添加一个事件。

div.onclick = function(){};

12、怎么去添加多个相同事件事件。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

</body>
<script>
    //对象  事件  函数  是否捕获
    function myEventListener(obj,e,func,is_capture){
        if(obj.attachEvent){
            obj.attachEvent('on'+e,func);
        }else{
            if(is_capture){
                obj.addEventListener(e,func,is_capture);
            }else{
                obj.addEventListener(e,func);
            }
        }
    }
    myEventListener(document,'click',fun1);
    myEventListener(document,'click',function(){
        console.log(2);
    });
    function fun1(){
        console.log(1);
        console.log(this);
    }
    fun1(); //1 window
</script>
</html>

13、setCapture可以将鼠标事件锁定在指定的元素上,当元素捕获了鼠标事件后,该事件只能作用在当前元素上。

案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .outer{
            width:800px;
            height:400px;
            margin:100px;
            background: #ccc;
            position: relative;
            left:50px;
        }
        div div{
            width:100px;
            height:100px;
            background: red;
            position: absolute;
        }
    </style>
</head>
<body>
    <div class="outer">
        <div></div>
    </div>
</body>
<script>
    var div = document.getElementsByClassName('outer')[0].children[0];
    var outer =  document.getElementsByClassName('outer')[0];
    outer.onmousemove = function(ev){
        var ev = ev || event;
        var pos = getPos(ev);
        //元素到边框的距离
        var l = pos.left - this.offsetLeft;
        var t = pos.top- this.offsetTop;
        //判断元素到边框的边界值
        if(l>=parseInt(getStyle(this,"width"))-div.offsetWidth){
            l = parseInt(getStyle(this,"width"))-div.offsetWidth;
        }
        if(t>=parseInt(getStyle(this,"height"))-div.offsetHeight){
            t = parseInt(getStyle(this,"height"))-div.offsetHeight;
        }
        div.style.left =  l+ 'px';
        div.style.top = t+'px';
    };
    function getStyle(obj,style){
            if(obj.currentStyle){
                return obj.currentStyle[style];
            }else{
                return getComputedStyle(obj,null)[style];
            }
        }
    function getPos(ev){
        var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft ||   document.body.scrollLeft;
        var scrollTop = window.pageYOffset || document.documentElement.scrollTop ||     document.body.scrollTop;
        return {
            left:scrollLeft+ev.clientX,
            top:scrollTop + ev.clientY
        };
    }
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>

            div{
                width:20px;
                height:20px;
                border-radius: 20px;
                background: red;
                position: absolute;
            }
    </style>
</head>
<body>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</body>
<script>
var div = document.getElementsByTagName('div');
document.onmousemove = function(ev){
    var ev = ev||event;
    for(var i=div.length-1;i>=0;i--){
        if(i==0){
            //获取第一个小圆的位置
            div[i].style.left= getPos(ev).left +'px';
            div[i].style.top= getPos(ev).top+'px';
        }else{
            //获取每个小圆的位置
            div[i].style.left= parseInt(div[i-1].offsetLeft) +'px';
            div[i].style.top= parseInt(div[i-1].offsetTop)+'px';
        }
    }
};
function getPos(ev){
    var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft ||   document.body.scrollLeft;
    var scrollTop = window.pageYOffset || document.documentElement.scrollTop ||     document.body.scrollTop;
    return {
        left:scrollLeft+ev.clientX,
        top:scrollTop + ev.clientY
    };
}
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        div{
            position: absolute;
            width:100px;
            height:100px;
            border-radius: 100px;
            background:cyan;
        }
    </style>
</head>
<body>
    dnasjkndjnasdlsajl
    <div>ahdhawdiuqhwui</div>
    dsanbhjdbjkashui
</body>
<script>
    var div = document.querySelector('div');
    //拖拽
    div.onmousedown = function(ev){
         var ev =  ev || event;
        //计算鼠标点击的位置相对于目标的位置
        var x = ev.clientX - div.offsetLeft;
        var y = ev.clientY - div.offsetTop;
        //判断元素是否捕捉
        var dest = div.setCapture ? div : document;
        dest.onmousemove = mousemove;
        dest.onmouseup = mouseup;
        function mousemove(ev){
            var ev = ev || event;
            //获取元素相对于浏览器的距离
            var left =  ev.clientX - x;
            var top = ev.clientY - y;
            //偏正超出范围
            if(left<0){
                left = 0;
            }else if(left>window.innerWidth-div.offsetWidth){
                left = window.innerWidth-div.offsetWidth;
            }
            if(top<0){
                top = 0;
            }else if(top>window.innerHeight-div.offsetHeight){
                top = window.innerHeight-div.offsetHeight;
            }
            div.style.left = left + 'px';
            div.style.top = top + 'px';
            if(div.setCapture){
                div.setCapture();
            }
        };
        function mouseup(){
            this.onmousemove = null;
            this.onmouseup = null;
        }
        return false;
    };
</script>
</html>
上一篇 下一篇

猜你喜欢

热点阅读