简体   繁体   English

滚动更改Java滚动条不透明度的Javascript使我的Nav内容消失

[英]Javascript on scroll to change nav bar opacity is making my nav content dissapear

I've looked everywhere but I can't find an answer that applies to this problem. 我到处都看过,但是找不到适用于此问题的答案。

I have a sticky nav bar that becomes opaque upon scrolling, using this code: 我有一个粘性导航栏,使用以下代码在滚动时变得不透明:

    var target = $('#navbar-container');
var targetHeight = target.outerHeight();

$(document).scroll(function(e){
    var scrollPercent = (targetHeight - window.scrollY) / targetHeight;
    if(scrollPercent >= 0){
        target.css('opacity', 1);
    }
    else  target.css('opacity', 0.50);
});

But this code makes the background, and everything within the nav bar opaque, and I want the text links, and a logo to remain visible. 但是这个代码使得背景,导航栏不透明内的一切,我想要的文字链接和一个标志仍然可见。 I've read about using an RGB background to make only the background opaque, but I'm unsure how to do this within this code's parameters. 我已经读过有关使用RGB背景仅使背景不透明的信息,但是我不确定如何在此代码的参数范围内执行此操作。 I'm sure there's a simple solution, but I can't find it! 我敢肯定有一个简单的解决方案,但我找不到!

Use the background-color attribute instead of opacity. 使用background-color属性而不是不透明度。

target.css('background-color', 'rgba(0, 0, 0, 0.5)');

rgba(red, green, blue, alpha) rgba(红色,绿色,蓝色,alpha)

This is because the parent container will affect any children when using opacity. 这是因为使用不透明度时,父容器会影响任何子容器。

Use rgba . 使用rgba

CSS CSS

#navbar-container {
    background-color: rgba(255, 255, 255, 1);
}

JS JS

var target = $('#navbar-container');
var targetHeight = target.outerHeight();

$(document).scroll(function(e){
    var scrollPercent = (targetHeight - window.scrollY) / targetHeight;
    if(scrollPercent >= 0){
        target.css('background-color', 'rgba(255, 255, 255, 1)');
    }
    else  target.css('background-color', 'rgba(255, 255, 255, 0.5)');
});

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

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