为什么再次悬停时下拉菜单动画会削高?

Why does the dropdown menu animation cut the height when you hover again?

我最近实现了来自 MegaDrop 的 HTML、CSS 和 jQuery 代码。与任何下拉菜单一样,当您将鼠标悬停在它上面时,它应该会显示所有链接。

请将鼠标悬停在“产品”上以查看结果。

当您悬停离开时问题就开始了,当您在动画完成之前再次悬停时,它会削减高度并记住这一点。将鼠标悬停几次后,它也不会响应。这很奇怪。

我没有在容器中设置高度。我只是让它本地呈现,因为链接有填充。

容器也有 overlay: hidden

要启用 jQuery 动画,我需要从 .nav > li:hover > [=49= 中删除 display: block ]

我的问题是,如何防止这种情况发生?我是否需要向 jQuery 本身提供更多说明以保持容器的高度而不是缩短它?

这是网站: http://planet.nu/dev/test/index.html

这是我的 HTML、CSS 和 Javascript:

$(function(){
 $('.animated > li').hover(function(){
  $(this).find('div[class^="container-"]').stop().slideDown('fast');
 },
 function(){
  $(this).find('div[class^="container-"]').stop().slideUp('slow');
 });
});
* { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; padding: 0; margin: 0;}


body { font-family: "PT Sans", sans-serif; background: #f5f5f5; font-size: 100%;}
a { color: #666; text-decoration: none;}

.nav {
 background: #666;
 border-radius: 3px;
 width: 960px;
 margin: 20px auto;
 height: 43px;
}

.nav li { list-style: none;  }
.nav > li { padding: 0; float: left; position: relative; }
.nav > li > a {
 float: left;
 color: #fff;
 font-size: 13px;
 text-decoration: none;
 line-height: 43px;
 padding: 0 20px; 
 border-right: 1px solid #777;
 border-left: 1px solid #555;
 height: 43px;
}
.nav > li:hover > a { background: #555; border-right: 1px solid #666; }
.nav > li:first-child > a { border-left: 0; }
.nav > li:last-child > a { border-right: 0; }
.nav > li:hover > div {  }

/* MEGA DROP DOWN */

.nav > li > div {
 position: absolute;
 left: 0;
 top: 43px;
 display: none;
 background: #fff;
 padding: 10px 10px;
 box-shadow: 0 2px 3px rgba(0,0,0,0.1);
 overflow: hidden;
}
.nav > li > div p { color: #666; }
.nav > li > div.right { right: 0; left: auto }
.container-1, .container-2, .container-3, .container-4 { font-size: 80%; }


/* COLUMNS */
.col1,.col2,.col3,.col4,.col5 { margin: 10px 0; float: left; padding: 0 10px;}


 /* 1 Column */
 .container-1 { width: 170px; }
 .container-1 .col1 { width: 100%; }

 /* 2 Column */
 .container-2 { width: 270px; }
 .container-2 .col1 { width: 50% }
 .container-2 .col2 { width: 100% }

 /* 3 Column */
 .container-3 { width: 400px; }
 .container-3 .col1 { width: 33.33% }
 .container-3 .col2 { width: 66.66% }
 .container-3 .col3 { width: 100% }


 /* 4 Column */
 .container-4 { width: 500px; }
 .container-4 .col1 { width: 25%; }
 .container-4 .col2 { width: 50% }
 .container-4 .col3 { width: 75% }
 .container-4 .col4 { width: 100% }


 /* 5 Column */
 .container-5 { width: 600px; }
 .container-5 .col1 { width: 20%; }
 .container-5 .col2 { width: 40% }
 .container-5 .col3 { width: 60% }
 .container-5 .col4 { width: 80% }
 .container-5 .col5 { width: 100% }

/* HIGHLIGHTS */

.container-3 .highlighted { 
 width: 400px; 
 margin-left: -20px; 
 padding: 20px; 
 background: #e4fbff; 
 border-top: 1px solid #ddd; 
 border-bottom: 1px solid #ddd;
}
.container-4 .highlighted { 
 width: 500px;
 margin-left: -10px; 
 padding: 20px; 
 background: #e4fbff; 
 border-top: 1px solid #ddd; 
 border-bottom: 1px solid #ddd;
}



.clearfix:after {
  content: ".";
  display: block;
  height: 0;
  clear: both;
  visibility: hidden;
}
<!doctype html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Mega Drop Down</title>
 <link rel="stylesheet" href="css/megadrop.css" />
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
 <script type="text/javascript" src="js/scripts.js"></script>
</head>
<body>
<ul class="nav clearfix animated">
 <li><a href="#">Home</a></li>
 <li>
  <a href="#">Products</a>
  <div class="container-4">
   <div class="col1">
    <h3>Vegetables</h3>
    <ul>
     <li><a href="#">Tomatoes</a></li>
     <li><a href="#">Squash</a></li>
     <li><a href="#">Zucchini</a></li>
     <li><a href="#">Cucumbers</a></li>
     <li><a href="#">Green Beans</a></li>
     <li><a href="#">Lima Beans</a></li>
    </ul>
   </div>
   <div class="col1">
    <h3>Fruits</h3>
    <ul>
     <li><a href="#">Apples</a></li>
     <li><a href="#">Blueberries</a></li>
     <li><a href="#">Cranberries</a></li>
     <li><a href="#">Raspberries</a></li>
     <li><a href="#">Strawberries</a></li>
    </ul>
   </div>
   <div class="col2">
    <h3>Quality Guaranteed</h3>
    <p>We stand behind our products and guarantee you will get high quality products.</p>
   </div>
   <div class="col4 highlighted">
    <h3>SALE: Buy one get one FREE</h3>
    <p>All of our products are buy one get one free until April 1, 2013.</p>
   </div>
   <div class="col1">
    <h3>Vegetables</h3>
    <ul>
     <li><a href="#">Tomatoes</a></li>
     <li><a href="#">Squash</a></li>
     <li><a href="#">Zucchini</a></li>
     <li><a href="#">Cucumbers</a></li>
     <li><a href="#">Green Beans</a></li>
     <li><a href="#">Lima Beans</a></li>
    </ul>
   </div>
   <div class="col1">
    <h3>Fruits</h3>
    <ul>
     <li><a href="#">Apples</a></li>
     <li><a href="#">Blueberries</a></li>
     <li><a href="#">Cranberries</a></li>
     <li><a href="#">Raspberries</a></li>
     <li><a href="#">Strawberries</a></li>
    </ul>
   </div>
   <div class="col2">
    <h3>Quality Guaranteed</h3>
    <p>We stand behind our products and guarantee you will get high quality products.</p>
   </div>
   <div class="col4 highlighted">
    <h3>SALE: Buy one get one FREE</h3>
    <p>All of our products are buy one get one free until April 1, 2013.</p>
   </div>
   <div class="col4">
    <h3>Quality Guaranteed</h3>
    <p>We stand behind our products and guarantee you will get high quality products.</p>
   </div>
  </div> 
 </li>
 <li><a href="#">Blog</a></li>
 <li><a href="#">Donate</a></li>
 <li>
  <a href="#">About</a>
  <div class="container-1 right">
   <ul>
    <li><a href="#">Who We Are</a></li>
    <li><a href="#">Why We Care</a></li>
    <li><a href="#">Some other page</a></li>
    <li><a href="#">How We Work</a></li>
   </ul>
  </div>
 </li>
 <li><a href="#">Locations</a></li>
 <li><a href="#">Contact Us</a></li>
</ul>
</body>
</html>

我了解的不够JavaScript,无法解释其中的复杂细节,但你必须将参数传递给stop()方法才能使之前的动画完成。

$(function(){
    $('.animated > li').hover(function(){
        $(this).find('div[class^="container-"]').stop(null, true).slideDown('fast');
    },
    function(){
        $(this).find('div[class^="container-"]').stop().slideUp('slow');
    });
});

啊!看起来 animate 在中断时在您的 div 中留下了一些剩余值(使用 firebug 的检查清楚地表明了这一点)。在开始动画之前清除它应该可以解决它。

$(this).find('div[class^="container-"]').attr("style","").stop().slideDown('fast');

$(function(){
 $('.animated > li').hover(function(){
  $(this).find('div[class^="container-"]').attr("style","").stop().slideDown('fast');
 },
 function(){
  $(this).find('div[class^="container-"]').stop().slideUp('slow');
 });
});
* { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; padding: 0; margin: 0;}


body { font-family: "PT Sans", sans-serif; background: #f5f5f5; font-size: 100%;}
a { color: #666; text-decoration: none;}

.nav {
 background: #666;
 border-radius: 3px;
 width: 960px;
 margin: 20px auto;
 height: 43px;
}

.nav li { list-style: none;  }
.nav > li { padding: 0; float: left; position: relative; }
.nav > li > a {
 float: left;
 color: #fff;
 font-size: 13px;
 text-decoration: none;
 line-height: 43px;
 padding: 0 20px; 
 border-right: 1px solid #777;
 border-left: 1px solid #555;
 height: 43px;
}
.nav > li:hover > a { background: #555; border-right: 1px solid #666; }
.nav > li:first-child > a { border-left: 0; }
.nav > li:last-child > a { border-right: 0; }
.nav > li:hover > div {  }

/* MEGA DROP DOWN */

.nav > li > div {
 position: absolute;
 left: 0;
 top: 43px;
 display: none;
 background: #fff;
 padding: 10px 10px;
 box-shadow: 0 2px 3px rgba(0,0,0,0.1);
 overflow: hidden;
}
.nav > li > div p { color: #666; }
.nav > li > div.right { right: 0; left: auto }
.container-1, .container-2, .container-3, .container-4 { font-size: 80%; }


/* COLUMNS */
.col1,.col2,.col3,.col4,.col5 { margin: 10px 0; float: left; padding: 0 10px;}


 /* 1 Column */
 .container-1 { width: 170px; }
 .container-1 .col1 { width: 100%; }

 /* 2 Column */
 .container-2 { width: 270px; }
 .container-2 .col1 { width: 50% }
 .container-2 .col2 { width: 100% }

 /* 3 Column */
 .container-3 { width: 400px; }
 .container-3 .col1 { width: 33.33% }
 .container-3 .col2 { width: 66.66% }
 .container-3 .col3 { width: 100% }


 /* 4 Column */
 .container-4 { width: 500px; }
 .container-4 .col1 { width: 25%; }
 .container-4 .col2 { width: 50% }
 .container-4 .col3 { width: 75% }
 .container-4 .col4 { width: 100% }


 /* 5 Column */
 .container-5 { width: 600px; }
 .container-5 .col1 { width: 20%; }
 .container-5 .col2 { width: 40% }
 .container-5 .col3 { width: 60% }
 .container-5 .col4 { width: 80% }
 .container-5 .col5 { width: 100% }

/* HIGHLIGHTS */

.container-3 .highlighted { 
 width: 400px; 
 margin-left: -20px; 
 padding: 20px; 
 background: #e4fbff; 
 border-top: 1px solid #ddd; 
 border-bottom: 1px solid #ddd;
}
.container-4 .highlighted { 
 width: 500px;
 margin-left: -10px; 
 padding: 20px; 
 background: #e4fbff; 
 border-top: 1px solid #ddd; 
 border-bottom: 1px solid #ddd;
}



.clearfix:after {
  content: ".";
  display: block;
  height: 0;
  clear: both;
  visibility: hidden;
}
<!doctype html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Mega Drop Down</title>
 <link rel="stylesheet" href="css/megadrop.css" />
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
 <script type="text/javascript" src="js/scripts.js"></script>
</head>
<body>
<ul class="nav clearfix animated">
 <li><a href="#">Home</a></li>
 <li>
  <a href="#">Products</a>
  <div class="container-4">
   <div class="col1">
    <h3>Vegetables</h3>
    <ul>
     <li><a href="#">Tomatoes</a></li>
     <li><a href="#">Squash</a></li>
     <li><a href="#">Zucchini</a></li>
     <li><a href="#">Cucumbers</a></li>
     <li><a href="#">Green Beans</a></li>
     <li><a href="#">Lima Beans</a></li>
    </ul>
   </div>
   <div class="col1">
    <h3>Fruits</h3>
    <ul>
     <li><a href="#">Apples</a></li>
     <li><a href="#">Blueberries</a></li>
     <li><a href="#">Cranberries</a></li>
     <li><a href="#">Raspberries</a></li>
     <li><a href="#">Strawberries</a></li>
    </ul>
   </div>
   <div class="col2">
    <h3>Quality Guaranteed</h3>
    <p>We stand behind our products and guarantee you will get high quality products.</p>
   </div>
   <div class="col4 highlighted">
    <h3>SALE: Buy one get one FREE</h3>
    <p>All of our products are buy one get one free until April 1, 2013.</p>
   </div>
   <div class="col1">
    <h3>Vegetables</h3>
    <ul>
     <li><a href="#">Tomatoes</a></li>
     <li><a href="#">Squash</a></li>
     <li><a href="#">Zucchini</a></li>
     <li><a href="#">Cucumbers</a></li>
     <li><a href="#">Green Beans</a></li>
     <li><a href="#">Lima Beans</a></li>
    </ul>
   </div>
   <div class="col1">
    <h3>Fruits</h3>
    <ul>
     <li><a href="#">Apples</a></li>
     <li><a href="#">Blueberries</a></li>
     <li><a href="#">Cranberries</a></li>
     <li><a href="#">Raspberries</a></li>
     <li><a href="#">Strawberries</a></li>
    </ul>
   </div>
   <div class="col2">
    <h3>Quality Guaranteed</h3>
    <p>We stand behind our products and guarantee you will get high quality products.</p>
   </div>
   <div class="col4 highlighted">
    <h3>SALE: Buy one get one FREE</h3>
    <p>All of our products are buy one get one free until April 1, 2013.</p>
   </div>
   <div class="col4">
    <h3>Quality Guaranteed</h3>
    <p>We stand behind our products and guarantee you will get high quality products.</p>
   </div>
  </div> 
 </li>
 <li><a href="#">Blog</a></li>
 <li><a href="#">Donate</a></li>
 <li>
  <a href="#">About</a>
  <div class="container-1 right">
   <ul>
    <li><a href="#">Who We Are</a></li>
    <li><a href="#">Why We Care</a></li>
    <li><a href="#">Some other page</a></li>
    <li><a href="#">How We Work</a></li>
   </ul>
  </div>
 </li>
 <li><a href="#">Locations</a></li>
 <li><a href="#">Contact Us</a></li>
</ul>
</body>
</html>