简体   繁体   English

我不知道我做错了什么,我想在页面滚动时更改导航栏的背景颜色

[英]I don't know what i am doing wrong, I want to change the background color of the navbar when the page gets scrolled

This is what i am doing, I used bootstrap to style this nav.这就是我正在做的,我使用引导程序来设计这个导航。 The nav basically collapses and a toggle button appears.导航基本上折叠并出现一个切换按钮。

<nav id="mainNavbar" class="navbar navbar-expand-lg navbar-dark py-0 fixed-top ">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">CANDY</a>

            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo01"
                aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation"
                class="btn btn-info">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarTogglerDemo01">
                <ul class="navbar-nav ">
                    <li class="nav-item">
                        <a class="nav-link" href="#">HOME</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">ABOUT</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">TICKETS</a>
                    </li>
                </ul>
            </div>
    </nav>

this is the css for styling it when scrolled.这是滚动时设置样式的 css。 Nothing else was used to style it没有其他东西用来设计它

#mainNavbar.scrolled{
    background-color: #ac3bac;
    transition: background 500ms;
}

But the problem is that it isn't toggling the class 'scrolled' to style it.但问题是它并没有切换“滚动”类来设置它的样式。 I have tried multiple approaches but none of them are working.我尝试了多种方法,但都没有奏效。 If possible pls help me complete it如果可能,请帮我完成它

<script>enter code here
        $(function () {
            $(document).scroll(function () {
                var $nav = $("#mainNavbar");
                $nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height());
            });
        });
    </script>

I just tried your code here , which i hope it will help you to focus on your problem.我刚刚在这里尝试了您的代码,希望它能帮助您专注于您的问题。

I have add some lorem in order to make the window scroll properly, in order to trigger the event.我添加了一些 lorem 以使窗口正确滚动,以触发事件。

<div>lorem...</div>

Everything should work fine, are you sure have you add all the proper scripts?一切正常,您确定添加了所有正确的脚本吗?

暂无
暂无

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

相关问题 试图更改活动标签颜色,不知道我在代码中做错了什么 - Trying to change active tab color, don't know what I am doing wrong in my code if/else 语句似乎在 Javascript 中不起作用,我不知道我做错了什么 - If/else statements just do not seem to be working in Javascript and i don't know what I am doing wrong 我正在执行切换,并且当切换功能启动时,我想更改div的背景颜色更改 - I am doing a toggle and I want to change the background color change of the div when the toggle function fired 如何更改Google Chart API的背景颜色,我在做什么错 - How to change the background color for google chart API, what am i doing wrong jQuery无法在html中工作,我不知道自己在做什么错 - jQuery not working in html and I don't know what i'm doing wrong 我不知道我在哪里错了Js对象 - I don't know where i am wrong with Js objects 我做错了什么? - What i am doing wrong? 使用递归交错两个数组(卡片组)......我能够在没有递归的情况下解决它,但想知道我做错了什么 - Interleave two arrays (card decks) using recursion... I was able to solve it without recursion but want to know what I am doing wrong 我不知道我做错了什么,但是我的带有 js 和 css 的 .html 不会显示必须显示的内容 - I don't know what I'm doing wrong but my .html with js and css won't show what is has to show HTML&Js颜色选择器:我在做什么错? - HTML & Js Color Picker: What am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM