I would like to show list items one by one by clicking on a button.
Here is my code:
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
$('ol > li').hide();
$('button').click(function() {
if ($('ol > li:first').is(':visible'))
$('ol > li:visible:last').next().show();
else
$('ol > li:first').show();
});
});
</script>
<button type="button">Show</button>
<ol>
<li>#1</li>
<li>#2</li>
<li>#3</li>
<li>#4</li>
<li>#5</li>
<li>#6</li>
</ol>
It works but I am pretty sure that can be optimize. What would be the best way?
Thanks in advance!
You can do the show as a single line instead:
Working Example - http://jsfiddle.net/WV84H/
You can make it a little bit more efficient by caching the list of li's:
Working Example - http://jsfiddle.net/WV84H/1/
I use css
transition-delay
property for this and scss to increment it for each<li>
elementto show
<li>
I use JS just to set classshow
which is responsible for opacity changesee example with compiled sass below:
Working Example http://jsfiddle.net/YVeuY/