您当前的位置: 首页 >  Java

命运之手

暂无认证

  • 1浏览

    0关注

    747博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Javascript】【jQuery】统一为自动悬浮框添加事件

命运之手 发布时间:2019-05-14 11:04:30 ,浏览量:1

运行效果 在这里插入图片描述 html代码




    
        
        jQuery
    

    
    
    

    
        
    Items
  • Item-1
  • Item-2
  • Item-3
  • Item-4
  • Item-5
    Items
  • Item-1
  • Item-2
  • Item-3
  • Item-4
  • Item-5
    Items
  • Item-1
  • Item-2
  • Item-3
  • Item-4
  • Item-5

js代码


$(() => {
    //悬浮框id规定为父容器id加上-pop,利用这个规则,就可以统一为悬浮框添加事件
    $(".pop-parent").hover(function () {
        let popChildSelector = "#" + this.id + "-pop";
        $(popChildSelector).show();
    }, function () {
        let popChildSelector = "#" + this.id + "-pop";
        $(popChildSelector).hide();
    });

    //给item按钮添加点击事件,每个id对应一个事件处理器(方法)
    let eventHandlers = new Map();
    eventHandlers.set("item-1", () => alert("Item-1 Click"));
    eventHandlers.set("item-2", () => alert("Item-2 Click"));
    //为每个item设置共通的点击事件,如点击后隐藏父容器
    $("#menu>ul>div>li").click(function () {
        $(this).parent().hide();
        eventHandlers.get(this.id)();
    });
});

css代码


* {
    padding: 0;
    margin: 0;
}

#menu {
    font-size: 0px;
}

#menu > ul {
    width: 100px;
    display: inline-block;
    margin-right: 1px;
}

#menu > ul > a {
    display: inline-block;
    width: 100%;
    background: deepskyblue;
    text-align: center;
    font-size: 16px;
    color: white;
    line-height: 100%;
    padding-top: 10px;
    padding-bottom: 10px;
    -webkit-user-select: none;
    cursor: pointer;
}

#menu > ul > a:hover {
    background: dodgerblue;
}

#menu > ul > div {
    position: relative;
    float: left;
    top: 1px;
    display: none;
}

#menu > ul > div > li {
    width: 100%;
    background: deepskyblue;
    font-size: 16px;
    list-style: none;
    padding-top: 8px;
    padding-bottom: 8px;
    margin-bottom: 1px;
    display: inline-block;
    text-align: center;
    color: white;
    -webkit-user-select: none;
    cursor: pointer;
}

#menu > ul > div > li:hover {
    background: dodgerblue;
}

关注
打赏
1654938663
查看更多评论
立即登录/注册

微信扫码登录

0.0368s