实现一个下拉菜单 点击title展开关闭菜单项 点击菜单项 让title显示点击的菜单项内容
<div class="wrap">
<div class="menu">
<div class="title">A-SOUL</div>
<ul class="list">
<li>嘉然</li>
<li>向晚</li>
<li>乃琳</li>
<li>贝拉</li>
<li>珈乐</li>
</ul>
</div>
</div>
* {
margin: 0;
padding: 0;
font: 30px / 90px "微软雅黑";
}
ul {
list-style: none;
}
.wrap {
width: 1000px;
height: 1000px;
position: absolute;
left: 50%;
top: 50%;
margin-top: -500px;
margin-left: -500px;
background: aliceblue;
}
.menu {
width: 330px;
border-radius: 10px;
overflow: hidden;
margin-top: 285px;
margin-left: 335px;
background: #f87a75;
box-shadow: 0 20px 50px rgba(248, 122, 117, .5);
}
.title {
height: 90px;
padding-left: 112px;
box-sizing: border-box;
color: #fff;
position: relative;
}
.title::after {
content: '';
position: absolute;
right: 36px;
top: 41px;
background: url(images/ar.png) no-repeat;
width: 28px;
height: 16px;
}
.list {
width: 316px;
padding-left: 7px;
padding-bottom: 5px;
/* display: none; */
}
.list li {
height: 90px;
margin-bottom: 2px;
background: #fee4e3;
border-radius: 20px;
padding-left: 112px;
box-sizing: border-box;
background-position: 23px 24px;
background-repeat: no-repeat;
}
.list li:hover {
background-color: #fff;
box-shadow: 0 0 50px rgba(248, 122, 117, 0.93);
position: relative;
}
.list li:nth-of-type(1) {
background: #fee4e3 url(images/rr.png) no-repeat 30px center/45px 45px;
}
.list li:nth-of-type(2) {
background: #fee4e3 url(images/向晚.png) no-repeat 30px center/45px 45px;
}
.list li:nth-of-type(3) {
background: #fee4e3 url(images/乃琳.png) no-repeat 30px center/45px 45px;
}
.list li:nth-of-type(4) {
background: #fee4e3 url(images/贝拉.png) no-repeat 30px center/45px 45px;
}
.list li:nth-of-type(5) {
background: #fee4e3 url(images/珈乐.png) no-repeat 30px center/45px 45px;
}
</style>
var menu = document.querySelector('.menu')
var title = document.querySelector('.title')
var ul = document.querySelector('.list')
var li = document.querySelectorAll('li')
var flag = true
title.onclick = function () {
if (flag) {
ul.style.display = 'none'
// flag = false
} else {
ul.style.display = 'block'
// flag = true
if (title.innerHTML != 'A-SOUL') {
title.innerHTML = 'A-SOUL'
}
}
flag = !flag
}
for (let i = 0; i < li.length; i++) {
li[i].onclick = function () {
title.innerHTML = li[i].innerHTML
ul.style.display = 'none'
flag = false
}
}
这里的for循环 新手小伙伴们用var声明i的话可能会出现问题
可以看看另一篇文章
juejin.cn/post/706521…