I have header
and sidebar
div blocks and I need them to stick at the top once the scroll event is triggered due to our specific requirements.
While making a single element sticky is not a problem, having more than one causes issues with scroll action and causes it to jump back to the top.
Is there a clean solution to this problem without relying on plugins?
Take a look at my JS Fiddle for reference.
Below is the script that works well for a single element:
$(window).on("scroll", function () {
var fromTop = $(window).scrollTop();
$(".sidebar").toggleClass("fixed", (fromTop > 50));
$(".header").toggleClass("fixed", (fromTop > 50));
});