Skip to content Skip to sidebar Skip to footer

Sticky Navbar Onscroll?

I'm currently building a site for myself, and I found this really awesome effect on multiple sites where the navbar is below an image, but when you scroll past it, it sticks to the

Solution 1:

Here is what @Matthew was talking about:

Check this fiddle http://jsfiddle.net/luckmattos/yp8HK/1/

JQUERY

var num = 200; //number of pixels before modifying styles

$(window).bind('scroll', function () {
    if ($(window).scrollTop() > num) {
        $('.menu').addClass('fixed');
    } else {
        $('.menu').removeClass('fixed');
    }
});

Hope it helps, I used bootstrap navbar.

Solution 2:

Here are some few examples

With in bootstrap you can use navbar-fixed-top class.

<navclass="navbar navbar-default navbar-fixed-top"role="navigation"><divclass="container">
    ...
  </div></nav>

Have a look at following links:

Fixed to Top : http://getbootstrap.com/components/#navbar-fixed-top

Fixed Navbar example : http://getbootstrap.com/examples/navbar-fixed-top/

Solution 3:

Here is a REAL quick example I threw together with jquery and some very simple css.

JS

$(window).on('scroll', function(){
    if($(window).scrollTop()>=95 && !$('nav').hasClass('fixed')){
        $('nav').addClass('fixed'); 
    }
    elseif($(window).scrollTop()<95 && $('nav').hasClass('fixed')){
       $('nav').removeClass('fixed') 
    }
});

CSS

.container {
  height: 1800px;
}

.header {
  text-align: center;
}

nav {
  background-color: #efefef;
  border-top: 2px solid #666;
  border-bottom: 2px solid #666;
  padding: 15px;
  width: 100%;
}
navul {
  margin: 0;
  padding: 0;
  list-style: none;
}
navli {
  display: inline-block;
}

.fixed {
  position: fixed;
  top: 0;
  left: 0;
}

HTML

<divclass="container"><divclass="header"><imgsrc="http://www.placehold.it/500x85"alt="" /><navrole='navigation'><ul><li><ahref="#">Home</a></li><li><ahref="#">About</a></li><li><ahref="#">Clients</a></li><li><ahref="#">Contact Us</a></li></ul></nav></div></div>

Solution 4:

Here is an example of sticky navigation bar On-Scroll with dynamic height getting

JS

You need to add a jQuery Library file first.

Here is fiddle link- https://jsfiddle.net/CV_pawan/4nr4codq/1/

$(document).ready(function(){
  var num = $(".header-class").offset().top; 
  $(window).bind('scroll', function() { 
    if ($(window).scrollTop() > num) {  
      $('.header-class').addClass('fixed');  
    } 
    else {  
     $('.header-class').removeClass('fixed');    
    } 
  });
});

CSS

body {
  margin: 0px;
  padding: 0px;
}

.nav-bar-white {
  background-color: #125456;
  width: 100%;
}

.active-2 {
  float: right;
  width: 49%;
  text-align: right;
}

.fixed {
  position: fixed;
  width: 100%;
  top: 0;
}

h3 {
  color: #fff;
}

.first {
  background: #000;
  width: 49%;
  display: inline-block;
  margin: 0px;
  padding: 0px;
  border: 1px solid #fff;
}

.firstp {
  color: #fff;
}

.second {
  background: #000;
  width: 49%;
  display: inline-block;
  margin: 0px;
  padding: 0px;
  border: 1px solid #fff;
}

.secondp {
  color: #fff;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #300E06;
}

li {
  float: left;
}

lia {
  display: block;
  color: white;
  text-align: center;
  padding: 14px16px;
  text-decoration: none;
}

lia:hover {
  background-color: #000;
}

HTML

<ul class="nav-bar-white">
  <li class="active-1"><a href="#home">Company Logo</a></li>
</ul>

<ul class="header-class">
  <li><a class="active" href="#home">Home</a></li>
  <li><a href="#news">News</a></li>
  <li><a href="#contact">Contact</a></li>
  <li><a href="#about">About</a></li>
</ul>
<div>
  <div class="first">
    <h3>
What is Lorem Ipsum?
</h3>
    <p>Lorem Ipsum is simply dummy textof the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960swith the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum
      <p>
        Lorem Ipsum is simply dummy textof the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960swith the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
        publishing software like Aldus PageMaker including versions of Lorem Ipsum
      </p>
    </p>
  </div>
  <div class="second">
    <h3>
What is Lorem Ipsum?
</h3>
    <p>
      <p>
        Lorem Ipsum is simply dummy textof the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960swith the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
        publishing software like Aldus PageMaker including versions of Lorem Ipsum
      </p>
      <p>
        Lorem Ipsum is simply dummy textof the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960swith the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
        publishing software like Aldus PageMaker including versions of Lorem Ipsum
      </p>
    </p>
  </div>
</div>

<div class="first">
  <h3>
What is Lorem Ipsum?
</h3>
  <p>Lorem Ipsum is simply dummy textof the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960swith the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
    publishing software like Aldus PageMaker including versions of Lorem Ipsum
    <p>
      Lorem Ipsum is simply dummy textof the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
      survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960swith the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum
    </p>
  </p>
</div>
<div class="second">
  <h3>
What is Lorem Ipsum?
</h3>
  <p>
    <p>
      Lorem Ipsum is simply dummy textof the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
      survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960swith the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum
    </p>
    <p>
      Lorem Ipsum is simply dummy textof the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
      survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960swith the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum
    </p>
  </p>
</div>
</div>

Solution 5:

For jQuery users, check out :

var yourNavigation = $(".nav");
    stickyDiv = "sticky";
    yourHeader = $('.header').height();

$(window).scroll(function() {
  if( $(this).scrollTop() > yourHeader ) {
    yourNavigation.addClass(stickyDiv);
  } else {
    yourNavigation.removeClass(stickyDiv);
  }
});
/* google font */body {
  text-align:center;
  margin:0;
  padding:0;
  font-family: 'Montserrat', sans-serif;
  color:rgba(0,0,0,0.87);
}

.header {
  width:100%;
  height:100px;
  font-size:50px;
  line-height:100px;
  text-transform:uppercase;
  font-weight:bold;
}

.nav {
  width:100%;
  height:60px; 
  font-size:20px;
  line-height:60px; 
  background:#ff5252;
  color:#fff;
  position:relative;
  margin-bottom:60px;
  z-index:3;
  text-transform:uppercase;
}

.content {
  width:640px;
  height:500vh;
  font-size:14px;
  padding-top:100px;
  margin:0 auto 0;
}

.sticky {
  position:fixed;
  top:0;
}

p {
  line-height:2;
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script><scriptsrc="sticky.js"></script><linkrel="stylesheet"type="text/css"href="sticky.css"><body><headerclass='header'>Header</header><divclass='nav'>Navigation</div><divclass='content'><h1>Simple sticky navigation bar</h1><p>Using jQuery we add (or remove) a sticky class to the navigation bar. The sticky class fixes the position.
        </p><p>This happens whenever the page is scrolled. We check to see how far down the page has scrolled, if this
            number is greater than the height of the header, we add the sticky class to the navigation.</p><p>You can already achieve this effect using only CSS with <b>position: sticky;</b> but it's not supported by
            many browsers at the moment.</p></div></body>

Post a Comment for "Sticky Navbar Onscroll?"