Toggle navbar color on window scroll bootstrap + jquery



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am using bootstrap to create a navigation bar, I basically want to achieve two things



  1. When I scroll down my page 20% ,the navbar should change its color and again
    if i went top it should be back to its original color.



  2. When the bootstrap's collapse is on i.e. when the collapsed div can be seen,
    I want my div to remain in its original color irrespective of the window
    scroll.



    <div class="navbar-collapse nav-mobile-collapse collapse show" 
    id="navbarResponsive" style="">



HTML




<nav class="navbar-expand-lg navbar-dark fixed-top d-block d-lg-none">
<div id="nav-mobile" class=" d-flex flex-row nav-flex-row">
<div class="p-3 mr-auto"><img class="img img-fluid"
src="img/png/logo.png" /></div>
<div class="p-3">
<span class="navbar-toggler navbar-toggler-right" style="background-
color:transparent;color:white" data-toggle="collapse"
data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-
expanded="false" aria-label="Toggle navigation">
<i class="fa fa-bars fa-2x" aria-hidden="false" style="color:white">
</i>
</span>
</div>
</div>

<div class="collapse navbar-collapse nav-mobile-collapse"
id="navbarResponsive">
<div id="top-menu" class="navbar-container collapsebar-main">
<div style="flex:1" class="">
<a class="active js-scroll-trigger " href="#about">About</a>
</div>
<div style="flex:1">
<a class=" js-scroll-trigger" href="#product">Products</a>
</div>
<div style="flex:1">
<a class=" js-scroll-trigger" href="#partners">Partners</a>
</div>
<div style="flex:1" class="testimonial-nav">
<a class=" js-scroll-trigger" href="#testimonial">Testimonials</a>
</div>
</div>
</div>
</nav>



JAVASCRIPT




$(window).scroll(function()

document.getElementById('nav-mobile').style.webkitTransition = 'opacity
1s';
document.getElementById('nav-mobile').style.mozTransition = 'opacity 1s';

var windowpos = $(window).scrollTop();

$('#navbarResponsive')
.on('shown.bs.collapse', function()
document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
);

.on('hidden.bs.collapse', function()
if (windowpos > 50)
document.getElementById('nav-mobile').style.backgroundColor =
'#1c223f';

else
document.getElementById('nav-mobile').style.backgroundColor =
'TRANSPARENT';

);

if ( windowpos > 50)
document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';

);









share|improve this question






























    1















    I am using bootstrap to create a navigation bar, I basically want to achieve two things



    1. When I scroll down my page 20% ,the navbar should change its color and again
      if i went top it should be back to its original color.



    2. When the bootstrap's collapse is on i.e. when the collapsed div can be seen,
      I want my div to remain in its original color irrespective of the window
      scroll.



      <div class="navbar-collapse nav-mobile-collapse collapse show" 
      id="navbarResponsive" style="">



    HTML




    <nav class="navbar-expand-lg navbar-dark fixed-top d-block d-lg-none">
    <div id="nav-mobile" class=" d-flex flex-row nav-flex-row">
    <div class="p-3 mr-auto"><img class="img img-fluid"
    src="img/png/logo.png" /></div>
    <div class="p-3">
    <span class="navbar-toggler navbar-toggler-right" style="background-
    color:transparent;color:white" data-toggle="collapse"
    data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-
    expanded="false" aria-label="Toggle navigation">
    <i class="fa fa-bars fa-2x" aria-hidden="false" style="color:white">
    </i>
    </span>
    </div>
    </div>

    <div class="collapse navbar-collapse nav-mobile-collapse"
    id="navbarResponsive">
    <div id="top-menu" class="navbar-container collapsebar-main">
    <div style="flex:1" class="">
    <a class="active js-scroll-trigger " href="#about">About</a>
    </div>
    <div style="flex:1">
    <a class=" js-scroll-trigger" href="#product">Products</a>
    </div>
    <div style="flex:1">
    <a class=" js-scroll-trigger" href="#partners">Partners</a>
    </div>
    <div style="flex:1" class="testimonial-nav">
    <a class=" js-scroll-trigger" href="#testimonial">Testimonials</a>
    </div>
    </div>
    </div>
    </nav>



    JAVASCRIPT




    $(window).scroll(function()

    document.getElementById('nav-mobile').style.webkitTransition = 'opacity
    1s';
    document.getElementById('nav-mobile').style.mozTransition = 'opacity 1s';

    var windowpos = $(window).scrollTop();

    $('#navbarResponsive')
    .on('shown.bs.collapse', function()
    document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
    );

    .on('hidden.bs.collapse', function()
    if (windowpos > 50)
    document.getElementById('nav-mobile').style.backgroundColor =
    '#1c223f';

    else
    document.getElementById('nav-mobile').style.backgroundColor =
    'TRANSPARENT';

    );

    if ( windowpos > 50)
    document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';

    );









    share|improve this question


























      1












      1








      1








      I am using bootstrap to create a navigation bar, I basically want to achieve two things



      1. When I scroll down my page 20% ,the navbar should change its color and again
        if i went top it should be back to its original color.



      2. When the bootstrap's collapse is on i.e. when the collapsed div can be seen,
        I want my div to remain in its original color irrespective of the window
        scroll.



        <div class="navbar-collapse nav-mobile-collapse collapse show" 
        id="navbarResponsive" style="">



      HTML




      <nav class="navbar-expand-lg navbar-dark fixed-top d-block d-lg-none">
      <div id="nav-mobile" class=" d-flex flex-row nav-flex-row">
      <div class="p-3 mr-auto"><img class="img img-fluid"
      src="img/png/logo.png" /></div>
      <div class="p-3">
      <span class="navbar-toggler navbar-toggler-right" style="background-
      color:transparent;color:white" data-toggle="collapse"
      data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-
      expanded="false" aria-label="Toggle navigation">
      <i class="fa fa-bars fa-2x" aria-hidden="false" style="color:white">
      </i>
      </span>
      </div>
      </div>

      <div class="collapse navbar-collapse nav-mobile-collapse"
      id="navbarResponsive">
      <div id="top-menu" class="navbar-container collapsebar-main">
      <div style="flex:1" class="">
      <a class="active js-scroll-trigger " href="#about">About</a>
      </div>
      <div style="flex:1">
      <a class=" js-scroll-trigger" href="#product">Products</a>
      </div>
      <div style="flex:1">
      <a class=" js-scroll-trigger" href="#partners">Partners</a>
      </div>
      <div style="flex:1" class="testimonial-nav">
      <a class=" js-scroll-trigger" href="#testimonial">Testimonials</a>
      </div>
      </div>
      </div>
      </nav>



      JAVASCRIPT




      $(window).scroll(function()

      document.getElementById('nav-mobile').style.webkitTransition = 'opacity
      1s';
      document.getElementById('nav-mobile').style.mozTransition = 'opacity 1s';

      var windowpos = $(window).scrollTop();

      $('#navbarResponsive')
      .on('shown.bs.collapse', function()
      document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
      );

      .on('hidden.bs.collapse', function()
      if (windowpos > 50)
      document.getElementById('nav-mobile').style.backgroundColor =
      '#1c223f';

      else
      document.getElementById('nav-mobile').style.backgroundColor =
      'TRANSPARENT';

      );

      if ( windowpos > 50)
      document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';

      );









      share|improve this question
















      I am using bootstrap to create a navigation bar, I basically want to achieve two things



      1. When I scroll down my page 20% ,the navbar should change its color and again
        if i went top it should be back to its original color.



      2. When the bootstrap's collapse is on i.e. when the collapsed div can be seen,
        I want my div to remain in its original color irrespective of the window
        scroll.



        <div class="navbar-collapse nav-mobile-collapse collapse show" 
        id="navbarResponsive" style="">



      HTML




      <nav class="navbar-expand-lg navbar-dark fixed-top d-block d-lg-none">
      <div id="nav-mobile" class=" d-flex flex-row nav-flex-row">
      <div class="p-3 mr-auto"><img class="img img-fluid"
      src="img/png/logo.png" /></div>
      <div class="p-3">
      <span class="navbar-toggler navbar-toggler-right" style="background-
      color:transparent;color:white" data-toggle="collapse"
      data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-
      expanded="false" aria-label="Toggle navigation">
      <i class="fa fa-bars fa-2x" aria-hidden="false" style="color:white">
      </i>
      </span>
      </div>
      </div>

      <div class="collapse navbar-collapse nav-mobile-collapse"
      id="navbarResponsive">
      <div id="top-menu" class="navbar-container collapsebar-main">
      <div style="flex:1" class="">
      <a class="active js-scroll-trigger " href="#about">About</a>
      </div>
      <div style="flex:1">
      <a class=" js-scroll-trigger" href="#product">Products</a>
      </div>
      <div style="flex:1">
      <a class=" js-scroll-trigger" href="#partners">Partners</a>
      </div>
      <div style="flex:1" class="testimonial-nav">
      <a class=" js-scroll-trigger" href="#testimonial">Testimonials</a>
      </div>
      </div>
      </div>
      </nav>



      JAVASCRIPT




      $(window).scroll(function()

      document.getElementById('nav-mobile').style.webkitTransition = 'opacity
      1s';
      document.getElementById('nav-mobile').style.mozTransition = 'opacity 1s';

      var windowpos = $(window).scrollTop();

      $('#navbarResponsive')
      .on('shown.bs.collapse', function()
      document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
      );

      .on('hidden.bs.collapse', function()
      if (windowpos > 50)
      document.getElementById('nav-mobile').style.backgroundColor =
      '#1c223f';

      else
      document.getElementById('nav-mobile').style.backgroundColor =
      'TRANSPARENT';

      );

      if ( windowpos > 50)
      document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';

      );






      javascript jquery html css bootstrap-4






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 4:26







      Shoyeb Memon

















      asked Nov 15 '18 at 13:09









      Shoyeb MemonShoyeb Memon

      412311




      412311






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You need to move $('#navbarResponsive') out of scroll because it will intialize only when window is scrolling.



          I have changed few things here is the demo of it.






          $(window).scroll(function() $("#navbarResponsive").hasClass("show")) 
          document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
          else
          document.getElementById('nav-mobile').style.backgroundColor = '';

          );

          $('#navbarResponsive')
          .on('shown.bs.collapse', function()
          document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
          ).on('hidden.bs.collapse', function()
          var windowpos = $(window).scrollTop();
          if (windowpos > 50)
          document.getElementById('nav-mobile').style.backgroundColor =
          '#1c223f';
          else
          document.getElementById('nav-mobile').style.backgroundColor =
          '';

          );

          body 
          height: 1000px

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />

          <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
          <nav class="navbar-expand-lg navbar-dark fixed-top d-block d-lg-none">
          <div id="nav-mobile" class=" d-flex flex-row nav-flex-row">
          <div class="p-3 mr-auto"><img class="img img-fluid" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSVCT-lYaIPfWsXbMZpFleT5-m-lA4Qbs9YhG44nDGAw7XEYjqB" /></div>
          <div class="p-3">

          <span class="navbar-toggler navbar-toggler-right" style="background-
          color:transparent;color:white" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria- expanded="false" aria-label="Toggle navigation">
          <i class="fa fa-bars fa-2x" aria-hidden="false" style="color:white">
          </i>
          </span>
          </div>
          </div>
          <div class="collapse navbar-collapse nav-mobile-collapse" id="navbarResponsive">
          <div id="top-menu" class="navbar-container collapsebar-main">
          <div style="flex:1" class="">
          <a class="active js-scroll-trigger " href="#about">About</a>
          </div>
          <div style="flex:1">
          <a class=" js-scroll-trigger" href="#product">Products</a>
          </div>
          <div style="flex:1">
          <a class=" js-scroll-trigger" href="#partners">Partners</a>
          </div>
          <div style="flex:1" class="testimonial-nav">
          <a class=" js-scroll-trigger" href="#testimonial">Testimonials</a>
          </div>
          </div>
          </div>
          </nav>








          share|improve this answer

























          • The actual output that i want is my navbar div reacting to window scroll to remove and add color (transparent if scroll 0-50 else blue) + when the bootstrap's shown.bs.collapse is true i don't want my navbar div to be effected by the scroll effect i.e if the collapse bar is open it should remain blue whether scroll is 0 or 1000

            – Shoyeb Memon
            Nov 16 '18 at 5:01











          • @ShoyebMemon that looks easier then this, I have updated my code, Please check

            – Just code
            Nov 16 '18 at 5:06











          • oh i totally forgot about the class show , anyway thanks it worked.

            – Shoyeb Memon
            Nov 16 '18 at 5:13











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320244%2ftoggle-navbar-color-on-window-scroll-bootstrap-jquery%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You need to move $('#navbarResponsive') out of scroll because it will intialize only when window is scrolling.



          I have changed few things here is the demo of it.






          $(window).scroll(function() $("#navbarResponsive").hasClass("show")) 
          document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
          else
          document.getElementById('nav-mobile').style.backgroundColor = '';

          );

          $('#navbarResponsive')
          .on('shown.bs.collapse', function()
          document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
          ).on('hidden.bs.collapse', function()
          var windowpos = $(window).scrollTop();
          if (windowpos > 50)
          document.getElementById('nav-mobile').style.backgroundColor =
          '#1c223f';
          else
          document.getElementById('nav-mobile').style.backgroundColor =
          '';

          );

          body 
          height: 1000px

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />

          <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
          <nav class="navbar-expand-lg navbar-dark fixed-top d-block d-lg-none">
          <div id="nav-mobile" class=" d-flex flex-row nav-flex-row">
          <div class="p-3 mr-auto"><img class="img img-fluid" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSVCT-lYaIPfWsXbMZpFleT5-m-lA4Qbs9YhG44nDGAw7XEYjqB" /></div>
          <div class="p-3">

          <span class="navbar-toggler navbar-toggler-right" style="background-
          color:transparent;color:white" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria- expanded="false" aria-label="Toggle navigation">
          <i class="fa fa-bars fa-2x" aria-hidden="false" style="color:white">
          </i>
          </span>
          </div>
          </div>
          <div class="collapse navbar-collapse nav-mobile-collapse" id="navbarResponsive">
          <div id="top-menu" class="navbar-container collapsebar-main">
          <div style="flex:1" class="">
          <a class="active js-scroll-trigger " href="#about">About</a>
          </div>
          <div style="flex:1">
          <a class=" js-scroll-trigger" href="#product">Products</a>
          </div>
          <div style="flex:1">
          <a class=" js-scroll-trigger" href="#partners">Partners</a>
          </div>
          <div style="flex:1" class="testimonial-nav">
          <a class=" js-scroll-trigger" href="#testimonial">Testimonials</a>
          </div>
          </div>
          </div>
          </nav>








          share|improve this answer

























          • The actual output that i want is my navbar div reacting to window scroll to remove and add color (transparent if scroll 0-50 else blue) + when the bootstrap's shown.bs.collapse is true i don't want my navbar div to be effected by the scroll effect i.e if the collapse bar is open it should remain blue whether scroll is 0 or 1000

            – Shoyeb Memon
            Nov 16 '18 at 5:01











          • @ShoyebMemon that looks easier then this, I have updated my code, Please check

            – Just code
            Nov 16 '18 at 5:06











          • oh i totally forgot about the class show , anyway thanks it worked.

            – Shoyeb Memon
            Nov 16 '18 at 5:13















          0














          You need to move $('#navbarResponsive') out of scroll because it will intialize only when window is scrolling.



          I have changed few things here is the demo of it.






          $(window).scroll(function() $("#navbarResponsive").hasClass("show")) 
          document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
          else
          document.getElementById('nav-mobile').style.backgroundColor = '';

          );

          $('#navbarResponsive')
          .on('shown.bs.collapse', function()
          document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
          ).on('hidden.bs.collapse', function()
          var windowpos = $(window).scrollTop();
          if (windowpos > 50)
          document.getElementById('nav-mobile').style.backgroundColor =
          '#1c223f';
          else
          document.getElementById('nav-mobile').style.backgroundColor =
          '';

          );

          body 
          height: 1000px

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />

          <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
          <nav class="navbar-expand-lg navbar-dark fixed-top d-block d-lg-none">
          <div id="nav-mobile" class=" d-flex flex-row nav-flex-row">
          <div class="p-3 mr-auto"><img class="img img-fluid" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSVCT-lYaIPfWsXbMZpFleT5-m-lA4Qbs9YhG44nDGAw7XEYjqB" /></div>
          <div class="p-3">

          <span class="navbar-toggler navbar-toggler-right" style="background-
          color:transparent;color:white" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria- expanded="false" aria-label="Toggle navigation">
          <i class="fa fa-bars fa-2x" aria-hidden="false" style="color:white">
          </i>
          </span>
          </div>
          </div>
          <div class="collapse navbar-collapse nav-mobile-collapse" id="navbarResponsive">
          <div id="top-menu" class="navbar-container collapsebar-main">
          <div style="flex:1" class="">
          <a class="active js-scroll-trigger " href="#about">About</a>
          </div>
          <div style="flex:1">
          <a class=" js-scroll-trigger" href="#product">Products</a>
          </div>
          <div style="flex:1">
          <a class=" js-scroll-trigger" href="#partners">Partners</a>
          </div>
          <div style="flex:1" class="testimonial-nav">
          <a class=" js-scroll-trigger" href="#testimonial">Testimonials</a>
          </div>
          </div>
          </div>
          </nav>








          share|improve this answer

























          • The actual output that i want is my navbar div reacting to window scroll to remove and add color (transparent if scroll 0-50 else blue) + when the bootstrap's shown.bs.collapse is true i don't want my navbar div to be effected by the scroll effect i.e if the collapse bar is open it should remain blue whether scroll is 0 or 1000

            – Shoyeb Memon
            Nov 16 '18 at 5:01











          • @ShoyebMemon that looks easier then this, I have updated my code, Please check

            – Just code
            Nov 16 '18 at 5:06











          • oh i totally forgot about the class show , anyway thanks it worked.

            – Shoyeb Memon
            Nov 16 '18 at 5:13













          0












          0








          0







          You need to move $('#navbarResponsive') out of scroll because it will intialize only when window is scrolling.



          I have changed few things here is the demo of it.






          $(window).scroll(function() $("#navbarResponsive").hasClass("show")) 
          document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
          else
          document.getElementById('nav-mobile').style.backgroundColor = '';

          );

          $('#navbarResponsive')
          .on('shown.bs.collapse', function()
          document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
          ).on('hidden.bs.collapse', function()
          var windowpos = $(window).scrollTop();
          if (windowpos > 50)
          document.getElementById('nav-mobile').style.backgroundColor =
          '#1c223f';
          else
          document.getElementById('nav-mobile').style.backgroundColor =
          '';

          );

          body 
          height: 1000px

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />

          <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
          <nav class="navbar-expand-lg navbar-dark fixed-top d-block d-lg-none">
          <div id="nav-mobile" class=" d-flex flex-row nav-flex-row">
          <div class="p-3 mr-auto"><img class="img img-fluid" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSVCT-lYaIPfWsXbMZpFleT5-m-lA4Qbs9YhG44nDGAw7XEYjqB" /></div>
          <div class="p-3">

          <span class="navbar-toggler navbar-toggler-right" style="background-
          color:transparent;color:white" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria- expanded="false" aria-label="Toggle navigation">
          <i class="fa fa-bars fa-2x" aria-hidden="false" style="color:white">
          </i>
          </span>
          </div>
          </div>
          <div class="collapse navbar-collapse nav-mobile-collapse" id="navbarResponsive">
          <div id="top-menu" class="navbar-container collapsebar-main">
          <div style="flex:1" class="">
          <a class="active js-scroll-trigger " href="#about">About</a>
          </div>
          <div style="flex:1">
          <a class=" js-scroll-trigger" href="#product">Products</a>
          </div>
          <div style="flex:1">
          <a class=" js-scroll-trigger" href="#partners">Partners</a>
          </div>
          <div style="flex:1" class="testimonial-nav">
          <a class=" js-scroll-trigger" href="#testimonial">Testimonials</a>
          </div>
          </div>
          </div>
          </nav>








          share|improve this answer















          You need to move $('#navbarResponsive') out of scroll because it will intialize only when window is scrolling.



          I have changed few things here is the demo of it.






          $(window).scroll(function() $("#navbarResponsive").hasClass("show")) 
          document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
          else
          document.getElementById('nav-mobile').style.backgroundColor = '';

          );

          $('#navbarResponsive')
          .on('shown.bs.collapse', function()
          document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
          ).on('hidden.bs.collapse', function()
          var windowpos = $(window).scrollTop();
          if (windowpos > 50)
          document.getElementById('nav-mobile').style.backgroundColor =
          '#1c223f';
          else
          document.getElementById('nav-mobile').style.backgroundColor =
          '';

          );

          body 
          height: 1000px

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />

          <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
          <nav class="navbar-expand-lg navbar-dark fixed-top d-block d-lg-none">
          <div id="nav-mobile" class=" d-flex flex-row nav-flex-row">
          <div class="p-3 mr-auto"><img class="img img-fluid" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSVCT-lYaIPfWsXbMZpFleT5-m-lA4Qbs9YhG44nDGAw7XEYjqB" /></div>
          <div class="p-3">

          <span class="navbar-toggler navbar-toggler-right" style="background-
          color:transparent;color:white" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria- expanded="false" aria-label="Toggle navigation">
          <i class="fa fa-bars fa-2x" aria-hidden="false" style="color:white">
          </i>
          </span>
          </div>
          </div>
          <div class="collapse navbar-collapse nav-mobile-collapse" id="navbarResponsive">
          <div id="top-menu" class="navbar-container collapsebar-main">
          <div style="flex:1" class="">
          <a class="active js-scroll-trigger " href="#about">About</a>
          </div>
          <div style="flex:1">
          <a class=" js-scroll-trigger" href="#product">Products</a>
          </div>
          <div style="flex:1">
          <a class=" js-scroll-trigger" href="#partners">Partners</a>
          </div>
          <div style="flex:1" class="testimonial-nav">
          <a class=" js-scroll-trigger" href="#testimonial">Testimonials</a>
          </div>
          </div>
          </div>
          </nav>








          $(window).scroll(function() $("#navbarResponsive").hasClass("show")) 
          document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
          else
          document.getElementById('nav-mobile').style.backgroundColor = '';

          );

          $('#navbarResponsive')
          .on('shown.bs.collapse', function()
          document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
          ).on('hidden.bs.collapse', function()
          var windowpos = $(window).scrollTop();
          if (windowpos > 50)
          document.getElementById('nav-mobile').style.backgroundColor =
          '#1c223f';
          else
          document.getElementById('nav-mobile').style.backgroundColor =
          '';

          );

          body 
          height: 1000px

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />

          <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
          <nav class="navbar-expand-lg navbar-dark fixed-top d-block d-lg-none">
          <div id="nav-mobile" class=" d-flex flex-row nav-flex-row">
          <div class="p-3 mr-auto"><img class="img img-fluid" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSVCT-lYaIPfWsXbMZpFleT5-m-lA4Qbs9YhG44nDGAw7XEYjqB" /></div>
          <div class="p-3">

          <span class="navbar-toggler navbar-toggler-right" style="background-
          color:transparent;color:white" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria- expanded="false" aria-label="Toggle navigation">
          <i class="fa fa-bars fa-2x" aria-hidden="false" style="color:white">
          </i>
          </span>
          </div>
          </div>
          <div class="collapse navbar-collapse nav-mobile-collapse" id="navbarResponsive">
          <div id="top-menu" class="navbar-container collapsebar-main">
          <div style="flex:1" class="">
          <a class="active js-scroll-trigger " href="#about">About</a>
          </div>
          <div style="flex:1">
          <a class=" js-scroll-trigger" href="#product">Products</a>
          </div>
          <div style="flex:1">
          <a class=" js-scroll-trigger" href="#partners">Partners</a>
          </div>
          <div style="flex:1" class="testimonial-nav">
          <a class=" js-scroll-trigger" href="#testimonial">Testimonials</a>
          </div>
          </div>
          </div>
          </nav>





          $(window).scroll(function() $("#navbarResponsive").hasClass("show")) 
          document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
          else
          document.getElementById('nav-mobile').style.backgroundColor = '';

          );

          $('#navbarResponsive')
          .on('shown.bs.collapse', function()
          document.getElementById('nav-mobile').style.backgroundColor = '#1c223f';
          ).on('hidden.bs.collapse', function()
          var windowpos = $(window).scrollTop();
          if (windowpos > 50)
          document.getElementById('nav-mobile').style.backgroundColor =
          '#1c223f';
          else
          document.getElementById('nav-mobile').style.backgroundColor =
          '';

          );

          body 
          height: 1000px

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />

          <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
          <nav class="navbar-expand-lg navbar-dark fixed-top d-block d-lg-none">
          <div id="nav-mobile" class=" d-flex flex-row nav-flex-row">
          <div class="p-3 mr-auto"><img class="img img-fluid" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSVCT-lYaIPfWsXbMZpFleT5-m-lA4Qbs9YhG44nDGAw7XEYjqB" /></div>
          <div class="p-3">

          <span class="navbar-toggler navbar-toggler-right" style="background-
          color:transparent;color:white" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria- expanded="false" aria-label="Toggle navigation">
          <i class="fa fa-bars fa-2x" aria-hidden="false" style="color:white">
          </i>
          </span>
          </div>
          </div>
          <div class="collapse navbar-collapse nav-mobile-collapse" id="navbarResponsive">
          <div id="top-menu" class="navbar-container collapsebar-main">
          <div style="flex:1" class="">
          <a class="active js-scroll-trigger " href="#about">About</a>
          </div>
          <div style="flex:1">
          <a class=" js-scroll-trigger" href="#product">Products</a>
          </div>
          <div style="flex:1">
          <a class=" js-scroll-trigger" href="#partners">Partners</a>
          </div>
          <div style="flex:1" class="testimonial-nav">
          <a class=" js-scroll-trigger" href="#testimonial">Testimonials</a>
          </div>
          </div>
          </div>
          </nav>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 5:05

























          answered Nov 16 '18 at 4:48









          Just codeJust code

          10.5k53267




          10.5k53267












          • The actual output that i want is my navbar div reacting to window scroll to remove and add color (transparent if scroll 0-50 else blue) + when the bootstrap's shown.bs.collapse is true i don't want my navbar div to be effected by the scroll effect i.e if the collapse bar is open it should remain blue whether scroll is 0 or 1000

            – Shoyeb Memon
            Nov 16 '18 at 5:01











          • @ShoyebMemon that looks easier then this, I have updated my code, Please check

            – Just code
            Nov 16 '18 at 5:06











          • oh i totally forgot about the class show , anyway thanks it worked.

            – Shoyeb Memon
            Nov 16 '18 at 5:13

















          • The actual output that i want is my navbar div reacting to window scroll to remove and add color (transparent if scroll 0-50 else blue) + when the bootstrap's shown.bs.collapse is true i don't want my navbar div to be effected by the scroll effect i.e if the collapse bar is open it should remain blue whether scroll is 0 or 1000

            – Shoyeb Memon
            Nov 16 '18 at 5:01











          • @ShoyebMemon that looks easier then this, I have updated my code, Please check

            – Just code
            Nov 16 '18 at 5:06











          • oh i totally forgot about the class show , anyway thanks it worked.

            – Shoyeb Memon
            Nov 16 '18 at 5:13
















          The actual output that i want is my navbar div reacting to window scroll to remove and add color (transparent if scroll 0-50 else blue) + when the bootstrap's shown.bs.collapse is true i don't want my navbar div to be effected by the scroll effect i.e if the collapse bar is open it should remain blue whether scroll is 0 or 1000

          – Shoyeb Memon
          Nov 16 '18 at 5:01





          The actual output that i want is my navbar div reacting to window scroll to remove and add color (transparent if scroll 0-50 else blue) + when the bootstrap's shown.bs.collapse is true i don't want my navbar div to be effected by the scroll effect i.e if the collapse bar is open it should remain blue whether scroll is 0 or 1000

          – Shoyeb Memon
          Nov 16 '18 at 5:01













          @ShoyebMemon that looks easier then this, I have updated my code, Please check

          – Just code
          Nov 16 '18 at 5:06





          @ShoyebMemon that looks easier then this, I have updated my code, Please check

          – Just code
          Nov 16 '18 at 5:06













          oh i totally forgot about the class show , anyway thanks it worked.

          – Shoyeb Memon
          Nov 16 '18 at 5:13





          oh i totally forgot about the class show , anyway thanks it worked.

          – Shoyeb Memon
          Nov 16 '18 at 5:13



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320244%2ftoggle-navbar-color-on-window-scroll-bootstrap-jquery%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo