I have a scroll event and add class and remove classes for different elements. As you can see the code is a bit big. Is this method ok or is there a more efficient one?

I mean, you can see that there are no child elements. I could use .siblings() for some but, as you can see, I also serve unique classes.

Any idea? I know it looks bad but I think it is the only way.

 $(document).ready(function () {
        $(window).scroll(function () {
            var scroll = $(window).scrollTop();

            if (scroll >= 50) {
                $("nav ul li").addClass("list-mini");
                $("nav ul li a").addClass("nav-recolor");
                $("nav img").removeClass("zoomOutLeft");
                $("nav img").addClass("zoomInUp");
                $("nav img").addClass("logo-mini-active");
                $("nav ul").addClass("margin-transition");
                $("nav ul li a span").addClass("text-removed");
                $("nav ul li a").addClass("nav-font");
                $("nav ul li a span").addClass("transition-02s");
            } else {
                $("nav ul").removeClass("margin-transition");
                $("nav ul li").removeClass("list-mini");
                $("nav ul li a").removeClass("nav-recolor");
                $("nav img").removeClass("zoomInUp");
                $("nav img").addClass(" zoomOutLeft");
                $("nav ul li a span").removeClass("text-removed");
                $("nav ul li a").removeClass("nav-font");
                $("nav ul li a span").removeClass("transition-02s")
  • \$\begingroup\$ Hi. Welcome to Code Review! I think that we would need more context to give meaningful comments on this code. It's possible that you might cut this down with the right data structure, but the bigger question is if you could do something other than changing fifteen classes with every change. That seems to be an awful lot of changes for a single event, and there is no way for us to know what any particular change does. \$\endgroup\$
    – Brythan
    Nov 9, 2014 at 6:20
  • \$\begingroup\$ can you create a fiddle ? need to see your html structure one solution would be you can add a class to parent and style its children with that class \$\endgroup\$ Nov 9, 2014 at 9:04

2 Answers 2


You shouldn't be doing so much manipulation of the classes. Instead, you could just add or remove a scrolled-down class to the <body> element. Then specify the rest of the changes using pure CSS. (For example, you would have a body.scrolled-down nav { … } rule.)


One thing you could do is create two functions that adds/removes a class using two parameters:

function foo(element, className) {
    $('' + element + '').addClass('' + className + '');

function foo(element, className) {
    $('' + element + '').removeClass('' + className + '');

And then just call the function so you don't have to repeat yourself every time.

OR another way would be to put all the elements you want to manipulate and the classes in two arrays, and then just call the functions using a loop and the index in the array.

var myElements = ['nav img', '.fa-shopping-cart', ...];
var myClasses = ['zoomIn', ...];

for (i = 0; i < myElements.length; i++) {

    foo(myElements[i], myClasses[i]);

This might need a bit of editing but you get the idea.

However I'm not sure if any of these are actually better ways of doing what you want, but it's other ways of doing it. It might make everything more manageable if the code gets even bigger. Hope this helped.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.