简体   繁体   English

JQuery的“动画”上的“高度:切换”使Div继续切换

[英]'Height: Toggle' on JQuery's 'animate' makes Div keep toggling

What I am trying to do is: to make the top bar of a website toggle down when a certain amount of page slide is reached. 我想做的是:当到达一定数量的页面幻灯片时,使网站的顶部栏向下切换。 IE, when the user slides down 328px, the top bar slides down and stay fixed on top. IE,当用户向下滑动328px时,顶部栏向下滑动并保持固定在顶部。

The problem is that when the user reaches the 328px sliding down, the top bar div starts toggling up and down and it doesn't stop! 问题是,当用户到达向下滑动的328px时,顶部栏div开始上下滚动,并且不会停止! It only stops when I move the page slide back to the top. 仅当我将页面幻灯片移回顶部时,它才会停止。

How do I make it toggle down when it reaches 328px and the toggle up when it gets below 328px? 当它达到328px时如何使其向下切换,当它低于328px时如何切换?

This is my code: 这是我的代码:

<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: 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 
}

change your html to 将您的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> 

and your css to 和你的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 

}

$(window).scroll() is fired everytime the user scrolls. 每次用户滚动都会触发$(window).scroll() Everytime the user scrolls $("#header-fixed").animate({"height": "toggle"}); 每次用户滚动$("#header-fixed").animate({"height": "toggle"}); is called. 叫做。 So that means even when a user only scrolls for 1px the topbar will animate again. 因此,这意味着即使用户仅滚动1像素,顶栏也将再次进行动画处理。 You have to be more careful with finding out when and how to animate the topbar. 您必须更加谨慎地确定何时以及如何为顶部栏设置动画。

Another problem is the initial state of the topbar and the use of the jQuery keyword toggle . 另一个问题是topbar的初始状态和jQuery关键字toggle Apparently toggle as an animation property also sets the display value to none for your topbar. 显然,作为动画属性toggle也会将顶部栏的display值设置为none

The suggested solution with CSS transitions is a good way to solve this. CSS过渡的建议解决方案是解决此问题的好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM