gpt4 book ai didi

javascript - CSS Accordion 菜单按钮

转载 作者:太空宇宙 更新时间:2023-11-04 07:50:24 25 4
gpt4 key购买 nike

希望在这里找到一些帮助,因为我是网页设计的新手。只是想知道为什么我的 Accordion 按钮不起作用?单击 .container 内部时,它应该运行我的动画并显示我的 <ul> .现在动画有效,但我似乎无法操纵我的 CSS 并更改 display:blockdisplay:none .

.container {
display: inline-block;
cursor: pointer;
}

.bar1,
.bar2,
.bar3 {
width: 35px;
height: 5px;
background-color: #000000;
margin: 6px 0;
transition: 0.4s;
}

.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-9px, 6px);
-moz-transform: rotate(-45deg) translate(-9px, 6px);
transform: rotate(-45deg) translate(-9px, 6px);
}

.change .bar2 {
opacity: 0;
}

.change .bar3 {
-webkit-transform: rotate(45deg) translate(-8px, -8px);
-moz-transform: rotate(45deg) translate(-8px, -8px);
transform: rotate(45deg) translate(-8px, -8px);
}

.links {
float: left;
margin-left: 15%;
display: none;
}
<html>

<head>

<script>
function myFunction(x) {
x.classList.toggle("change");
}
</script>
<script>
var acc = document.getElementsByClassName("container");
acc.onclick = function() {
var panel = getElementsByClassName("links");
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
}
</script>

</head>

<body>
<div class="container" onclick="myFunction(this)">
<ul class="links">
<li><a href="#skills"><span>Skills</span></a></li>
<li><a href="#about"><span>About</span></a></li>
<li> <a href="#contact"><span>Contact</span></a></li>
</ul>
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
</body>

</html>

最佳答案

我发现 jQuery sintaxis 更容易。

希望对你有帮助

$('.container').click(function(){
var panel = $('#links');

if (panel.css('display') === "block") {
panel.css('display',"none");
} else {
panel.css('display',"block");
}
})
.container {
display: inline-block;
cursor: pointer;
}

.bar1,
.bar2,
.bar3 {
width: 35px;
height: 5px;
background-color: #000000;
margin: 6px 0;
transition: 0.4s;
}

.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-9px, 6px);
-moz-transform: rotate(-45deg) translate(-9px, 6px);
transform: rotate(-45deg) translate(-9px, 6px);
}

.change .bar2 {
opacity: 0;
}

.change .bar3 {
-webkit-transform: rotate(45deg) translate(-8px, -8px);
-moz-transform: rotate(45deg) translate(-8px, -8px);
transform: rotate(45deg) translate(-8px, -8px);
}

.links {
float: left;
margin-left: 15%;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<html>

<head>

<script>
function myFunction(x) {
x.classList.toggle("change");
}
</script>
</head>

<body>
<div class="container" onclick="myFunction(this)">
<ul class="links" id="links">
<li><a href="#skills"><span>Skills</span></a></li>
<li><a href="#about"><span>About</span></a></li>
<li> <a href="#contact"><span>Contact</span></a></li>
</ul>
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
</body>

</html>

关于javascript - CSS Accordion 菜单按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47816893/

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