Skip to content

Instantly share code, notes, and snippets.

@Cafnio
Last active May 8, 2019 17:48
Show Gist options
  • Save Cafnio/f897ffe29a0515d37483a8e4c40802eb to your computer and use it in GitHub Desktop.
Save Cafnio/f897ffe29a0515d37483a8e4c40802eb to your computer and use it in GitHub Desktop.
Wordpress fixed widget bar with Jquery
jQuery( document ).ready(function() {
var fixmeTop = 100; // get initial position of the element
jQuery(window).scroll(function() { // assign scroll event listener
var distanceBottom = jQuery(document).height() - (jQuery(document).scrollTop() + jQuery(window).height());
var currentScroll = jQuery(window).scrollTop(); // get current position
if (currentScroll >= fixmeTop && distanceBottom > 540) { // apply position: fixed if you
jQuery('#secondary').css({ // scroll to that element or below it
position: 'fixed',
top: '110px'
});
}else if(currentScroll >= fixmeTop && distanceBottom <= 540){
jQuery('#secondary').css({ // scroll to that element or below it
position: 'absolute',
top: parseInt(jQuery('.entry-content').css('height')) - 600,
});
} else { // apply position: static
jQuery('#secondary').css({ // if you scroll above it
position: 'absolute',
top: 'unset'
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment