I’m trying to add a rule that goes as: If the window width is less than 991 and resizes to something more or equal to 992, then do something.
I want to remove the class ‘opened’ from a megamenu only once if the screen is resized from mobile/tablet view (less than 991) to desktop view (more or equal to 992). But if the screen is already more than 992, do nothing.
All I can come up with are the resizes rules, but these keep applying every time I resize
if ($(window).width() >= 992) {
$('.megamenu').removeClass('opened');
}
2
Answers
I am not 100% sure of your logic, but you can remove whatever elements from this that does not meet your specs. You can add the load event if you want to run this when the page loads too
There is plenty of answers on internet about it, but here is some help :
When you write
your code is executed only once, when you load the page.
You should also listen to the
resize
event to execute your function every time the window is resized.Note that I also added the
load
event in order to run the function on the page loaded event.