Fork me on GitHub

Fixtop

A jQuery plugin to fix DOM element to the top when a page scrolls to its position. See the bar below for an example.

How to include

Simply include jquery.js and jquery.fixtop.js you have downloaded.

<script src="http://code.jquery.com/jquery-[latest].min.js">
<script src="jquery.fixtop.js">

How to use

Create a <div> element in a HTML file

<div id='fixtop'></div>

Then, call the jQuery fixtop() method in JS

$('#fixtop').fixtop();

Options

Attribute Type Default Description
marginTop integer 0 Margin top of the fixed-top element, applied once it is fixed to the top of a page
zIndex integer 1000 z-index of the fixed-top element, applied once it is fixed to the top of a page
fixed function() - Fuction that will be called after the element become fixed. It receive a jQeury object of the fixed-top element as a parameter
unfixed function() - Fuction that will be called after the element become normal. Use mostly for reverting fixed fuction()
fixedWidth string 100% A value of width css attribute, that will be set once the element become fixed. This is mainly used when your nav bar does not have width 100%. For example, you can set to 'none' if a bar is side bar with float: left;

For example, you can set options as:

$("#fixtop").fixtop({
  marginTop: 10,
  zIndex: 2000,
  fixed: function(el){
    el.addClass("barChanged");
  },
  unfixed: function(el){
    el.removeClass("barChanged");
  },
});