延迟关闭仅使用div进行的css菜单上的子菜单

时间:2013-12-22 19:36:49

标签: javascript jquery html css css3

我用DIV制作了这个水平菜单,(根本没有使用ul和li列表),我正在寻找一种方法来延迟鼠标输出的子菜单的崩溃,不介意它是用javascript,jquery还是CSS。 我尝试了一些javascript解决方案,但都没有。

CSS

.topmenu
{
margin: 0 auto; width: auto;float:left; 
position:relative;
z-index:4;
height: 50px;
font-family: Arial, Helvetica, sans-serif;
font-size:16px;
color:#444;
}

.topmenu a
{
padding:0 16px;
line-height:50px; /*Note: keep this value the same as the height of .topmenu */
font-size:16px;
font-weight:normal;
display:block;
outline:0;
text-decoration: none;
position:relative;
color:#444;
}

 .topmenu .home, .topmenu .button1, .topmenu .button2 {float: left;}
 .topmenu .home img {vertical-align: top; margin-top:8px; border:none;}
 .topmenu .home:hover a, .topmenu .button1:hover a, .topmenu .button2:hover a
  {background-color:#333; color:#FFF; z-index:9;}



 .topmenu .subhome,
 .topmenu .submenu1,
 .topmenu .submenu2
 {
position: absolute;
right:0;
z-index:20;
display:none;
background-color:#0e5079;           
text-align: left;
padding: 20px;  
top:50px;
color:#999;
border-radius:3px;
-moz-box-shadow:1px 2px 12px #333333;
-webkit-box-shadow:1px 2px 12px #333333;
box-shadow:1px 2px 12px #333333;
text-shadow:
-1px -1px 0 #333,0px -1px 0 #333,1px -1px 0 #333,-1px 1px 0 #333,0px 1px 0 #333,1px 1px 0 #333;
 }
.topmenu .home:hover .subhome {display:block;}
.topmenu .button1:hover .submenu1 {display:block;}
.topmenu .button2:hover .submenu2 {display:block;}

.topmenu .subhome {
width:960px;
height:560px;
background-image:url(menu/menu-bg/corfu-bw-bg.jpg);
background-repeat:no-repeat;
background-position:center;     
  }
.topmenu .submenu1 {
background-image:url(menu/menu-bg/benitses-bg.jpg);
background-repeat:no-repeat;
background-position:center;     
}
.topmenu .submenu2 {
background-image:url(menu/menu-bg/corfu-bg.jpg);
background-repeat:no-repeat;
background-position:center;     
}
.topmenu .submenuleft {padding:20px;float:left;}
.topmenu .submenuleft span {font-weight:bold; font-size:16px;color:#DDD;line-  height:32px;}
.topmenu .submenuleft a span {font-weight:bold; font-size:16px;color:#FA0;padding-   top:5px;padding-bottom:8px;}
.topmenu .submenuleft a span:hover {color:#FF0; text-decoration:none;}
.topmenu .submenuleft a:hover {color:#FF0;font-size:14px;text-decoration:none;background:none;}
.topmenu .submenuleft img {display:block;border:#FFF 2px solid;padding:4px;margin-top:5px;}

.topmenu .subhome .submenuleft a,
.topmenu .submenu1 .submenuleft a,
.topmenu .submenu2 .submenuleft a
{
padding:0px;
line-height:26px;
font-size:14px;
background:none;
display: inline;
text-align:left;    
z-index: 0;
} 

和HTML

     <div class="topmenu">
<div class="home"><a href="#">HOME</a>
     <div class="subhome">
     </div> 
</div>
<div class="button1"><a href="#">ITEM1</a>
    <div class="submenu1" style="width:840px;">
        <div class="submenuleft">
                <a href="#"><span>LINK TITLE</span></a><br> 
                <a href="#">Example link</a><br>
                <a href="#">Example link</a><br>
                <a href="#">Example link</a><br>
                <a href="#">Example link</a>
        </div>
        <div class="submenuleft">
                <a href="#"><span>LINK TITLE</span></a><br> 
                <a href="#">Example link</a><br>
                <a href="#">Example link</a>
        </div>
        <div class="submenuleft">
                <a href="#"><span>LINK TITLE</span></a><br> 
                <a href="#">Example link</a><br>
                <a href="#">Example link</a><br>
                <a href="#">Example link</a><br>
                <a href="#">Example link</a><br>
                <a href="#">Example link</a>
        </div>
        <div class="submenuleft" style="text-align:right;width:130px;">
            <img src="menu/benitses-m.jpg" alt="" style="width:128px; height:200px; height:190px;margin-top:5px;">
        </div>            
    </div>
</div>

<div class="button2"><a href="#">ITEM2</a>
     <div class="submenu2" style="width:800px;">
       <div class="submenuleft">
                 <a href="#"><span>LINK TITLE</span></a><br> 
                 <a href="#">Example link</a><br>
                 <a href="#">Example link</a><br>   
                 <a href="#">Example link</a>                  
        </div>
        <div class="submenuleft">
                 <a href="#"><span>LINK TITLE</span></a><br>                  
                 <a href="#"><span>LINK TITLE</span></a><br> 
                 <a href="#">Example link</a><br>
                 <a href="#">Example link</a>                  
        </div>
        <div class="submenuleft">
                  <a href="#">Example link</a><br>
                  <a href="#">Example link</a><br>
                  <a href="#">Example link</a>                   
        </div>
        <div class="submenuleft" style="text-align:right; width:140px;">
            <img src="menu/mouse-island.jpg" alt="" style="width:140px; height:210px;">
        </div>
     </div>
</div>

您可以看到此菜单here

的TEST示例

谢谢

3 个答案:

答案 0 :(得分:2)

您可以使用CSS Transitions。只需使用visibility属性来显示和隐藏菜单(而不是在displayblock之间切换none),指定其上的转换,您将延迟{仅{1}}到visible,而不是反向。

显然,您还可以创建淡入淡出效果,为hidden属性设置动画。

实例:http://jsfiddle.net/KVtV7/

示例代码:

opacity

CSS:

<ul id="menu">
    <li>
        <a href="">Text</a>
        <ul>
            <li><a href="">A</a></li>
            <li><a href="">B</a></li>
            <li><a href="">C</a></li>
            <li><a href="">D</a></li>
            <li><a href="">E</a></li>
        </ul>
    </li>
</ul>

答案 1 :(得分:0)

您不能仅使用CSS来延迟隐藏元素(使用:hover)。浏览器立即呈现新状态。你需要一些JavaScript和setTimeout魔法来实现这一点。

一个简单的例子就像下面的代码。我在这里使用了jQuery,因为它很紧凑,它提供了一些有用的东西,比如mouseleave事件。

我为每个按钮button添加了一个课程div

<div class="button1 button"><a href="#">ITEM1</a>

$('.button').each(function() {

  // We use closures here so that each button has it's own 'to'.
  var $that = $(this)
      ,to;

  $that.on('mouseleave', function() {

    // We wait 500 ms using setTimeout
    to = setTimeout(function() { 
      $that.find('> div').hide(); 
    }, 500);
  });

  $that.on('mouseenter', function() {

    // Hide all other open submenu's that are potentially open.
    $('.button > div').hide(); 

    // I use a show here, but you could also keep using your CSS :hover technique. 
    // In that case remove this show call. 
    $that.show();
  });

答案 2 :(得分:0)

带3个主要按钮的代码(可以扩展为任意数字)可以是这样的,包括图片和背景图片等...

HTML

 <div class="topmenu">
<div class="home"><a href="#">HOME</a>
     <div class="subhome">
     </div> 
</div>
<div class="button1 sub"><a href="#">ITEM1</a>
    <div class="submenu1" style="width:840px;">
        <div class="submenuleft">
                <a href="#"><span>LINK TITLE</span></a><br> 
                <a href="#">Example link</a><br>
                <a href="#">Example link</a><br>
                <a href="#">Example link</a><br>
                <a href="#">Example link</a>
        </div>
        <div class="submenuleft">
                <a href="#"><span>LINK TITLE</span></a><br> 
                <a href="#">Example link</a><br>
                <a href="#">Example link</a>
        </div>
        <div class="submenuleft">
                <a href="#"><span>LINK TITLE</span></a><br> 
                <a href="#">Example link</a><br>
                <a href="#">Example link</a><br>
                <a href="#">Example link</a><br>
                <a href="#">Example link</a><br>
                <a href="#">Example link</a>
        </div>
        <div class="submenuleft" style="text-align:right;width:130px;">
            <img src="" alt="" style="width:128px; height:200px; height:190px;margin-top:5px;">
        </div>            
    </div>
</div>

<div class="button2"><a href="#">ITEM2</a>
     <div class="submenu2" style="width:800px;">
       <div class="submenuleft">
                 <a href="#"><span>LINK TITLE</span></a><br> 
                 <a href="#">Example link</a><br>
                 <a href="#">Example link</a><br>   
                 <a href="#">Example link</a>                  
        </div>
        <div class="submenuleft">
                 <a href="#"><span>LINK TITLE</span></a><br>                  
                 <a href="#"><span>LINK TITLE</span></a><br> 
                 <a href="#">Example link</a><br>
                 <a href="#">Example link</a>                  
        </div>
        <div class="submenuleft">
                  <a href="#">Example link</a><br>
                  <a href="#">Example link</a><br>
                  <a href="#">Example link</a>                   
        </div>
        <div class="submenuleft" style="text-align:right; width:140px;">
            <img src="" alt="" style="width:140px; height:210px;">
        </div>
     </div>
</div>

和CSS部分

      .home .topmenu .home a,
      .button1 .topmenu .button1 a,
      .button2 .topmenu .button2 a { 
      color:#FFF;  
      background-color:#a4472d;
 }
.topmenu
{
      margin: 0 auto; width: auto;float:left; 
      position:relative;
      z-index:20;
      height: 50px;
      font-family: Arial, Helvetica, sans-serif;
  font-size:16px;
  color:#444;
    }

     .topmenu a
 {
     padding:0 16px;
     line-height:50px; /*Note: keep this value the same as the height of .topmenu */
     font-size:16px;
     font-weight:normal;
     display:block;
     outline:0;
     text-decoration: none;
     position:relative;
 color:#444;
 }

     .topmenu .home, .topmenu .button1, .topmenu .button2 {float: left;}
     .topmenu .home img {vertical-align: top; margin-top:8px; border:none;}
     .topmenu .home:hover a, .topmenu .button1:hover a, .topmenu .button2:hover a  {background-color:#333; color:#FFF; z-index:9;}



     .topmenu .subhome,
     .topmenu .submenu1,
     .topmenu .submenu2
{
    position: absolute;
right:0;
    z-index:20;
visibility:hidden; 
opacity:0;        **/* use both visibility and opacity */**
    background-color:#0e5079;           
    text-align: left;
    padding: 20px;  
    top:50px;
color:#999;
border-radius:3px;
-moz-box-shadow:1px 2px 12px #333333;
-webkit-box-shadow:1px 2px 12px #333333;
box-shadow:1px 2px 12px #333333;
text-shadow:
-1px -1px 0 #333,0px -1px 0 #333,1px -1px 0 #333,-1px 1px 0 #333,0px 1px 0 #333,1px 1px 0 #333;

    -moz-transition-property: visibility, opacity;
    -moz-transition-duration: 2s;
    -webkit-transition-property: visibility, opacity;
    -webkit-transition-duration: 2s;
    transition-property: visibility, opacity;
    transition-duration: 2s;   **/* some changes on transition */**

   }
   .topmenu .home:hover .subhome {visibility:visible; opacity:1; z-index:150;}
   .topmenu .button1:hover .submenu1 {visibility:visible; opacity:1; z-index:150;}
   .topmenu .button2:hover .submenu2 {visibility:visible; opacity:1; z-index:150;  **/* the rise of z-index during topbuttons hover is important */**}


.topmenu .subhome {
width:960px;
height:560px;
background-image:url(example.jpg);
background-repeat:no-repeat;
background-position:center;     
}
    .topmenu .submenu1 {
background-image:url(example2.jpg);
background-repeat:no-repeat;
background-position:center;     
}
    .topmenu .submenu2 {
background-image:url(example3.jpg);
background-repeat:no-repeat;
background-position:center;     
}
    .topmenu .submenuleft {padding:20px;float:left;}
    .topmenu .submenuleft span {font-weight:bold; font-size:16px;color:#DDD;line-height:32px;}
    .topmenu .submenuleft a span {font-weight:bold; font-size:16px;color:#FA0;padding-top:5px;padding-bottom:8px;}
    .topmenu .submenuleft a span:hover {color:#FF0; text-decoration:none;}
    .topmenu .submenuleft a:hover {color:#FF0;font-size:14px;text-decoration:none;background:none;}
    .topmenu .submenuleft img {display:block;border:#FFF 2px solid;padding:4px;margin-top:5px;}

    .topmenu .subhome .submenuleft a,
    .topmenu .submenu1 .submenuleft a,
    .topmenu .submenu2 .submenuleft a
{
    padding:0px;
    line-height:26px;
    font-size:14px;
background:none;
    display: inline;
    text-align:left;    
    z-index: 0;
color:#FFF;
}