If you have a function which fires on a scroll event, which is better.
- Check if a class is already added and if not add it
- Don't do any checks but just add the class everytime needed.
$(document).on('scroll', function () {
var scrollTop = $(this).scrollTop();
if (scrollTop > 50) {
if (!$("nav .branding").hasClass("collapse"))
$("nav .branding").addClass("collapse");
} else {
if ($("nav .branding").hasClass("collapse"))
$("nav .branding").removeClass("collapse");
}
});
or
$(document).on('scroll', function () {
var scrollTop = $(this).scrollTop();
if (scrollTop > 50) {
$("nav .branding").addClass("collapse");
} else {
$("nav .branding").removeClass("collapse");
}
});
The first occasion there is an extra check but in the other action there might (?) be a more intense operation(?)