javascript - Navigation bar is not responsive after scrolling -


i new web development.i met problem when designing scrolling navigation bar.

i used bootstrap grid , jquery this. if don't scroll down , resize window, fine. navigation bar resized , in center of window. after scroll down, size of navigation bar fixed. size won't change when make window larger(it smaller if make window smaller, not centered). may caused setting width: nav.width() in js file, don't know how fix it.

this new account don't have enough reputation upload images. try describe using words. example, @ beginning, parent of navigation bar 1140px, width of navigation bar 100%, size 1140px. if resize window, let width of parent 720px, navigation bar width 720px. if scroll down, callback function in js run, set width 720px, fixed value, won't responsible more. now, if resize window make parent 1140px, width of navigation bar 720px rather 1140. how fix this?

    <div class="container">         <div class="row">             <div class="col-md-12">                 <header id="home" style="height:300px">                     <nav id="nav-wrap">                         <ul id="nav" class="nav">                             <li class="current">                                 <a href="">home</a>                             </li>                             <li class="smotthscroll">                                 <a href=""> </a>                             </li>                             <li class="smotthscroll">                                 <a href=""> portfolio </a>                             </li>                             <li class="smotthscroll">                                 <a href=""> skills </a>                             </li>                             <li class="smotthscroll">                                 <a href=""> contact </a>                             </li>                         </ul>                     </nav>                 </header>             </div>         </div>       </div> 

css

 #nav-wrap {     font: 22px 'opensans-bold', sans-serif;     width: 100%;     margin: 0 auto;     left: 0;     top: 0;  }       ul#nav {     text-align: center;     padding: 0;     width: 100%;     background: rgba(0, 0, 0, 0.3);     z-index: 2; }      ul#nav li {     height: 48px;     display: inline-block; }      ul#nav li {     color:white;     padding: 8px 13px;     display: inline-block;     text-align: center; }       $(function() {     var nav = $('#nav');     var navhomey = nav.offset().top;     var isfixed = false;     var navwrap = $('#nav-wrap');     var $w = $(window);     $w.scroll(function () {         var scrolltop = $w.scrolltop();         var shouldbefixed = scrolltop > navhomey;         if(shouldbefixed && !isfixed) {             nav.css({                 position: 'fixed',                 width: nav.width()             });             isfixed = true;         }else if (!shouldbefixed && isfixed) {             nav.css({                 position: 'static'             });             isfixed = false;         }     }); }); 

your problem comes part.

 var shouldbefixed = scrolltop > navhomey;         if(shouldbefixed && !isfixed) {             nav.css({                 position: 'fixed',                 width: nav.width()             });             isfixed = true; 

try inversing or removing part together. saying if navbar not @ top, add position of fixed nav.width(), whatever set equal - , precisely opposite of want because it's problem according question.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -