Javascript JQuery'animate'上的'Height:Toggle'使Div保持切换

Javascript JQuery'animate'上的'Height:Toggle'使Div保持切换,javascript,jquery,html,css,toggle,Javascript,Jquery,Html,Css,Toggle,我想做的是:当达到一定数量的页面幻灯片时,使网站的顶部栏向下切换。即,当用户向下滑动328px时,顶部栏向下滑动并固定在顶部 问题是,当用户到达328px时,向下滑动,顶部的条div开始上下切换,并且没有停止!只有当我将页面滑回顶部时,它才会停止 我如何使它在达到328px时向下切换,在低于328px时向上切换 这是我的代码: <script type="text/javascript"> $( document ).ready(function() {

我想做的是:当达到一定数量的页面幻灯片时,使网站的顶部栏向下切换。即,当用户向下滑动328px时,顶部栏向下滑动并固定在顶部

问题是,当用户到达328px时,向下滑动,顶部的条div开始上下切换,并且没有停止!只有当我将页面滑回顶部时,它才会停止

我如何使它在达到328px时向下切换,在低于328px时向上切换

这是我的代码:

<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>
将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> 
$window.scroll将在用户每次滚动时触发。用户每次滚动$header fixed.animate{height:toggle};被称为。因此,这意味着即使用户仅滚动1px,顶栏也会再次设置动画。您必须更加小心地找出何时以及如何设置顶栏的动画

另一个问题是topbar的初始状态和jQuery关键字切换的使用。显然,“切换为动画”属性也会将顶栏的“显示”值设置为“无”

建议的CSS转换解决方案是解决这一问题的好方法

<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> 
#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 

}