I have header
and sidebar
div blocks and for our specific needs, I need to make both elements stick at the top once scroll event fires.
Making single element sticky is no problem but if there is more than one, it prevents scroll action and keeps jumping back to the top.
Is there any nice solution to this without using plugins?
Here is my JS Fiddle
And, the following is the script which works well with single element.
$(window).on("scroll", function () {
var fromTop = $(window).scrollTop();
$(".sidebar").toggleClass("fixed", (fromTop > 50));
$(".header").toggleClass("fixed", (fromTop > 50));
});