Skip to content

Instantly share code, notes, and snippets.

@n7studios
Last active August 29, 2015 14:02
Show Gist options
  • Save n7studios/c5785a89a5f64460d03a to your computer and use it in GitHub Desktop.
Save n7studios/c5785a89a5f64460d03a to your computer and use it in GitHub Desktop.
WordPress: Fade in last sidebar widget
jQuery(document).ready(function($) {
/**
* Configuration
* The container for your sidebar e.g. aside, #sidebar etc.
*/
var sidebarElement = $('div#secondary');
// End config
// Check if the sidebar exists
if ($(sidebarElement).length > 0) {
// Get the last widget in the sidebar, and its position on screen
var widgetDisplayed = false;
var lastWidget = $('.widget:last-child', $(sidebarElement));
var lastWidgetOffset = $(lastWidget).offset().top;
// Hide the last widget
$(lastWidget).hide();
// When the user scrolls, check if we have reached the top of the last widget
// If we have, display the widget
$(document).scroll(function() {
// If the widget has been displayed, we don't need to keep doing
// a check.
if (!widgetDisplayed) {
if($(this).scrollTop() > lastWidgetOffset) {
$(lastWidget).fadeIn('slow');
widgetDisplayed = true;
}
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment