简体   繁体   English

滚动后如何更改背景

[英]how can I change the background after scroll

I have a navbar with background transparent. 我有一个背景透明的导航栏。 How can make background black after scrol. 滚动后如何使背景变黑。

Here is template live 这是模板直播

and there is html with navbar 并且有带有导航栏的html

<nav class="navbar navbar-default navbar-fixed-top">
    <div class="container-fluid container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>       
            <a class="navbar-brand"><img src="img/logo.svg" class="brand"></a>                          
        </div>
        <div class="collapse navbar-collapse">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#about">Despre noi</a></li>
                <li><a href="#services">Servicii</a></li>
                <li><a href="#portfolio">Galerie</a></li>
                <li><a href="#contact">Contact</a></li>
            </ul>
        </div>
    </div>
</nav>

Do it with Jquery and add a class 使用Jquery并添加一个类

jQuery jQuery的

$(window).scroll(function() {    
    var scroll = $(window).scrollTop();

    if (scroll >= 1) { // Adjust with your needs
        $(".navbar").addClass("navbar-black");
    } else {
        $(".navbar").removeClass("navbar-black");
    }
});

CSS 的CSS

.navbar-black{
background: #000 !important;
}

so you should be able to achieve this with ScrollTop, Something like: 因此,您应该可以使用ScrollTop来实现此目的,例如:

onscroll = function() 
    {
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        if (scrollTop > 830) 
        {
    $('.navbar').css('background-color','black');
    }
    if (scrollTop < 830) 
        {
    $('.navbar').css('background-color','transparent');
    }
}

I would perhaps consider using an anchor rather than an absolute height. 我也许会考虑使用锚点而不是绝对高度。

If this is what you wanted. 如果这是您想要的。 :) :)

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

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