Giter VIP home page Giter VIP logo

stickyheaders's Introduction

stickyHeaders

jQuery Plugin for iOS-like Sticky Headers

Installation

  1. Include jQuery, i.e. place <script src="js/jquery.min.js"></script> in your footer.
  2. Include stickyHeaders JS file: <script src="js/jquery.stickyHeaders.js"></script>.
  3. Include stickyHeaders CSS file: <link rel="stylesheet" href="css/jquery.stickyHeaders.css" />.

Usage

If you have markup like this:

<!-- header -->
<h1>Lorem ipsum dolor sit amet.</h1>

<div class="sticky-header-wrap">

	<h2 class="sticky-header">An Epic Heading</h2>
	<!-- ... -->
	
	<h2 class="sticky-header">Another Epic Heading</h2>
	<!-- ... -->

	<!-- more headings and content -->

</div>

<!-- footer -->
<footer>Lorem ipsum dolor sit amet.</footer>

initialize the plugin as follows:

$('.sticky-header-wrap').stickyHeaders();

You'll have to add some CSS rules for the sticky header element to fit the style of the other headers. Refer to the tips at the bottom of this document for a few valuable tips.

Options

Here is a list of possible options with the corresponding default values.

var options = {
    headlineSelector:      '.sticky-header',
    hiddenClass:           'sticky-header-hidden',
    stickyElement:         'h2',
    stickyClass:           'sticky-helper',
    stickyChildren:        '<span></span>',
    textContainerSelector: 'span',
    endOfScrollPos:        null
}

headlineSelector

The selector for the actual headlines.

  • Default: .sticky-header

hiddenClass

The class to be used for hidden headlines.

  • Default: sticky-header-hidden

stickyElement

The HTML tag of the sticky element.

  • Default: h2

stickyClass

The CSS class that will be applied to the sticky element.

  • Default: sticky-helper

stickyChildren

The HTML markup that will be placed inside the stickyElement. Will be used for inserting text. If you need more nested elements for your styling, this is the right place to go.

  • Default: <span></span>
  • Example: <span class="outer"><span class="inner"></span></span>

textContainerSelector

The selector to find inside the stickyElement where the text will be placed. Change this in conjunction to stickyChildren.

  • Default: span
  • Example: .inner (see stickyChildrens example)

endOfScrollPos

The position to end the scrolling at. You can give a numeric value as well as a function. The latter is recommended since it's more dynamic.

  • Default: null
  • Example: function() { return $('footer').offset().top; }

Misc

If you run into issues where the Plugin will assume the wrong header positions, try rebuilding the header cache by triggering the corresponding event:

$(window).trigger('stickyHeadersRebuildCache');

You may need to do this as soon as the height of your content changes.

Tips for CSS

  • The simplest way to ensure same styling for headers and sticky element is to make sure the sticky element has the same class applied as the normal headers. But don't forget to add a unique class like sticky-helper.
    You can add those classes via stickyClass option.
  • Use nested elements in case the current markup isn't enough for your purposes. Refer to the stickyChildren and textContainerSelector options.
  • Make the wrapper have position:relative. By saying "the wrapper" I mean the element the Plugin is called on. So when using $('.sticky-header-wrap').stickyHeaders(); apply relative positioning to .sticky-header-wrap.
  • If you change the default class names make sure to include the default stylings. Refer to the jquery.stickyHeaders.css file.
  • CSS3 animations can cause some flickering. Consider disabling them for headers.
  • If you have problems, check the demos from the next chapter.

Demo

  1. Simple Lorem-Ipsum Blog
  2. Daring Fireball with stickyHeaders (you might have heard of it)
  3. Peter Kröners Blog with stickyHeaders (a german blog)

stickyheaders's People

Contributors

hoxu avatar leenno avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

stickyheaders's Issues

Does not work in IE11

"Unable to get property 'height' of undefined or null reference" error. Line 112 of the original raw file.

Sticky navigation bar at the top - offset required

Hi.

I stumbled upon an issue when you have a sticky navigation bar at the top, where the main content scrolls behind it. Then two things need to behave differently:

  • The point where the sticky headline is triggered needs to be offset by the height of the navigation bar.
  • The top position of the sticky headline needs to be the height of the navigation bar.

I solved both of them and will create a pull request (if you are interested).

Kind regards, Estigy

IOS fling issue

On IOS, if user scroll view with a fling gesture, the header won't stick until scroll stops. How to achieve the effect that the header would sticky right on the header scroll to top, no matter it's in a fling? (fling: quick scroll on ios)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.