Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript for position fixed on scroll

I have this div which is just a title.

<div class="container-fluid DFOCpadding">
<div class="row">
    <div class="col-lg-12">
        <div class=" text-center ">
        <span class="toptext">DANCE FOR OVARIAN CANCER</span>
        </div>
    </div>
</div>

and a nav bar under it which I'd like to position as fixed

<nav class="navbar navbar-default">
<div class="container"> 
    <div class="navbar-header page-scroll">
        <button type="button" class="navbar-toggle glyphicon glyphicon-chevron-down" data-toggle="collapse" data-target="#navbar">
            <span class="sr-only">Dance Dance</span>
        </button>
    </div>
    <div class="collapse navbar-collapse" id="navbar">
        <ul class="nav navbar-nav">
            <li><a href="http://danceforovariancancer.com.au">HOME</a></li>             
            <li><a class="scroll" href="#register"><span class="register">REGISTER</span></a></li>
            <li><a class="scroll" href="#faq">FAQ's</a></li>    
            <li><a class="scroll" href="#acts">ACTS</a></li>    
            <li><a class="scroll" href="#about">ABOUT</a></li>  
            <li><a class="scroll" href="#contact">CONTACT</a></li>      
        </ul>
    </div>
</div>
</nav>  

and I need the page to just catch the navbar when you scroll past it like on this site: https://ovariancancer.net.au/

I have seen answers for this but they are very restricted to the OP's use case and so I can't get it to work for my page.

cheers

like image 556
lopu Avatar asked Nov 30 '15 01:11

lopu


People also ask

How do I fix scrolling position?

To make an element stick to the screen when you are scrolling, select it and just click the Fix Position When Scrolling checkbox. Select the artboard, set a Vertical Scrolling and set a value for the Viewpoint Height that's lower than the height of the entire artboard.

How do you keep the header static on top when scrolling?

Set padding-top on the <body> element to be the same as the height of your header. This will move your content <div> down, and leave enough space for the header. Set the header <div> to position: absolute; top: 0; to remove it from the normal layout flow, and position it at the top of the page.

How do you stick a div after scrolling?

To make an element sticky, use the following code: make sticky('#sticky-elem-id'); When an element becomes sticky, the code maintains the remaining content's position to prevent it from jumping into the gap left by the sticky element.

How do I fix a scrolling header?

By setting postion: sticky and top: 0, we can create a fixed header on a scroll in HTML tables.


2 Answers

Try this:

.navbarfixed {
    position: fixed;
 }



$(function() {
var nav = $(".navbar");
$(window).scroll(function() {    
    var scroll = $(window).scrollTop();

    if (scroll >= 500) {
        nav.addClass("navbarfixed");
    } else {
        nav.removeClass("navbarfixed");
    }
});
});
like image 39
The Beast Avatar answered Sep 23 '22 21:09

The Beast


Her's the code similar to 'https://ovariancancer.net.au/'

hope this helps

$(window).scroll(function() {
  if ($(window).scrollTop() > 100) {
    $(".navbar").css({
      'position': 'fixed',
      'top': '0px',
      'width': '100%',
      'background': '#cfcfcf',
      'z-index': '99'
    });
  } else {
    $(".navbar").css({
      'position': 'relative',
      'background': '#FFFFFF',
      'width': '100%'
    })
  }
});
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid DFOCpadding">
  <div class="row">
    <div class="col-lg-12">
      <div class=" text-center ">
        <span class="toptext">DANCE FOR OVARIAN CANCER</span>
      </div>
    </div>
  </div>
  <nav class="navbar navbar-default">
    <div class="container">
      <div class="navbar-header page-scroll">
        <button type="button" class="navbar-toggle glyphicon glyphicon-chevron-down" data-toggle="collapse" data-target="#navbar">
          <span class="sr-only">Dance Dance</span>
        </button>
      </div>
      <div class="collapse navbar-collapse" id="navbar">
        <ul class="nav navbar-nav">
          <li><a href="http://danceforovariancancer.com.au">HOME</a>
          </li>
          <li><a class="scroll" href="#register"><span class="register">REGISTER</span></a>
          </li>
          <li><a class="scroll" href="#faq">FAQ's</a>
          </li>

          <li><a class="scroll" href="#acts">ACTS</a>
          </li>
          <li><a class="scroll" href="#about">ABOUT</a>
          </li>
          <li><a class="scroll" href="#contact">CONTACT</a>
          </li>
        </ul>
      </div>
    </div>
  </nav>
  <P>&nbsp;</p>
  <P>&nbsp;</p>      <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>      <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>      <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>      <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>      <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>      <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>      <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>      <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>      <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>      <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
  <P>&nbsp;</p>
like image 153
Lucky Chingi Avatar answered Sep 23 '22 21:09

Lucky Chingi