Skip to content

Instantly share code, notes, and snippets.

@jessehintze
Created July 3, 2019 18:32
Show Gist options
  • Save jessehintze/f83b0d0e48e43648848973061f66a470 to your computer and use it in GitHub Desktop.
Save jessehintze/f83b0d0e48e43648848973061f66a470 to your computer and use it in GitHub Desktop.
Sticky Sidebar
// grouped products sticky
var groupedImage = function () {
var sticky = $('#views-exposed-form-news-recipes');
var stickyrStopper = $('.footer-inner');
if (sticky.length > 0) { // make sure ".sticky" element exists
var generalSidebarHeight = sticky.outerHeight();
var stickyTop = sticky.offset().top;
var stickOffset = 0;
var stickyStopperPosition = stickyrStopper.offset().top;
var stopPoint = stickyStopperPosition - generalSidebarHeight - stickOffset;
var diff = stopPoint + stickOffset;
$(window).scroll(function () { // scroll event
var windowTop = $(window).scrollTop(); // returns number
if (stopPoint < windowTop) {
sticky.css({position: 'absolute', top: diff, background: 'rgba(255,255,255,.8)'});
} else if (stickyTop < windowTop + stickOffset) {
sticky.css({position: 'fixed', top: stickOffset, background: 'rgba(255,255,255,.8)'});
} else {
sticky.css({position: 'absolute', top: 'initial', background: 'none'});
}
});
}
};
groupedImage();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment