gpt4 book ai didi

JavaScript上下箭头菜单(仅限Javascript)

转载 作者:行者123 更新时间:2023-11-30 06:21:36 24 4
gpt4 key购买 nike

Javascript 上下箭头菜单

请在这里发布我的代码,我想为菜单创建一些 javascript 箭头,功能需要向上和向下,所以我试图弄清楚它只在 jquery 中我到处搜索,那里有太多令人困惑的代码.但我只需要一个简单而纯粹的javascript代码。请有人愿意帮助我。

var menuup = document.getElementsByClassName("btn-down");
var menudwn = document.getElementsByClassName('btn-open');
nav {
position: relative;
padding: 45px 0px 0px 180px;
width: 1rem;
}

nav ul {
list-style: none;
margin: 0;
padding: 0;
}

nav>ul>li {
float: left;
background: blue;
}

nav ul::after {
content: '';
display: block;
clear: both;
}

nav ul li:hover>ul {
display: block;
}

nav ul li a {
display: inline-block;
color: #fff;
padding: 0.9rem 1rem;
text-decoration: none;
width: 120px;
}

nav ul li a:hover {
background-color: #339999;
}

.btn-open:after {
font-family: "FontAwesome";
content: "\f0de";
color: #fff;
font-size: 22px;
}

.btn-down:after {
font-family: "FontAwesome";
content: "\f0dd";
color: #fff;
font-size: 22px;
position: relative;
left: 10px;
}
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"/>
<nav>
<ul>
<li><a href="#" class="btn-down">About us</a></li>
</ul>
</nav>

最佳答案

这是你想要的吗?

var anchors = Array.from(document.querySelectorAll('a'));

anchors.map(anchor => {
if(anchor.classList.contains('btn-open') || anchor.classList.contains('btn-down')) {
anchor.addEventListener('click', () => {
if(anchor.classList.contains('btn-open')) {
anchor.classList.remove('btn-open');
anchor.classList.add('btn-down');
} else {
anchor.classList.add('btn-open');
anchor.classList.remove('btn-down');
}
});
}
});
nav {
position: relative;
padding: 45px 0px 0px 180px;
width: 1rem;
}

nav ul {
list-style: none;
margin: 0;
padding: 0;
}

nav>ul>li {
float: left;
background: blue;
}

nav ul::after {
content: '';
display: block;
clear: both;
}

nav ul li:hover>ul {
display: block;
}

nav ul li a {
display: inline-block;
color: #fff;
padding: 0.9rem 1rem;
text-decoration: none;
width: 120px;
}

nav ul li a:hover {
background-color: #339999;
}

.btn-open:after {
font-family: "FontAwesome";
content: "\f0de";
color: #fff;
font-size: 22px;
position: relative;
left: 10px;
top: 8px;
}

.btn-down:after {
font-family: "FontAwesome";
content: "\f0dd";
color: #fff;
font-size: 22px;
position: relative;
left: 10px;
top: -2px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />

<nav>
<ul>
<li><a href="#" class="btn-down">About us</a></li>
<li><a href="#" class="btn-down">Products</a></li>
</ul>
</nav>

关于JavaScript上下箭头菜单(仅限Javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52756143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com