Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am using class "active" to keep track on wich child to fadein and fade out in my gallery. I wonder if there are any other, better options to do this?

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
#div_with_children{margin:0 auto;width:960px;height:499px;overflow:hidden}
.child{width:960px;height:500px}
</style>
</head>
<body>
<div id="div_with_children">
<div class="child" style="background:#0C3">1</div>
<div class="child" style="background:#993">2</div>
<div class="child" style="background:#F63">3</div>
<div class="child" style="background:#FC0">4</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
(function ($) {
    $.fn.bildspel_fade = function () {
        var $t = this,
            $c = $t.children(),
            $c0 = $c.eq(0),
            $c1 = $c.eq(-1),
            n = function () {
                var i = $t.children('.active').index(),
                    i1 = i + 1;
                $c.css('position', 'absolute');
                if ($c1.hasClass("active")) { // om man ÄR på sista
                    $c1.fadeOut(1000).removeClass('active');
                    $c0.addClass('active').fadeIn(1000);
                }
                else { // om man INTE ÄR på sista
                    $t.find('.active').fadeOut(1000).removeClass('active').next().addClass('active').fadeIn(1000);
                }
            };
        setInterval(n, 6000);
        $c.hide();
        $c0.addClass('active').fadeIn(1000);
    };
})(jQuery);
$(window).load(function () {
    $('#div_with_children').bildspel_fade();
});
</script>
</body>
</html>
share|improve this question
add comment

migrated from programmers.stackexchange.com Dec 21 '11 at 15:22

This question came from our site for professional programmers interested in conceptual questions about software development.

1 Answer

up vote 0 down vote accepted

Maybe using jQuery's $.data(); to store a bool variable or something like that. (http://api.jquery.com/jQuery.data/)

Also you could store a global variable with the active position (e.g. "3" for the 3rd item in the gallery), and then change it, using it along with the :eq() selector.

Either way, I think that the .active class approach is very useful because it gives you the chance to alter the CSS properties for that specific item on your gallery.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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