Make a nav bar stick

前端 未结 11 1226
别跟我提以往
别跟我提以往 2020-11-29 19:44

Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make

相关标签:
11条回答
  • 2020-11-29 19:49

    I would recommend to use Bootstrap. http://getbootstrap.com/. This approach is very straight-forward and light weight.

    <div class="navbar navbar-inverse navbar-fixed-top">
       <div class="container">
          <div class="navbar-collapse collapse">
             <ul class="nav navbar-nav navbar-fixed-top">
                <li><a href="#home"> <br>BLINK</a></li>
                    <li><a href="#news"><br>ADVERTISING WITH BLINK</a></li>
                    <li><a href="#contact"><br>EDUCATING WITH BLINK</a></li>
                    <li><a href="#about"><br>ABOUT US</a></li>
                </ul>
            </div>
        </div>
    </div>
    

    You need to include the Bootstrap into your project, which will include the necessary scripts and styles. Then just call the class 'navbar-fixed-top'. This will do the trick. See above example

    0 讨论(0)
  • 2020-11-29 19:52

    add to your .nav css block the

    position: fixed
    

    and it will work

    0 讨论(0)
  • 2020-11-29 19:52

    I hope this can help someone. Determine the nav offset through js and then apply sticky position css to nav:

    But first, we will define the styles in the stylesheet, like so.

    .sticky {
        position: fixed;
        width: 100%;
        left: 0;
        top: 0;
        z-index: 100;
        border-top: 0;
    }
    

    Then, we will apply that class to the navigation conditionally with jQuery.

    $(document).ready(function() {
      var stickyNavTop = $('.nav').offset().top;
    
      var stickyNav = function(){
        var scrollTop = $(window).scrollTop();
    
        if (scrollTop > stickyNavTop) { 
          $('.nav').addClass('sticky');
        } else {
          $('.nav').removeClass('sticky'); 
        }
      };
    
      stickyNav();
    
      $(window).scroll(function() {
        stickyNav();
      });
    });
    
    0 讨论(0)
  • 2020-11-29 19:54

    Give headercss position fixed.

    .headercss {
        width: 100%;
        height: 320px;
        background-color: #000000;
        position: fixed;
        top:0
    }
    

    Then give the content container a 320px padding-top, so it doesn't get behind the header.

    0 讨论(0)
  • 2020-11-29 19:54
    /* Add css in your style */
    
    
    .sticky-header {
        position: fixed;
        width: 100%;
        left: 0;
        top: 0;
        z-index: 100;
        border-top: 0;
        transition: 0.3s;
    }
    
    
    /* and use this javascript code: */
    
    $(document).ready(function() {
    
      $(window).scroll(function () {
        if ($(window).scrollTop() > ) {
          $('.headercss').addClass('sticky-header');
        } else{
          $('.headercss').removeClass('sticky-header');
        }
      });
    });
    
    0 讨论(0)
  • 2020-11-29 19:57

    $(document).ready(function() {
      
      $(window).scroll(function () {
          //if you hard code, then use console
          //.log to determine when you want the 
          //nav bar to stick.  
          console.log($(window).scrollTop())
        if ($(window).scrollTop() > 280) {
          $('#nav_bar').addClass('navbar-fixed');
        }
        if ($(window).scrollTop() < 281) {
          $('#nav_bar').removeClass('navbar-fixed');
        }
      });
    });
    html, body {
    	height: 4000px;
    }
    
    .navbar-fixed {
        top: 0;
        z-index: 100;
      position: fixed;
        width: 100%;
    }
    
    #body_div {
    	top: 0;
    	position: relative;
        height: 200px;
        background-color: green;
    }
    
    #banner {
    	width: 100%;
    	height: 273px;
        background-color: gray;
    	overflow: hidden;
    }
    
    #nav_bar {
    	border: 0;
    	background-color: #202020;
    	border-radius: 0px;
    	margin-bottom: 0;
        height: 30px;
    }
    
    .nav_links {
        margin: 0;
    }
    
    .nav_links li {
    	display: inline-block;
        margin-top: 4px;
    }
    .nav_links li a {
    	padding: 0 15.5px;
    	color: #3498db;
    	text-decoration: none;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="banner">
         <h2>put what you want here</h2>
         <p>just adjust javascript size to match this window</p>
      </div>
    
      <nav id='nav_bar'>
        <ul class='nav_links'>
          <li><a href="url">Nav Bar</a></li>
          <li><a href="url">Sign In</a></li>
          <li><a href="url">Blog</a></li>
          <li><a href="url">About</a></li>
        </ul>
      </nav>
    <div id='body_div'>
        <p style='margin: 0; padding-top: 50px;'>and more stuff to continue scrolling here</p>
    </div>

    0 讨论(0)
提交回复
热议问题