简体   繁体   English

导航菜单在滚动时淡入

[英]Navigation menu fade in on scroll

I'm designing a website and I'd like the navigation menu to fade in once I scroll down >50px. 我正在设计一个网站,当我向下滚动> 50px时,希望导航菜单消失。 I'm using the following JavaScript with JQuery library: 我在JQuery库中使用以下JavaScript:

(function ($) {
    $(document).ready(function () {

        $(window).scroll(function () {
            if ($(this).scrollTop() > 50) {
                $('.menu').fadeIn(500);
            } else {
                $('.menu').fadeOut(500);
            }
        });

    });
})(jQuery);

The class .menu is set on {display: none;} . .menu类在{display: none;}

This should work 这应该工作

$(document).ready(function(){
       $(window).bind('scroll', function() {
       var distance = 50;
             if ($(window).scrollTop() > distance) {
                 $('nav').fadeIn(500);
             }
             else {
                 $('nav').fadeOut(500);
             }
        });
    });

Codepen Demo Codepen演示

It's working for me. 它为我工作。

Your .menu is probably at the top of a page and when you scroll you can't see it. 您的.menu可能位于页面顶部,而滚动时则看不到它。

Add to test: 加入测试:

.menu {
    position: fixed;
    z-index: 10000; //just to check if it is behind the content
}

DEMO 演示

Try this: 尝试这个:

$(document).ready(function () {
    $(window).scroll(function () {
        if ($(this).scrollTop() > 50) {
            $('.menu').fadeIn(1000);
        } else {
            $('.menu').fadeOut(1000);
        }
    });
});

Just corrected your code! 刚刚更正了您的代码! It works fine! 工作正常!

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

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