accordion手风琴效果
网站常用效果之一,以下为简化版,用于学习javascript基础知识。
效果图:
accordion手风琴效果 - 纯js简化版
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>accordion手风琴效果 - 纯js简化版</title> <style type="text/css"> *{margin: 0; padding: 0;} #acclist {list-style: none; 400px; margin: 10px; padding: 0;} #acclist li { background: #ccc; list-style: none; margin: 0 0 1px 0; padding: 0;} #acclist li h3 {font-size: 13px; color: #fff; background: #666; padding: 5px;cursor:pointer;} #acclist li div { display: none; padding: 5px; height: 80px;} </style> <script type="text/javascript"> window.onload = function() { var acc = document.getElementById('acclist'); var accli = acc.getElementsByTagName('li'); for(var i=0; i<accli.length; i++) { accli[i].getElementsByTagName('h3')[0].index = i; accli[i].getElementsByTagName('h3')[0].onmouseover = function() { for(var a=0; a<accli.length; a++) { accli[a].getElementsByTagName('div')[0].style.display = "none"; } if(accli[this.index].getElementsByTagName('div')[0].style.display == "block") { accli[this.index].getElementsByTagName('div')[0].style.display = "none"; } else { accli[this.index].getElementsByTagName('div')[0].style.display = "block" } } } accli[0].getElementsByTagName('div')[0].style.display = "block"; } </script> </head> <body> <div> <ul id="acclist"> <li> <h3>acctit01</h3> <div>accdiv01</div> </li> <li> <h3>acctit02</h3> <div>accdiv02</div> </li> <li> <h3>acctit03</h3> <div>accdiv03</div> </li> <li> <h3>acctit04</h3> <div>accdiv04</div> </li> <li> <h3>acctit05</h3> <div>accdiv05</div> </li> <li> <h3>acctit06</h3> <div>accdiv06</div> </li> </ul> </div> </body> </html>