透明导航栏未在加载时激活

时间:2018-01-20 21:07:12

标签: jquery css navbar

我正在尝试让页面顶部的导航栏保持透明,直到用户开始滚动,但是当页面加载时,会出现无样式的灰色。在用户向上滚动之前,透明性质不会激活。首次加载页面时,如何让导航栏保持透明,我需要做什么?

HTML

<div class="container">
        <nav class="navbar navbar-default navbar-fixed-top">
            <div class="container-fluid">
                <div class="navbar-header">
                    <a class="navbar-brand" href="#">AE</a>
                </div>

                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="#">About Me</a>
                        <a href="#">My Life Before Code</a>
                        <a href="#">Portfolio</a>
                        <a href="#">Contact</a>
                    </li>
                </ul>
            </div>
        </nav>
    </div>

    <div class="container-full-big">
        <div class="jumbotron">
            <h1>Hi. I'm Ashley.</h1>
        </div>
    </div>

CSS

.container-fluid {
    height: 60px;
}

.navbar-default {
    border: none;
}

.navbar-default .navbar-brand {
    font-family: 'Lobster', cursive;
    font-size: 30px;
    color: #ffffff;
}

.navbar-default .navbar-nav>li>a {
    float: left;
    font-size: 20px;
    color: #ffffff;
    padding-top: 20px;
}

.jumbotron {
    background: url("img/sunset.jpg") center center no-repeat;
    font-family: 'Lobster', cursive;
    color: #ffffff;
    background-size: cover;
    height: 100vh;
    padding: 0;
}

JS

 $(window).scroll(function () {
        if ($(window).scrollTop() > 50) {
            $(".navbar-default").css("background", "#443e41");
        } else {
            $(".navbar-default").css("background", "transparent");
        }
    });

1 个答案:

答案 0 :(得分:0)

您需要在transparent

上应用$(document).ready()代码部分
 $(document).ready(function(){
  $(".navbar-default").css("background", "transparent");
});

您也可以尝试$('window').load(): -

$('window').load(function(){
  $(".navbar-default").css("background", "transparent");
});