简体   繁体   English

滚动时,如何让粘性导航栏的动画在两个方向上都能正常工作?

[英]How do I get animation for my sticky navigation bar to work in both directions when scrolling?

Here's a link to my test blog for reference 这是我测试博客的链接供参考

Thanks to TylerH, I've got the animation set for when I scroll down , but I'd like that animation to reverse when I scroll back up. 感谢TylerH,当我向下滚动时,我已经设置了动画集,但是当我向上滚动 ,我希望该动画能够反转。 I'm going to assume that I need an additional javascript function for that to work, so for the time being: 我将假设我需要一个额外的javascript函数才能工作,所以暂时:

Here is the new CSS (with added .unfixed property): 这是新的CSS(添加了.unfixed属性):

sticknav {
background: #b50000;
height: 46px;
width: 1080px;
margin-right: 0px;
margin-left: 413px;
left: 0px;
right: 0px;
position: relative;
z-index: 9999;
border-bottom: 4px solid #e50000;
webkit-box-shadow: 0 2px 6px rgba(0,0,0,0.49);
box-shadow: 0 2px 6px rgba(0,0,0,0.49);
}
.fixed {
    position:fixed;
    animation: fill 333ms forwards ease-in-out;
    -webkit-animation: fill 333ms forwards ease-in-out;
}
@keyframes fill {
    from {margin-left: 413px; width: 1080px;}
    to {margin-left: 0px; width: 100%;}
}
@-webkit-keyframes fill {
    from {margin-left: 413px; width: 1080px;}
    to {margin-left: 0px; width: 100%;}
}
.unfixed {
    position:fixed;
    animation: unfill 333ms ease-in-out;
    -webkit-animation: unfill 333ms ease-in-out;
}
@keyframes unfill {
    from {margin-left: 0px; width: 100%;}
    to {{margin-left: 413px; width: 1080px;}
}
@-webkit-keyframes unfill {
    from {margin-left: 0px; width: 100%;}
    to {{margin-left: 413px; width: 1080px;}
}

Here is the Javascript that does with the sticky navigation: 以下是使用粘性导航的Javascript:

<script type="text/javascript">
$(document).ready(function() {

var aboveHeight = 205;

    $(window).scroll(function(){
        if ($(window).scrollTop() > aboveHeight){
        $('sticknav').addClass('fixed').css('top','0').next().css('padding-top','60px');
        } else {
       $('sticknav').removeClass('fixed').next().css('padding-top','0');
        }
    });
});
</script>

I need to find a way to activate .unfixed once .fixed deactivates, but that sounds complicated. 我需要找到一种方法来激活.unfixed一次.fixed去激活,但这听起来很复杂。 I'll need it to activate only after the user scrolls back up after .fixed activates. 只有在用户在.fixed激活后向上滚动后,我才需要激活它。 Yeah, that sounds complicated, any help would be appreciated. 是的,这听起来很复杂,任何帮助都会受到赞赏。 Hopefully there's an easier way. 希望有一种更简单的方法。 An example of what I want exactly can be found on Game Rebels . 我想要的一个例子可以在Game Rebels上找到。

There's a simple solution to your problem. 有一个简单的解决方案来解决您的问题。 In order to use animation and keep the end result, rather than have it reset upon completion, you need to apply an animation-fill-mode . 为了使用animation并保留最终结果,而不是在完成时重置它,您需要应用animation-fill-mode

Move your animation to .fixed instead of .sticknav (this will make the animation play when .fixed is added on scroll-down, rather than at page load), and add the animation-fill-mode : 将动画移动到.fixed而不是.sticknav (这将使动画在向下滚动时添加.fixed而不是在页面加载时播放),并添加animation-fill-mode

.fixed {
    position: fixed;
    animation: fill 11s linear;
    animation-fill-mode: forwards;
}

I would also strongly recommend that you use external CSS files, rather than putting all your CSS in <style> tags. 我还强烈建议您使用外部CSS文件,而不是将所有CSS放在<style>标签中。

Try this below code and use CSS transition property instead of animation which looks simpler unlike animation 尝试下面的代码并使用CSS transition属性而不是animation ,它看起来比animation更简单

stickynav{ 
     background: #b50000;
     height: 46px;
     width: 1080px;
     margin-right: 0px;
     margin-left: 413px;
     left: 0px;
     right: 0px;
     position: relative;
     z-index: 9999;
     border-bottom: 4px solid #e50000;
     webkit-box-shadow: 0 2px 6px rgba(0,0,0,0.49);
     box-shadow: 0 2px 6px rgba(0,0,0,0.49);
     transition:width 500ms; //Add this
}
.fixed { 
    position:fixed;
    margin-left:0px;
    width:100%;
    top:0px;
}

You js would be: js将是:

$(window).scroll(function(){
        if ($(window).scrollTop() > aboveHeight)
           $('stickynav').addClass('fixed')
        else
           $('stickynav').removeClass('fixed')
});  

Working solution 工作方案

Try margin-left: 92px; 尝试margin-left: 92px; instead of margin-left: 413px 而不是margin-left: 413px

sticknav {
   background: #b50000;
   height: 46px;
   width: 1080px;
   margin-right: 0px;
   margin-left: 92px;
   left: 0px;
   right: 0px;
   position: relative;
   z-index: 9999;
   border-bottom: 4px solid #e50000;
   webkit-box-shadow: 0 2px 6px rgba(0,0,0,0.49);
   box-shadow: 0 2px 6px rgba(0,0,0,0.49);
   animation-name: fill;
   animation-duration: 12s;
}

Output 产量

在此输入图像描述

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

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