JQuery - Tie inside a DIV

0

Hello, good morning.

Simple:

I'm giving a For a div:

<div class="Laço">
   <div class="ItemLaco"></div>
   <div class="ItemLaco"></div>
   <div class="ItemLaco"></div>
</div>

Now I need to give a For and let it dynamically leave the current loop visible and the previous invisible! is it possible?

<script>
$(function () {
    this.class
    setInterval($(".ItemLaco").each(function (index, element) {
        alert(element);
    }),5000);
});

    
asked by anonymous 22.06.2015 / 07:34

1 answer

2

Your code has some problems but I imagine you want to do a slideshow effect, changing div.

You can do it like this:

$(function () {
    var divs = $(".ItemLaco");
    var mostra = 0;

    function slide() {
        mostra++;
        if (mostra == divs.length) mostra = 0;
        divs.removeClass('mostrar');
        divs.eq(mostra).addClass('mostrar');
    }

    setInterval(slide, 2000);
});

jsFiddle: link

What you have% does not make sense, I do not understand what you want to do with it, so I can not fix anything.

In this.class you should take a look this question / answer , it's the same problem, that is you have to pass a function as argument to setInterval use / call x at x time.

Then you have setInterval , it's not very clear what you want to do, but in jQuery methods applied to collections of elements are applied to all. For example as I used above $(".ItemLaco").each remove the CSS class from all elements of this collection, even if some do not.

I used CSS classes to show and hide:

.ItemLaco {
    display: none;
}
.mostrar {
    display: block;
}

Because it's better this way, cleaner and everything is in CSS.

    
22.06.2015 / 09:17