1
\$\begingroup\$

I created the most simple though still quite flexible jQuery slider ever! Or at least, I hope so.

var slider = $(".slider-ul");

slider.each(function () {

    var e = $(this),
        images = e.find("li"),
        current = null;

    slide();

    function slide() {

        images.each(function() {

            var li = $(this),
                next,
                pDone = false,
                sDone = false;

            if (li.hasClass("primary")) {
                li.removeClass("primary");
                pDone = true;
            } else if (li.hasClass("secondary")) {
                li.removeClass("secondary").addClass("primary");

                next = li.next();

                console.log(next);
                if (next.size()) {
                    next.addClass("secondary");
                    sDone = true;
                } else {
                    images.filter(":first").addClass("secondary");
                    sDone = true;
                }
            }

            if (sDone && pDone) {
                return false;
            }
        });

        setTimeout(slide, 5000);

    }
});

How does this look?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

In this code, you set sDone = true in both branches of the if-else:

if (next.size()) {
    next.addClass("secondary");
    sDone = true;
} else {
    images.filter(":first").addClass("secondary");
    sDone = true;
}

So you could move that line outside of the if-else:

if (next.size()) {
    next.addClass("secondary");
} else {
    images.filter(":first").addClass("secondary");
}
sDone = true;

At the top you declared the current variable, but then you don't use it. If you don't need it, then remove it.

\$\endgroup\$
0

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.