在下拉菜单中显示图像

时间:2015-11-15 20:27:28

标签: javascript jquery html css

我想将鼠标悬停在下拉菜单中的列表项目上时显示图像,我不能按照我想要的方式单独使用css,我想在悬停时右侧显示图像左边的相应名称。

到目前为止我编写的代码不起作用,因为使用css时必须具有亲子关系,我很缺乏经验,所以也许解决方案非常简单,如果是这样,请原谅。< / p>

HTML:

<body>
<div class="menu-wrap">
    <nav class="menu">
        <ul class="clearfix">
            <li class="current-item"><a href="#">Home</a>
            </li>
            <li><a href="#">Collaboratori</a>                
                <div class="sub-menu-collab">
                    <div>
                         <ul class="listanomi">
                            <li><a href="#">Paolo</a></li>
                            <li><a href="#">Luca</a></li>
                            <li><a href="#">Anna</a></li>
                            <li><a href="#">Mara</a></li>
                        </ul>
                        <img class="menuimage" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSR6KryU6yZY04TXZ7qgzBYtX61idklEgJGxnee_MausSGZJQqvmRXEbvo" title="Paolo">
                        <img class="menuimage" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSR6KryU6yZY04TXZ7qgzBYtX61idklEgJGxnee_MausSGZJQqvmRXEbvo" title="Luca">              
                        <img class="menuimage" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSR6KryU6yZY04TXZ7qgzBYtX61idklEgJGxnee_MausSGZJQqvmRXEbvo" title="Anna">
                        <img class="menuimage" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSR6KryU6yZY04TXZ7qgzBYtX61idklEgJGxnee_MausSGZJQqvmRXEbvo" title="Mara">
                    </div>
                </div>
            </li>
    </nav>
</div>

</body>

的CSS:

.clearfix:after {
    display:block;
    clear:both;
}

/*----- Menu Outline -----*/
.menu-wrap {
    width:100%;
    box-shadow:0px 1px 3px rgba(0,0,0,0.2);
    background:#3e3436;
}

.menu {
    width:1000px;
    margin:0px auto;
}

.menuimage {
    float: right;
    clear: none;
    padding: 40px;
    opacity: 1;
    position: relative;
    transition:all linear 0.15s;
}

.menu > .listanomi > li:hover .menuimage{
    opacity: 1;
}

.listanomi {
    float: left;
}

.menu li {
    margin:0px;
    list-style:none;
    font-family: 'Playfair Display', serif;
    text-transform: uppercase;
    text-decoration: none;
}

.menu a {
    transition:all linear 0.15s;
    color:#a1a1a1;
}

.menu li:hover > a, .menu .current-item > a {
    text-decoration:none;
    color:#e8e8e8;
}

.menu .arrow {
    font-size:11px;
    line-height:0%;
}

/*----- Top Level -----*/
.menu > ul > li {
    float:left;
    display:inline-block;
    position:relative;
    font-size:16px;
    text-decoration: none;
}

.menu > ul > li > a {
    padding:10px 40px;
    display:inline-block;
    text-shadow:0px 1px 0px rgba(0,0,0,0.4);
    text-decoration: none;
}

.menu > ul > li:hover > a, .menu > ul > .current-item > a {
    background:#2e2728;
    text-decoration: none;
}

/*----- Bottom Level -----*/
.menu li:hover .sub-menu-collab, .menu li:hover .sub-menu-mat, .menu li:hover .sub-menu-arte {
    z-index:1;
    opacity:1;
}

.sub-menu-collab {
    width:150%;
    padding:5px 0px;
    position:absolute;
    top:100%;
    left:0%;
    z-index:-1;
    opacity:0;
    transition:opacity linear 0.15s;
    box-shadow:0px 2px 3px rgba(0,0,0,0.2);
    background:#2e2728;
    text-decoration: none;
}


.sub-menu-collab li, .sub-menu-mat li, .sub-menu-arte li {
    display:block;
    font-size:16px;
    text-decoration: none;
}

.sub-menu-collab li a, .sub-menu-mat li a, .sub-menu-arte li a {
    padding:10px 30px;
    display:block;
    text-decoration: none;
}

.sub-menu-collab li a:hover, .sub-menu-mat li a:hover, .sub-menu-arte li a:hover {
    background:#3e3436;
    text-decoration: none;
}

.current-item a {
    background:#3e3436;
}

jsfiddle上的代码:code

谢谢!

3 个答案:

答案 0 :(得分:1)

