Last active
March 12, 2017 20:22
-
-
Save PauliusKrutkis/00ce21ae503656bfb6992a833b4bb0a2 to your computer and use it in GitHub Desktop.
jQuery sticky element script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sticky = (function($) { | |
var content = '.page-content'; | |
var element = '.sticky-block'; | |
if (!$(element).length || !(content).length) | |
return; | |
var barHeight = $(element).height(); | |
var barOffset = $(element).offset().top; | |
$(document).on('scroll', checkPosition); | |
function checkPosition() { | |
if($(window).scrollTop() > barOffset) | |
sticky(); | |
else | |
normal(); | |
} | |
function sticky() { | |
$(element).addClass('sticky'); | |
$(content).css('padding-top', barHeight); | |
} | |
function normal() { | |
$(element).removeClass('sticky'); | |
$(content).css('padding-top', 0); | |
} | |
})(jQuery) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment