为什么当你再次悬停时下拉菜单动画会降低高度?
Why does the dropdown menu animation cut the height when you hover again?
我最近从MegaDrop实现了这个HTML, CSS和jQuery代码。像任何下拉菜单一样,当你把鼠标悬停在它上面时,它应该显示所有的链接。
请将鼠标悬停在"产品"上查看结果。
当你悬停时问题就开始了,当你在动画完成之前再次悬停时,它会剪切高度并记住它。它也不会响应后,你悬停和关闭几次。这很奇怪。
我没有设置容器的高度。我只是让它原生渲染,因为链接有填充。
容器也有overlay: hidden
要启用jQuery动画,我需要从中删除display: block。div我的问题是,我如何预防这种情况?我是否需要给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>
啊!看起来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>
我对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');
});
});
相关文章:
- 如何将高度动画设置为自动
- 如何设置'auto'JQuery动画中的高度
- 为矩形制作动画'它的高度应该上升,而不是下降
- 格林索克动画不适用于 SVG 高度
- 纯JavaScript - 定时动画 - 一致的高度
- 制作jQuery高度百分比动画
- jQuery在.show期间设置高度动画('幻灯片')
- jQuery动画到容器的高度
- 替换后将动画设置为高度0
- 动画渐变(javascript)背景不会延伸到窗口的整个高度
- 核心动画页面中的聚合物内容标签不会;t测量高度
- 页脚在滚动底部时没有设置高度动画
- CSS 切换过渡动画,自动高度不起作用
- 制作潜水高度动画
- Javascript在Angular中切换高度动画
- 设置高度动画时元素跳跃
- Div高度动画会导致Div在jQuery中更改对齐方式
- 在高度动画后获取回调
- Javascript高度动画问题
- 如何在jQuery中反转高度动画方向