练手小案例:用js实现二级导航

使用HTML5+CSS3+JS实现简单的二级导航,绝对的干货!

效果图:

练手小案例:用js实现二级导航_第1张图片

HTML部分:

    
    
    

 CSS部分:

*{
    padding: 0;
    margin: 0;
}
ul,li,ol{
    list-style: none;
}
a{
    text-decoration: none; 
}
.pageNav{
    width: 560px;
    height: 60px;
    margin-right: auto;
    margin-left: auto;
    background-color: #09f;
}
.navUl{
    display: flex;
}
.navUl>li{
    position: relative;
}
.navUl>li>a{
    display: block;
    width: 140px;
    height: 50px;
    color: white;
    text-align: center;
    font-size: 25px;
    padding-top: 10px;
    transition: all 0.5s;
}
.navUl>li>a:hover{
    background-color: orange;
}
.erji{
    width: 100%;
    height: 100px;
    position: absolute;
    display: none;
}
.erji>li>a{
    display: block;
    color: black;
    text-align: center;
    line-height: 33px;
    background-color: #96cdff;
}
.erji>li>a:hover{
    background-color: orange;
    color: white;
}

JavaScript部分:

let li = document.querySelectorAll(".navUl > li");
for(let i = 0; i <= li.length-1; i++){
    li[i].addEventListener("mouseenter",function(){
        let erji = this.children[1];
        if(!erji){
            return false;
        }
        erji.style.display = "block";
    });
    li[i].addEventListener("mouseleave",function(){
        let erji = this.children[1];
        if(!erji){
            return false;
        }
        erji.style.display = "none";
    });
}

学会了记得点赞哦!

你可能感兴趣的:(JavaScript,javascript,前端,html,html5,css3)