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?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jan 14, 2015 at 8:19
\$\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.

answered Jan 22, 2015 at 22:12
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.