mercredi 22 juillet 2020

jQuery if/else nested statement

Consider the following code, which adds a sticky header on scroll to a page where a certain ID "sticky-header" is present and window size is greater than 425

    sticky : function() {
        var stickyTools = $j('#sticky-header');
        if (stickyTools.length > 0 && window.outerWidth > 425) {
            var headerHeight = stickyTools.height();
            var fixmeTop = stickyTools.offset().top + headerHeight - 50;
            $j(window).scroll(function() {
                var currentScroll = $j(window).scrollTop();
                if (currentScroll >= fixmeTop) {
                    stickyTools.addClass('sticky');
                } else {
                    stickyTools.removeClass('sticky');
                }
            });
        }
    }
};

Now this works fine on one template but on another template I want to re-use the same functionality but ONLY if a check box is selected. The checkboxes have the class of "item-checkbox". I have a unique ID on that page which is "template-list".

I am unsure of the best way to nest another IF statement within the current one (if (currentScroll >= fixmeTop)) or if there is a more clever way of doing this?

Aucun commentaire:

Enregistrer un commentaire