I use this function to make a help box stay visible when a user scrolls down:
var top = $('.help-box').offset().top - parseFloat($('.help-box').css('marginTop').replace(/auto/, 0));
$(window).scroll(function (event) {
var y = $(this).scrollTop();
if (y >= top) {
$('.help-box').addClass('fixed');
} else {
$('.help-box').removeClass('fixed');
}
});
I want to reuse it across several pages, so I included it in my layout (on every page load). The problem now is that I get an error on pages that do not have the help box: $(".help-box").offset() is null
Is there a way to write this function so it can be reused without causing an error? I want to avoid selectively including where it's need it as it's easier to just leave the include in my layout.