jQuery Plugin for Sticky Objects

Overview

Sticky

Sticky is a jQuery plugin that gives you the ability to make any element on your page always stay visible.

Sticky in brief

This is how it works:

  • When the target element is about to be hidden, the plugin will add the class className to it (and to a wrapper added as its parent), set it to position: fixed and calculate its new top, based on the element's height, the page height and the topSpacing and bottomSpacing options.
  • That's it. In some cases you might need to set a fixed width to your element when it is "sticked". But by default (widthFromWrapper == true) sticky updates elements's width to the wrapper's width. Check the example-*.html files for some examples.

Usage

  • Include jQuery & Sticky.
  • Call Sticky.
<script src="jquery.js"></script>
<script src="jquery.sticky.js"></script>
<script>
  $(document).ready(function(){
    $("#sticker").sticky({topSpacing:0});
  });
</script>
  • Edit your css to position the elements (check the examples in example-*.html).

  • To unstick an object

<script>
  $("#sticker").unstick();
</script>

Options

  • topSpacing: (default: 0) Pixels between the page top and the element's top.
  • bottomSpacing: (default: 0) Pixels between the page bottom and the element's bottom.
  • className: (default: 'is-sticky') CSS class added to the element's wrapper when "sticked".
  • wrapperClassName: (default: 'sticky-wrapper') CSS class added to the wrapper.
  • center: (default: false) Boolean determining whether the sticky element should be horizontally centered in the page.
  • getWidthFrom: (default: '') Selector of element referenced to set fixed width of "sticky" element.
  • widthFromWrapper: (default: true) Boolean determining whether width of the "sticky" element should be updated to match the wrapper's width. Wrapper is a placeholder for "sticky" element while it is fixed (out of static elements flow), and its width depends on the context and CSS rules. Works only as long getWidthForm isn't set.
  • responsiveWidth: (default: false) Boolean determining whether widths will be recalculated on window resize (using getWidthfrom).
  • zIndex: (default: inherit) controls z-index of the sticked element.

Methods

  • sticky(options): Initializer. options is optional.
  • sticky('update'): Recalculates the element's position.

Events

  • sticky-start: When the element becomes sticky.
  • sticky-end: When the element returns to its original location
  • sticky-update: When the element is sticked but position must be updated for constraints reasons
  • sticky-bottom-reached: When the element reached the bottom space limit
  • sticky-bottom-unreached: When the element unreached the bottom space limit

To subscribe to events use jquery:

<script>
  $('#sticker').on('sticky-start', function() { console.log("Started"); });
  $('#sticker').on('sticky-end', function() { console.log("Ended"); });
  $('#sticker').on('sticky-update', function() { console.log("Update"); });
  $('#sticker').on('sticky-bottom-reached', function() { console.log("Bottom reached"); });
  $('#sticker').on('sticky-bottom-unreached', function() { console.log("Bottom unreached"); });
