JS事件:onmouseover与onmouseout多次触发

初学javascript就碰见一个棘手的问题,真是够折腾的。
作业目标是做一个可以缓动的下拉导航栏;
html与css代码自然没有任何的问题

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0px;
                padding: 0px;
                border: 0 none;
            }
            ul,li{
                list-style: none;
                overflow: hidden;
            }
            body>ul>li{
                float: left;
                text-align: center;
            }
            a{
                display: block;
                width: 90px;
                height: 30px;
                background-color: #232323;
                color: #fff;
                text-decoration: none;
                line-height: 30px;
            }
            ul ul li{
                display: block;
                width: 90px;
                height: 30px;
                background-color: #808080;
                color: #fff;
                cursor: pointer;
            }
            ul ul{
                display: none;
            }
        </style>
    </head>
    <body>
        <ul>
            <li>
                <a href="#">顶级菜单1</a>
                <ul>
                    <li>子菜单11</li>
                    <li>子菜单12</li>
                    <li>子菜单13</li>
                </ul>
            </li>
            <li>
                <a href="#">顶级菜单2</a>
                <ul>
                    <li>子菜单21</li>
                    <li>子菜单22</li>
                    <li>子菜单23</li>
                </ul>
            </li>
            <li>
                <a href="#">顶级菜单3</a>
                <ul>
                    <li>子菜单31</li>
                    <li>子菜单32</li>
                    <li>子菜单33</li>
                </ul>
            </li>
        </ul>
    </body>
</html>

一开始理所当然是用鼠标进入和出去来控制js的onmouseover与onmouseout事件:

<script type="text/javascript">
            //找到顶级菜单并绑定移入事件
            var lis = document.querySelectorAll("body>ul>li");
            for(var i=0; i<lis.length; i++){
                lis[i].onmouseover = function(){
                    //鼠标移入子菜单出现
                    // console.log(this.nextElementSibling);
                    this.children[1].style.display = "block";
                    var inEle = this.children[1];
                    change(inEle);
                    console.log(this);
                };
                
                //绑定鼠标移出事件
                lis[i].onmouseout = function(){
                    this.children[1].style.height = 0;
                    this.children[1].style.display = "none";
                };
            }
            
            
            //让元素缓动
            function change(ele){
                // console.log("1212");
                //重置元素高度
                ele.style.height = 0;
                //经0.36秒变化完毕
                for(var i=1; i<=45; i++){
                    (function(x){
                        setTimeout(function(){
                            // alert("OK");
                            ele.style.height = x*2 + "px";
                            // console.log(x);
                        },x*8);
                    })(i);
                };
            };
        </script>

看起来好像没有问题,但是问题大着呢:
js.gif
这时候就要引出JS事件之onmouseenter与onmouseleave了;

onmouseover、onmouseout:鼠标经过时自身触发事件,经过其子元素时也触发该事件;(父亲有的东西,儿子也有)
onmouseenter、onmouseleave:鼠标经过时自身触发事件,经过其子元素时不触发该事件。(父亲的东西就是父亲的,不归儿子所有)
这四个事件两两配对使用,onmouseover、onmouseout一对,onmouseenter、onmouseleave一对,不能混合使用。

在父元素使用onmouseover、onmouseout时如果父元素中有子元素并进入子元素将会触发onmouseout然后再次触发onmouseover,从子元素进入父元素同理;
而onmouseenter、onmouseleave无论是从父元素进入子元素还是从子元素进入父元素,都将有且只有触发一次onmouseenter、onmouseleave事件;
上面的代码只需要把onmouseover、onmouseout换成onmouseenter、onmouseleave事件就可以达到目的了,有些坑真的不想再踩第二次了。
js2.gif
引用资料:
JS事件之onmouseover 、onmouseout 与onmouseenter 、onmouseleave

评论

暂无评论