jQuery: List expands on page load
I've been looking for something very simple: How to make a side navigation expand with animation on page load, but all the tutorial websites I usually go to don't seem to have it.
The closest I could find is this jQuery sample: http://codeblitz.w开发者_开发技巧ordpress.com/2009/04/15/jquery-animated-collapsible-list/
I've managed to strip down the list like so:
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(function(){
$('li')
.css('pointer','default')
.css('list-style','none');
$('li:has(ul)')
.click(function(event){
if (this == event.target) {
$(this).css('list-style',
(!$(this).children().is(':hidden')) ? 'none' : 'none');
$(this).children().toggle('slow');
}
return false;
})
.css({cursor:'pointer', 'list-style':'none'})
.children().hide();
$('li:not(:has(ul))').css({cursor:'default', 'list-style':'none'});
});
</script>
</head>
<body>
<fieldset>
<legend>Collapsable List Demo</legend>
<ul>
<li>A - F</li>
<li>G - M
<ul>
<li>George Kent Technology Centre</li>
<li>Hampshire Park</li>
<li>George Kent Technology Centre</li>
<li>Hampshire Park</li>
</ul>
</li>
<li>
N - R
</li>
<li>S - Z</li>
</ul>
</fieldset>
My question is:
Is there any way to make this list expand on page load instead of on click? I also don't need it to collapse at all; basically I need only the animating expansion.
Thank you for your time and advice. :)
edit Wonder if we could do the reversed effect of this...
Add this line after $('li:not(:has(ul))').css({cursor:'default', 'list-style':'none'});
$('li:has(ul)').click();
I would use setTimeout
inside of $(document).ready()
to animate the list in after a short period of time after the page has loaded:
var animateList = function() {
$('li:has(ul)').each(function() {
$(this).css('list-style', (!$(this).children().is(':hidden')) ? 'none' : 'none');
$(this).children().toggle('slow');
})
};
setTimeout(animateList, 500);
You can adjust the time period as necessary.
Example: http://jsfiddle.net/andrewwhitaker/7wQT5/
精彩评论