jQuery Animate spans in a div one by one -
i'm building type of one-bar jquery chart, goes except filling animation, script sections animate in same time. want make each section animate, complete, wait 0.1 sec pass the next.
$('#chart .chart-item').each(function() { $(this).animate({ width: $(this).data('w') + '%' }, 1000); });
.chart { width: 100%; margin-top: 40px; background: #eee; } .chart-item { display: inline-block; width: 0; height: 40px; border-left: 1px solid #fff; } .chart-item:first-child { border-left: none; } .chart-item:first-child { background-color: #0d47a1; } .chart-item:nth-child(2) { background-color: #1565c0; } .chart-item:nth-child(3) { background-color: #1976d2; } .chart-item:nth-child(4) { background-color: #1e88e5; } .chart-item:nth-child(5) { background-color: #2196f3; } .chart-item:nth-child(6) { background-color: #42a5f5; } .chart-item:nth-child(7) { background-color: #64b5f6; } .chart-item:nth-child(8) { background-color: #90caf9; } .chart-item:nth-child(9) { background-color: #bbdefb; } .chart-item:nth-child(10) { background-color: #e3f2fd; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="chart" class="chart"> <span class="chart-item" data-w="40.80"></span> <span class="chart-item" data-w="28.56"></span> <span class="chart-item" data-w="16.93"></span> <span class="chart-item" data-w="13.54"></span> </div>
nb: no problem wrap of last section in app works perfect.
just add delay
before each animation
below:
var delay = 0; $('#chart .chart-item').each(function() { $(this).delay(delay).animate({ width: $(this).data('w') + '%' },500); delay += 500; });
var delay = 0; $('#chart .chart-item').each(function() { $(this).delay(delay).animate({ width: $(this).data('w') + '%' },500); delay += 500; });
.chart { width: 100%; margin-top: 40px; background: #eee; } .chart-item { display: inline-block; width: 0; height: 40px; border-left: 1px solid #fff; } .chart-item:first-child { border-left: none; } .chart-item:first-child { background-color: #0d47a1; } .chart-item:nth-child(2) { background-color: #1565c0; } .chart-item:nth-child(3) { background-color: #1976d2; } .chart-item:nth-child(4) { background-color: #1e88e5; } .chart-item:nth-child(5) { background-color: #2196f3; } .chart-item:nth-child(6) { background-color: #42a5f5; } .chart-item:nth-child(7) { background-color: #64b5f6; } .chart-item:nth-child(8) { background-color: #90caf9; } .chart-item:nth-child(9) { background-color: #bbdefb; } .chart-item:nth-child(10) { background-color: #e3f2fd; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="chart" class="chart"> <span class="chart-item" data-w="40.80"></span> <span class="chart-item" data-w="28.56"></span> <span class="chart-item" data-w="16.93"></span> <span class="chart-item" data-w="13.54"></span> </div>
Comments
Post a Comment