简体   繁体   English

淡化导航并使其固定在向下滚动,而淡入则在向上滚动

[英]Fading a nav and making it fixed on scroll down, fade on scroll up

I'm trying to get my navigation to fade out the changes that were faded in (ul li color, and background color). 我正在尝试让我的导航淡入淡出的更改(ul颜色和背景颜色)。 I want the nav to fade back to its original state but I'm having trouble with the animation fading out. 我希望导航器退回到其原始状态,但是动画淡出时遇到了麻烦。

I tried animating the removal of the class but that messes everything up! 我曾尝试为删除班级设置动画,但这使一切搞砸了!

fiddle: http://jsfiddle.net/vp7chr47/1/ 小提琴: http : //jsfiddle.net/vp7chr47/1/

HTML 的HTML

<div id="nav">
    <ul>
        <li>link</li>
        <li>link</li>
        <li>link</li>
    </ul>
</div>

JS JS

$(window).scroll(function () {
    var scroll_top = $(this).scrollTop();
    if (scroll_top >= 1) {
        $("#nav").addClass("nav-float");
    } else {
        setTimeout(function(){
            $("#nav").removeClass("nav-float");
        }, 1000 ).fadeOut("slow");
    }
});

CSS 的CSS

body {
    background: #000;
    padding: 0;
    margin: 0;
    color: #00ff00;
}
#nav {
    width: 100%;
    height: 80px;
    border: 1px solid #ff0000;
}
#nav ul li {
    display: inline;
}
.nav-float {
    position: fixed;
    background: #fff;
    transition: all 0.3s ease-in-out 0s;
    -moz-transition: all 0.3s ease-in-out 0s;
    -webkit-transition: all 0.3s ease-in-out 0s;
}
.nav-float ul li {
    color: #ff0000;
    transition: all 0.3s ease-in-out 0s;
    -moz-transition: all 0.3s ease-in-out 0s;
    -webkit-transition: all 0.3s ease-in-out 0s;
}

You could move the css transition from the .nav-float classes to the #nav classes. 您可以将CSS transition.nav-float类移动到#nav类。 Then you can simply just add or remove the class: 然后,您只需添加或删除类即可:

$(window).scroll(function () {
    var scroll_top = $(this).scrollTop();
    if (scroll_top >= 1) {
        $("#nav").addClass("nav-float");
    } else {
        $("#nav").removeClass("nav-float");
    }
});

Working fiddle is here 工作小提琴在这里

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

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