做好的手风琴效果如下,具体看代码:
html代码
复制代码 代码如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Accordion</title>
<style>
#accordion{
width:200px;
}
#accordion ul{
list-style: none;
display:none;
}
.first-level{
background-color: #d8d8d8;
background-color: rgba(236, 208, 208, 0.53);
border-radius: 4px;
display: block;
cursor: pointer;
position: relative;
margin: 2px 0 0 0;
padding: 8px;
min-height: 0;
}
</style>
</head>
<body>
<div id="accordion">
<div>
<h3 class="first-level">level one</h3>
<ul>
<li>first item</li>
<li>second item</li>
<li>third item</li>
</ul>
</div>
<div>
<h3 class="first-level">level one</h3>
<ul>
<li>first item</li>
<li>second item</li>
<li>third item</li>
</ul>
</div>
<div>
<h3 class="first-level">level one</h3>
<ul>
<li>first item</li>
<li>second item</li>
<li>third item</li>
</ul>
</div>
</div>
<script src="/UploadFiles/2021-04-02/common.js"><script>
var heads = document.querySelectorAll(".first-level");
function headClick(event){
var target = EventUtil.getTarget(event);
toggleDisplay(target.parentNode.querySelector("ul"));
}
for(var i=0;i<heads.length;i++){
EventUtil.addHandler(heads[i], "click", headClick);
}
window.onunload = function (){
for(var i=0;i<heads.length;i++){
EventUtil.removeHandler(heads[i], "click", headClick);
}
heads = null;
}
common.js文件
复制代码 代码如下:
var EventUtil = {
addHandler : function (element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
} else if (element.attachEvent) {
element.attachEvent("on" + type, handler);
} else {
element["on" + type] = handler;
}
},
removeHandler : function (element, type, handler) {
if (element.removeEventListener) {
element.removeEventListener(type, handler, false);
} else if (element.detachEvent) {
element.detachEvent("on" + type, handler);
} else {
element["on" + type] = null;
}
},
getEvent : function (event) {
return event || window.event;
},
getTarget : function (event) {
return event.target || event.srcElement;
}
}
var getStyle = function (el, style) {
if (el.currentStyle) {
style = style.replace(/\-(\w)/g, function (all, letter) {
return letter.toUpperCase();
});
var value = el.currentStyle[style];
return value;
} else {
return document.defaultView.getComputedStyle(el, null).getPropertyValue(style);
}
}
var toggleDisplay = function (element) {
var display = getStyle(element, "display");
if (display == "none") {
element.style.display = "block";
} else {
element.style.display = "none";
}
}
原生js,手风琴
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 杨泰瑞《City Boy》[320K/MP3][28.71MB]
- 胡彦斌《失业情歌》首版[WAV+CUE]
- 杨泰瑞《City Boy》[FLAC/分轨][159.66MB]
- APM亚流新世代《一起冒险》[320K/MP3][68.66MB]
- FIM《天伦乐》(DaydreamsAndLullabies)24K金碟[WAV+CUE]
- 【雨果唱片】中国管弦乐《鹿回头》
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]