你犯了两个错误:

  1. 在css中使用父子关系时,您需要将此案例中`locale1` => [ 'domain1' => [ 'key1-1' => 'value1-1', 'key1-2' => 'value1-2' ], 'domain2' => [ ... ] ] 元素的子项指向父级。
  2. img

    1. CSS选择应该是

      <li> <a href="#">Anna</a> <img class="menuimage" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSR6KryU6yZY04TXZ7qgzBYtX61idklEgJGxnee_MausSGZJQqvmRXEbvo" title="Anna"> </li>

    2. .menu .listanomi > li:hover .menuimage { opacity: 1; }不是.listanomi的直接子女。

      注意:我还将img float更改为绝对到右上角。

      此处的工作示例:

      .menu
      .clearfix:after {
        display: block;
        clear: both;
      }
      /*----- Menu Outline -----*/
      
      .menu-wrap {
        width: 100%;
        box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.2);
        background: #3e3436;
      }
      .menu {
        width: 1000px;
        margin: 0px auto;
      }
      .menuimage {
        clear: none;
        padding: 40px;
        opacity: 0;
        position: absolute;
        top: 0;
        right: 0;
        transition: all linear 0.15s;
      }
      .menu .listanomi > li:hover .menuimage {
        opacity: 1;
      }
      .listanomi {
        float: left;
      }
      .menu li {
        margin: 0px;
        list-style: none;
        font-family: 'Playfair Display', serif;
        text-transform: uppercase;
        text-decoration: none;
      }
      .menu a {
        transition: all linear 0.15s;
        color: #a1a1a1;
      }
      .menu li:hover > a,
      .menu .current-item > a {
        text-decoration: none;
        color: #e8e8e8;
      }
      .menu .arrow {
        font-size: 11px;
        line-height: 0%;
      }
      /*----- Top Level -----*/
      
      .menu > ul > li {
        float: left;
        display: inline-block;
        position: relative;
        font-size: 16px;
        text-decoration: none;
      }
      .menu > ul > li > a {
        padding: 10px 40px;
        display: inline-block;
        text-shadow: 0px 1px 0px rgba(0, 0, 0, 0.4);
        text-decoration: none;
      }
      .menu > ul > li:hover > a,
      .menu > ul > .current-item > a {
        background: #2e2728;
        text-decoration: none;
      }
      /*----- Bottom Level -----*/
      
      .menu li:hover .sub-menu-collab,
      .menu li:hover .sub-menu-mat,
      .menu li:hover .sub-menu-arte {
        z-index: 1;
        opacity: 1;
      }
      .sub-menu-collab {
        width: 150%;
        padding: 5px 0px;
        position: absolute;
        top: 100%;
        left: 0%;
        z-index: -1;
        opacity: 0;
        transition: opacity linear 0.15s;
        box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.2);
        background: #2e2728;
        text-decoration: none;
      }
      .sub-menu-mat {
        width: 120%;
        padding: 5px 0px;
        position: absolute;
        top: 100%;
        left: 0%;
        z-index: -1;
        opacity: 0;
        transition: opacity linear 0.15s;
        box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.2);
        background: #2e2728;
        text-decoration: none;
      }
      .sub-menu-arte {
        width: 150%;
        padding: 5px 0px;
        position: absolute;
        top: 100%;
        left: -50%;
        z-index: -1;
        opacity: 0;
        transition: opacity linear 0.15s;
        box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.2);
        background: #2e2728;
        text-decoration: none;
      }
      .sub-menu-collab li,
      .sub-menu-mat li,
      .sub-menu-arte li {
        display: block;
        font-size: 16px;
        text-decoration: none;
      }
      .sub-menu-collab li a,
      .sub-menu-mat li a,
      .sub-menu-arte li a {
        padding: 10px 30px;
        display: block;
        text-decoration: none;
      }
      .sub-menu-collab li a:hover,
      .sub-menu-mat li a:hover,
      .sub-menu-arte li a:hover {
        background: #3e3436;
        text-decoration: none;
      }
      .sub-menu-collab li a:visited,
      .sub-menu-mat li a:visited,
      .sub-menu-arte li a:visited {
        text-decoration: none;
        color: #989898;
      }
      .menu li a:visited {
        text-decoration: none;
        color: #999999;
      }
      .menu li a:active {
        text-decoration: none;
        color: #999999;
      }
      .current-item a {
        background: #3e3436;
      }

答案 1 :(得分:0)

试试这种方式

<body>
    <div class="menu-wrap">
        <nav class="menu">
            <ul class="clearfix">
                <li class="current-item"><a href="#">Home</a>
                </li>
                <li><a href="#">Collaboratori</a>                
                    <div class="sub-menu-collab">
                        <div>
                             <ul class="listanomi">
                                <li><a href="#"><img class="menuimage" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSR6KryU6yZY04TXZ7qgzBYtX61idklEgJGxnee_MausSGZJQqvmRXEbvo" title="Paolo">Paolo</a></li>
                                <li><a href="#"><img class="menuimage" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSR6KryU6yZY04TXZ7qgzBYtX61idklEgJGxnee_MausSGZJQqvmRXEbvo" title="Luca">Luca</a></li>
                                <li><a href="#"><img class="menuimage" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSR6KryU6yZY04TXZ7qgzBYtX61idklEgJGxnee_MausSGZJQqvmRXEbvo" title="Anna">Anna</a></li>
                                <li><a href="#"><img class="menuimage" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSR6KryU6yZY04TXZ7qgzBYtX61idklEgJGxnee_MausSGZJQqvmRXEbvo" title="Mara">Mara</a></li>
                            </ul>
                        </div>
                    </div>
                </li>
        </nav>
    </div>

</body>

答案 2 :(得分:0)

如果要显示与标题对应的图像,可以使用jQuery作为示例:

$(".item").hover(function() {
   $(".menuimage[title='"+$(this).html()+"']").css("opacity", 1);
  }, function() {
    $(".menuimage[title='"+$(this).html()+"']").css("opacity", 0);
});

Working demo