2.2.7 • Published 3 years ago

hc-sticky v2.2.7

Weekly downloads
1,353
License
MIT
Repository
github
Last release
3 years ago

HC-Sticky

Version Downloads

JavaScript library that makes any element on your page visible while you scroll. Dependency free, but lso works as a jQuery plugin. Check out the demos.

Quick start

Install

This package can be installed with:

  • npm: npm install --save hc-sticky

Or download the latest release.

Including HC-Sticky

Script tag

<script src="/path/to/hc-sticky.js"></script>

Webpack/Browserify

In the script, including HC-Sticky will usually look like this:

const hcSticky = require('hc-sticky');

Babel

import hcSticky from 'hc-sticky';

AMD (Asynchronous Module Definition)

If using AMD, the module will be automatically defined as hcSticky.

Usage

Be sure to call HC-Sticky once your element is available in the DOM.

Vanilla JS

document.addEventListener('DOMContentLoaded', function() {

  var Sticky = new hcSticky('#element', {
    stickTo: '#content'
  });

});

jQuery

jQuery(document).ready(function($) {

  $('#element').hcSticky({
    stickTo: $('#content')
  });

});

For HC-Sticky to work as a jQuery plugin, jQuery has to be a property of global window object, so be careful when using it in compbination with Babel/Webpack/Browserify and jQuery.

Options

HC Sticky has a wide range of options you can set to have a full controll over the sticky elements.

PropertyDefaultTypeDescription
top0intThe distance from the top of the Window at which to trigger HC-Sticky.
bottom0intThe distance from the bottom of the Window at which to attach HC-Sticky.
innerTop0intThe distance from the top inside of the sticky element at which to trigger HC-Sticky.
innerStickernullstring / element objectElement inside of the sticky element at which to attach HC-Sticky. This has higher priority than innerTop option.
bottomEnd0intThe distance from the bottom of the referring element at which to stop HC-Sticky.
stickTonull (parent element)string / element objectElement that represents the reference for height instead of height of the container.
followScrolltruebooleanWhen set to false, sticky content will not move with the page if it is bigger than Window.
stickyClass'sticky'stringHTML class that will be applied to sticky element while it is attached.
responsivenullobjectObject containing responsive breakpoints, on which you can tell HC Sticky what to do.
mobileFirstfalsebooleanDirection of the responsive queries.
disablefalsebooleanDisable the plugin. Usualy used with responsive object.
onStartnullfunctionCallback function fired when the element becomes attached.
onStopnullfunctionCallback function fired when the element stops floating.
onBeforeResizenullfunctionCallback function fired before sticky has been resized (happens after Window resize and before sticky reinit).
onResizenullfunctionCallback function fired after sticky has been resized (happens after Window resize and sticky reinit).
resizeDebounce100intLimit the rate at which the HC Sticky can fire on window resize.

More on how to use the responsive object here.

Methods

Methods are used to control the plugin after initialization.

MethodAcceptsDescription
optionsstringReturns current settings, or a specific setting if you specify it.
updateobjectUpdates the settings with the new ones.
refreshN/ARecalculates sticky size and position. Useful after altering DOM elements inside sticky.
detachN/ADetaches the HC-Sticky from element, preventing it from running.
attachN/AAttaches the HC-Sticky back to the element.
destroyN/ACompletely destroys HC-Sticky and reverts element to original state.

Vanilla JS

var Sticky = new hcSticky('#element', {
  stickTo: '#content'
});

Sticky.update({
  top: 20
});

jQuery

var $sticky = $('#element');

$sticky.hcSticky({
  stickTo: '#content'
});

$sticky.hcSticky('update', {
  top: 20
});

Dev Building

This package comes with Gulp. The following tasks are available:

  • default compiles the JS into /dist and builds the Demos into /docs.
  • watch watches source JS and Demo files and builds them automatically whenever you save.

You can pass a --dev command if you don't want the compiled JS to be minified.

License

The code and the documentation are released under the MIT License.

2.2.7

3 years ago

2.2.6

4 years ago

2.2.5

4 years ago

2.2.4

4 years ago

2.2.3

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.7

6 years ago

2.1.6

6 years ago

2.1.5

6 years ago

2.1.4

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.1

6 years ago

1.2.43

8 years ago