简体   繁体   English

导航栏不透明度/滚动更改

[英]navbar opacity/rgba change on scroll

I've been trying to create a nav that would be transparent at the top and would gain white color as the user scrolls down the page. 我一直在尝试创建一个导航栏,该导航栏在顶部是透明的,并且在用户向下滚动页面时将获得白色。 My header height is 800px and I want my nav to lose 100% of transparency after those 800px . 我的标题高度为800px ,但我希望导航器在这些800px之后失去100%的透明度。 Here`s my code: 这是我的代码:

<header id="header">
   <nav class="navbar">
       <ul class="navigation">
           <li><a href="#header">Home</a></li>
           <li><a href="">About us</a></li>
           <li><a href="">Our qualities</a></li>
           <li><a href="">Contact us</a></li>
           <li><a href="">contact us</a></li>
       </ul>
   </nav>
nav  {
    width: 1600px;
    position: fixed;
    top: 0; 

    ul {
        margin: 0 auto;

        li {
            display: inline-block;
            padding: 5px 20px;

            a {
                font-family:  $f1;
                font-size: 16pt;
                color: $c3;
            }
        }
    }
}
}

First I tried with opacity, but it didn't work, and on top of that child elements ( ul and li ) had opacity of 0 as well. 首先,我尝试使用不透明度,但是它不起作用,并且在那个子元素( ulli )之上,不透明度也为0。 Here`s the JS for that: 这是为此的JS:

jQuery(document).ready( function() {        
    var navOffset = jQuery("nav").offset().top;

    jQuery(window).scroll(function() {
        var scrollPos = jQuery(window).scrollTop();
        var navOpacity = scrollPos /800;
        jQuery('.navbar').css(opacity, 'navOpacity');

        if (jQuery('nav').css('opacity') < 1) {
            jQuery('.navigation').css('opacity', '1')  
        };

Then I tried to change RGBA value on scroll, that didn't work either Instead of 然后我尝试在滚动上更改RGBA值,但也没有用

jQuery('.navbar').css( opacity, 'navOpacity' ); 

I used 我用了

jQuery('.navbar').css(backgroundcolor, 'rgba (255, 255, 255, + "navOpacity")');

That failed as well, so, I have to ask you too help me 那也失败了,所以,我不得不问你也帮我

You have made opacity not a string, but the variable navOpacity has become a string . 您已将opacity不是字符串,但是变量navOpacity已变为string That was wrong. 错了 Everything else is working fine in general. 总体而言,其他所有工作都很好。 :) :)

// change
$('.navbar').css(opacity, "navOpacity");

// to
$('.navbar').css("opacity", navOpacity);

Working example. 工作示例。

The issue in your code is that you're providing navOpacity as a string to css() , instead of the variable itself. 代码中的问题是,您将navOpacity作为字符串提供给css() ,而不是变量本身。 Try this: 尝试这个:

$('.navbar').css('opacity', navOpacity);

Also note that your current logic is backwards to what you describe as your goal (the header starts transparent and becomes opaque at 800px) and the logic can also be simplified a lot. 还要注意,您当前的逻辑倒退至您所描述的目标(标头开始透明,在800px处变为不透明),并且逻辑也可以简化很多。 Try this: 尝试这个:

$(window).scroll(function() {
    var pc = $(this).scrollTop() / 800;
    $('.navbar').css('opacity', 1 - pc);
});

Working example 工作实例

Alternatively you could use jquery method .fadeTo() instead of css('opacity') . 或者,您可以使用jquery方法.fadeTo()代替css('opacity') This method animates the opacity of the elements smoothly. 此方法可平滑地使元素的不透明度动画化。 It is easier to use and the animation is pretty good looking compared to instant opacity change. 与即时不透明度更改相比,它更易于使用,并且动画外观也不错。

jQuery('.navbar').fadeTo( "slow" , navOpacity);

if (jQuery('nav').css('opacity') < 1) {
  jQuery('.navigation').fadeTo( "slow" , 1);
};

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

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