Skip to content

Instantly share code, notes, and snippets.

@airton
Last active May 16, 2018 22:45
Show Gist options
  • Save airton/96fa8556c0b6664e78db477b52709786 to your computer and use it in GitHub Desktop.
Save airton/96fa8556c0b6664e78db477b52709786 to your computer and use it in GitHub Desktop.
import $ from 'jquery';
export const onScrollFixSidebar = function() {
const relatedPosts = $('.related-post')
const body = $('body')
if(relatedPosts.length === 0 || body.width() <= 768) return
const sidebar = $('.sidebar')
const relatedPostsPositionTop = relatedPosts.position().top
const clearMenu = $('.clear-menu')
const footer = $('.footer')
const footerPositionTop = footer.position().top
$(window).scroll(function() {
let scroll = $(window).scrollTop();
let scrollDiff = scroll + clearMenu.height()
let scrollRelated = scrollDiff + relatedPosts.outerHeight()
let footerPosition = footer.position().top
if ( scrollDiff <= relatedPostsPositionTop ) {
sidebar.removeClass('is-fixed');
} else {
if ( scrollRelated >= footerPosition ) {
sidebar.removeClass('is-fixed').addClass('is-fixed-bottom');
} else {
sidebar.removeClass('is-fixed-bottom').addClass('is-fixed');
}
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment