Jacek Dominiak
Jacek Dominiak

Reputation: 877

Add margin dynamically to any found occurrence

How can I make it more elegant and universal? It works but I would like to have the margin added +52px to every next .alert occurrence.

var alerts = $(".alert");
if(alerts.is(":visible")) {
    if(alerts.length > 1 ) {
        alerts.eq(1).css('margin-top', '52px');
        alerts.eq(2).css('margin-top', '104px');
        alerts.eq(3).css('margin-top', '156px');
    }
    alerts.delay(5000).slideUp("slow");
}

Any thoughts?

Upvotes: 2

Views: 70

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382122

This :

 $(".alert:visible").css('margin-top', function(i){return 52*i+'px'})
    .delay(5000).slideUp("slow");

Upvotes: 5

Related Questions