我想做的是:当达到一定数量的页面滑动时,使网站的顶部栏向下切换。即,当用户向下滑动 328px 时,顶部栏向下滑动并固定在顶部。
问题是当用户向下滑动到328px的时候,top bar div就开始上下跳动而且不停!只有当我将页面幻灯片移回顶部时它才会停止。
如何让它在达到 328 像素时向下切换,在低于 328 像素时向上切换?
这是我的代码:
<script type="text/javascript">
$( document ).ready(function() {
$( window ).scroll(function() {
if ($( window ).scrollTop() > 328) {
$("#header-fixed").css({"display": "block"});
$("#header-fixed").animate({"height": "toggle"});
}
if ($( window ).scrollTop() <=328) {
$("#header-fixed").css({"display": "none"});
$("#header-fixed").animate({"height": "toggle"});
}
});
});
</script>
<div id="header-fixed">
<a href="index.html"> <img id = "logo" src = "img/logo-new.png"/> </a>
<div id = "menu-links-div">
<ul id = "ul-links">
<a href = "index.html"> <li class = "menu-links"> Home </li> </a>
<a href = "media.html"> <li class = "menu-links"> Media </li> </a>
<a href = "/"> <li class = "menu-links"> Sobre </li> </a>
<a href = "/"> <li class = "menu-links"> Contatos </li> </a>
</ul>
</div>
</div>
CSS:
#header-fixed {
position: fixed;
width: 100%;
top: 0px;
display: none;
height: 100px;
background: #1e5799; /* Old browsers */
background: -moz-linear-gradient(-45deg, #1e5799 0%, #7db9e8 60%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, right bottom, color-stop(0%,#1e5799), color-stop(60%,#7db9e8)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(-45deg, #1e5799 0%,#7db9e8 60%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(-45deg, #1e5799 0%,#7db9e8 60%); /* Opera 11.10+ */
background: -ms-linear-gradient(-45deg, #1e5799 0%,#7db9e8 60%); /* IE10+ */
background: linear-gradient(135deg, #1e5799 0%,#7db9e8 60%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=1 ); /* IE6-9 fallback on horizontal gradient
}
最佳答案
将您的 html 更改为
<script type="text/javascript">
$( document ).ready(function() {
$( window ).scroll(function() {
if ($( window ).scrollTop() > 328) {
$("#header-fixed").css('height','100px');
}
if ($( window ).scrollTop() <=328) {
$("#header-fixed").css('height','0');
}
});
});
</script>
和你的CSS到
#header-fixed {
position: fixed;
width: 100%;
top: 0px;
overflow: hidden;
height: 0px;
-webkit-transition: height 0.5s;
-moz-transition: height 0.5s;
-o-transition: height 0.5s;
background: #1e5799; /* Old browsers */
background: -moz-linear-gradient(-45deg, #1e5799 0%, #7db9e8 60%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, right bottom, color-stop(0%,#1e5799), color-stop(60%,#7db9e8)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(-45deg, #1e5799 0%,#7db9e8 60%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(-45deg, #1e5799 0%,#7db9e8 60%); /* Opera 11.10+ */
background: -ms-linear-gradient(-45deg, #1e5799 0%,#7db9e8 60%); /* IE10+ */
background: linear-gradient(135deg, #1e5799 0%,#7db9e8 60%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=1 ); /* IE6-9 fallback on horizontal gradient
}
关于javascript - '高度 : Toggle' on JQuery's 'animate' makes Div keep toggling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28457191/