</script>
Comments
  • Make sticky element stick to bottom of wrapper when reached

    Make sticky element stick to bottom of wrapper when reached

    Checks if sticky element has reached bottom of its wrapper, and makes it stick to the bottom instead of overflowing.

    The caveat is that the script checks on every scroll event if this condition happens and then always applies css styles to the element.

    It's not the most elegant solution but it works, and at least is a good start.

    Solves #164, #127 and maybe #106.

    opened by naoisegolden 11
  • Requires to update the size when the section size is relative

    Requires to update the size when the section size is relative

    If you use this on a fluid layout, the size then needs to be updated based on the window size. If the content is dynamic and newly loaded content changed the document height, then again the size might be invalid.

    opened by mjfathinia 7
  • Stop defaults from being overwritten

    Stop defaults from being overwritten

    Commit message:

    Stop $.extend overwriting defaults object.

    When using $.extend, the first argument object is altered, and since objects are copied by reference in Javascript that means the defaults object gets changed when it shouldn't and subsequent calls to $().sticky() will use previous options rather than fresh defaults.

    More info:

    var defaults = {
      // defaults set in jquery.sticky.js
      topSpacing: 0,
      bottomSpacing: 0,
      className: 'is-sticky',
      wrapperClassName: 'sticky-wrapper',
      center: false,
      getWidthFrom: ''
    };
    console.log(defaults.center);
    // false
    var options = {
      // options passed in to $(element).sticky()
      center: true
    };
    $.extend(defaults, options)
    console.log(defaults.center);
    // true
    // Subsequent sticky calls will be centered.
    
    opened by henrahmagix 5
  • Add UMD wrapper

    Add UMD wrapper

    UMD wrapper allows it to require the jquery dep from a module system if it exists. Taken from here https://github.com/umdjs/umd/blob/master/jqueryPluginCommonjs.js

    opened by SimenB 4
  • Adding bottom reached events + fix start event flood on bottom limit reached

    Adding bottom reached events + fix start event flood on bottom limit reached

    Hello,

    • start event is now triggered only once when the real start begins. It was flooding when bottom limit is reached and the top css property have to be updated to push up the sticked element.
    • To keep the event triggering when top is updated after start, the update event have been added. So it floods like start did before.
    • bottom-reached is triggered when sticked element reached the bottom space limit
    • bottom-unreached is triggered when sticked element is not reaching the bottom space limit

    Hope you will find this usefull...

    opened by thochra 4
  • Sticky does not work at all

    Sticky does not work at all

    Hi @garand ,

    I'm trying to use this plugin but once the plugin is instantiated the stickiness does not work. All the buttons just play around.

    Please help

    opened by sahanDissanayake 4
  • Tag releases in Git/Github

    Tag releases in Git/Github

    It currently isn't possible to specify a version of sticky to use because there are no tags on the git repository. Can you tag the current version (1.0.1) so that users can specify a version to lock to? (Some Bower tools can't use libraries that don't have tagged versions.)

    opened by geoffharcourt 4
  • "className" is added to parent of sticky element

    Hi! This plugin has saved me countless hours! :cookie: :smile:

    But I want to give some sticky elements on my page some common styles, so I set the plugin's className option to something common to these two elements, e.g. sticky-top-bar and style it.

    The sticky wrapper plugin however, does apply this class both to the wrapper (.sticky-wrapper) and to the sticky element. So when the element is sticked, then the styles are applied to both elements, which looks weird.

    My suggestion would be to omit the className from the wrapper, but I don't now which technical decision was behind this.

    opened by CHH 4
  • Make sticky cognizent of top and bottom margins.

    Make sticky cognizent of top and bottom margins.

    So we're using sticky for a bunch of navigational items, that may or may not have margins on the top or bottom (typically the bottom).

    The library doesn't take this margin into account when determining the height, and so text gets bumped around when it gets activated. Not cool... We can remove those margins, but then that means we have to add those margins to other things, and it just feels like a bit of a hack.

    Is there a better way I'm missing, or should I create a PR?

    Example: https://jsfiddle.net/heathdutton/85k7g8vt/5/

    opened by heathdutton 3
  • .widthFromWrapper option added and other improvements

    .widthFromWrapper option added and other improvements

    I made some improvements to stiky so that it could work in some of my projects. I didn't change any of the old functionality.

    The new option .widthFromWrapper enables me to handle width of the wrapper with pure CSS and sticky just adjusts it's width accordingly, without any extra coding.

    opened by duzun 3
  • Missing git tags for releases

    Missing git tags for releases

    The bower package is currently unable to install as your git repo is missing tags for released versions. In the interim I've worked around it by forking your project, but it would obviously be better for your users if your version worked :)

    opened by lukerandall 3
  • Can't seem to find a way to Put my element sticky at the bottom

    Can't seem to find a way to Put my element sticky at the bottom

    I tried to use bottomSpacing: 0 , but the element stays on top.

    Here is my code, I used the example html:

    <!doctype html>
    <html>
    <head>
      <title>Sticky Plugin</title>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
      <script type="text/javascript" src="jquery.sticky.js"></script>
      <script>
        
        $(window).load(function(){
          $("#header").sticky({ topSpacing:0,
                               bottomSpacing:0
                              });
        });
        
        $(window).scroll(function(){
          position = $(".dickydick").offset().top -$("#header").height();
          if ($(window).scrollTop() > position) $("#header").unstick();
          else $("#header").sticky();
        });
      </script>
      <style>
        body {
          height: 10000px;
          padding: 0;
          margin: 0;
        }
    
        #header {
          background: #bada55;
          color: white;
          font-family: Droid Sans;
          font-size: 18px;
          line-height: 1.6em;
          font-weight: bold;
          text-align: center;
          padding: 10px;
          text-shadow: 0 1px 1px rgba(0,0,0,.2);
          width:100%;
          box-sizing:border-box;
        }
      </style>
    </head>
    <body>
      <div class="header-sticky-wrapper" style="">
      <p>This is test this is text this is text at the top.</p>
      <div id="header">
        <p>This is the sticky thingy that is really cool.</p>
      </div>
      <p style="margin-top:200vh">This is test this is text this is text at the bottom.</p>
        </div>
      <div class="blockdiv">
        
      </div>
    </body>
    </html>
    
    opened by Crunchyman-ralph 0
  • Doesn't work with latest jquery 3.4.1

    Doesn't work with latest jquery 3.4.1

    I used this plugin with jquery 3.3.1. but once I update to jquery 3.4.1 Its not working anymore. I had to downgrade jquery version back to 3.3.1.

    change logs after 3.3.1:

    • https://blog.jquery.com/2019/04/10/jquery-3-4-0-released/
    • https://blog.jquery.com/2019/05/01/jquery-3-4-1-triggering-focus-events-in-ie-and-finding-root-elements-in-ios-10/
    opened by oshanz 1
  • Make sticky element scrollable with page after unstick?

    Make sticky element scrollable with page after unstick?

    This plugin works well, but it it possible to return a sticky element to its original scrollable state after unstick is called? When I do this, the div just seems to disappear, and I need it to just naturally scroll up with the rest of the content

    opened by swbates 0
  • add support for scrolling the sticky element if its higher from window

    add support for scrolling the sticky element if its higher from window

    In our use case, we have a webshop sidebar filters which can expand and be quite long height-wise. I extended your library so it has a config key "scrollStickyElement", which syncs scrolling of sticky element with main page. Should you find this snippet useful, you can merge, otherwise close the request.

    opened by DeepSkyDad 0
Releases(1.0.3)
Owner
Anthony Garand
Anthony Garand
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Customizable sticky notes for the web. jQuery plugin

Post It All! Customizable sticky notes for the web. jQuery Plugin The jQuery plugin for adding sticky notes in your webpage. http://postitall.txusko.c

Javi Filella 51 Apr 17, 2022
A simple jQuery extension to make any HTML element sticky on scroll.

jquery.sticky.js A simple jQuery extension to make any HTML element sticky on scroll. Installation Just download the script and include it in your HTM

Achal Jain 2 Aug 22, 2022
📝 You Can Create Your Own Short Notes With The Help of Sticky-Notes Website.

Hi ?? , I'm Sneh Agrawal A passionate Web developer from India ?? I’m currently working on Chatting Website Chit-Chat ?? How to reach me on My Gmail A

Sneh (Smilyboyy) 1 Feb 23, 2022
Create sticky element in flexbox sidebars. it can use in Vanilla JS and frameworks like Vue and React

js sticky side simple sticky side with js that can use in frameworks like vue and react. notes it can be used just in flexbox grids. target element sh

milad nazari 10 Mar 3, 2022
Show floating sticky outline (table of contents) for Notion pages, powered by nbundle.

Notion Outline Show floating sticky outline (table of contents) for Notion pages, powered by nbundle. This is an nbundle-powered Notion app bootstrapp

nbundle 11 Nov 10, 2022
Json-parser - A parser for json-objects without dependencies

Json Parser This is a experimental tool that I create for educational purposes, it's based in the jq works With this tool you can parse json-like stri

Gabriel Guerra 1 Jan 3, 2022
Hierarchical Converter for Array of Objects

Conversor Hierárquico para Array de Objetos - Hierarchical Converter to Array of Objects Docker-compose Cria a interface network e containers indicado

Victor Vinícius Eustáquio de Almeida 2 Jan 27, 2022
Visualize the Directed Acyclic Graph that Git creates to connect Commit, Tree and Blob objects internally.

Git Graph Visualize the Directed Acyclic Graph that Git creates to connect Commit, Tree and Blob objects. Hosted at HarshKapadia2.github.io/git-graph.

Harsh Kapadia 15 Aug 21, 2022
Binary-encoded serialization of JavaScript objects with generator-based parser and serializer

YaBSON Schemaless binary-encoded serialization of JavaScript objects with generator-based parser and serializer This library is designed to transfer l

Gildas 11 Aug 9, 2022
An API for producing and validating ActivityPub objects.

ActivityHelper A library that exports an API for producing and validating ActivityPub objects. In a federated system bound together by protocols, it's

Diana Thayer 6 May 2, 2022
An free e-book entirely about JavaScript objects

Advanced JavaScript Objects An e-book entirely about JavaScript objects Chapters Total length: 70 A4 pages. Chapter 0 - Introduction Chapter 1 - Getti

Carl Riis 240 Dec 13, 2022
🧩 TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)

?? TypeScript Primitives type TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types

CodelyTV 82 Dec 7, 2022
Awesome books: plain JavaScript with objects

Awesome books: plain JavaScript with objects Awesome books is an online book store. It's built with HTML, CSS and mostly JS based on a medium-fidelity

null 17 Aug 30, 2022
A JavaScript library improving inspection of objects

Pro Inspector A JavaScript utility improving inspection of objects on Node.js. Introduction Let's suppose that we have this declaration. class Person

Reiryoku Technologies 60 Dec 30, 2022
LiveJSON provides LiveView-like updating for JSON objects rather than DOM elements.

live_json LiveJSON provides LiveView-like updating for JSON objects rather than DOM elements. It works within your existing LiveViews - just use push_

Rich Jones 57 Dec 29, 2022
Solid Forms provides several form control objects useful for making working with forms easier.

Solid Forms Solid Forms provides several form control objects useful for making working with forms easier. Demos and examples below. # solidjs yarn ad

John 28 Jan 2, 2023
A small library for turning RSS XML feeds into JavaScript objects

rss-parser A small library for turning RSS XML feeds into JavaScript objects. Installation npm install --save rss-parser Usage You can parse RSS from

Robert Brennan 1.1k Dec 31, 2022