I am trying to stick the ads on website while user scroll it, using the code below
//to Stick Ads while scroll on Desktop
var StickHead=$('#StickHead').offset().top;
$(window).scroll(function () {
if ($(window).scrollTop() > stickyHeaderTop) {
$('#StickHead').css({ position: 'fixed', top: '0px' });
} else {
$('#StickHead').css({ position: 'static', top: '0px' });
}
});
this code work's fine when we have div with id StickHead, but it throws an error when there is no StickHead id, as it would be difficult to copy paste it in selected page I have placed this code in script which is on all pages, now my question how can I make it work in all pages without error?
I have thought it would be easier if I can find if the Id exists, then run this code, otherwise leave it.
I have idea but not able to implement, any help?
thanks
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly