JSFiddle - React, Tailwind, and code Playground

by nicholasstephan

HTML

<div class="container peek-a-boo">
    <h3 class="autohide-header">Auto Hiding Header</h3>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since...

CSS

h3, p {
	margin:0;
	padding:0 0 10px 0;
}

h3 {
	display:block;
	height:30px;
	line-height:30px;
}

.container {
	display:block;
	overflow:auto;
	background-color:#eee;
}

.peek-a-boo {
    position: absolute;
    top:50px;
    left:50px;
    width:200px;
    height:400px;
}

.is-sticking {
	background-color:green;
}

JavaScript

(function ($) {
	"use strict";

	/* Sticky Definition */

	var Sticky = function(element, options) {
		this.options = options;
		this.$element = $(element);
		this.$parent = this.$element.parent();
		this.$placeholder = $("<div class='sticky-placeholder'/>");
		
		this.init();
	};

	Sticky.prototype = {
		init: function() {
			// Create a placeholder to hold the shape of the element. 
			// Note: Using `outerHeight()` only works as long as there aren't
			// any margins on the element and the elements immediate siblings
			// or we get have with collapsing margins.
			this.$placeholder
				.css('height', this.$element.outerHeight())
				.insertAfter(this.$element).hide();

			this.staticTop = this.$element.position().top;
			this.stickyTop = this.staticTop;
			this.isSticking = false;

			this.$parent.on('scroll.sticky', this.draw.bind(this));
		},

		destroy: function() {
			this.$parent.off('scroll.sticky');
			this.positionStatic();
			this.$element.removeData('sticky');
		},

		positionStatic: function() {
			if(!this.isSticking)
				return;

			this.isSticking = false;

			this.$placeholder.hide();

			this.$element
				.css('position', 'static')
				.removeClass('is-sticking');
		},

		positionSticky: function() {
			if(this.isSticking)
				return;

			this.isSticking = true;

			this.$placeholder.show();

			this.$element
				.css('position', 'fixed')
				.addClass('is-sticking');
		},

		stickTo: function(left, top) {
			var parentTop = this.$parent.offset().top;

			this.$element
				.css('top', top + parentTop + "px");

			this.positionSticky();
		},

		draw: function() {
			var scrollTop = this.$parent.scrollTop();

			if(this.options.autohide && scrollTop >= this.staticTop) {
				// The stickyTop is the fixed position of $element. This should
				// be between the scrollTop of the $parent and the scrollTop of 
				// the parent less the height of the element. 
				this.stickyTop = Math.min(Math.max(this.stickyTop,...