I am looking to implement a standard UX feature that involves changing an element on scroll. Currently, I have this jQuery code in place:
$(document).scroll(function() {
var scroll = $(document).scrollTop();
if (scroll >= 50) {
$(".search-bar").removeClass('open');
$(".search-bttn").removeClass('close');
}
});
While this code works well and changes the classes after scrolling 50px from the top of the document, I am wondering if it is possible to achieve the same effect after scrolling 50px from anywhere on the page?
The reasoning behind this design choice is that I have a position:fixed search bar that appears when a click function opens it, and then I want it to disappear upon scrolling. However, I don't want it to vanish after just one or two accidental scrolls as it's easy to do so unintentionally. Ideally, I'd like it to only be removed after a significant scroll of around 50 pixels.
I hope this explanation makes sense! Any insights on how to achieve this would be much appreciated...
Cheers!