gpt4 book ai didi

javascript - 如何隐藏并同时显示另一个部分(类似于 Accordion )

转载 作者:行者123 更新时间:2023-12-04 07:50:32 29 4
gpt4 key购买 nike

我需要一些帮助来制作类似于 Accordion 的东西。目标是,如果您单击导航中的链接,一个部分会消失,而您单击的部分会出现(在相同位置且不明显)。
如果可能,它还应该自动滚动到该部分的开头(导航的结尾)。
任何建议或起点?
JSFiddle

$(function() {
var shrinkHeader = 50;
$(window).scroll(function() {
var scroll = getCurrentScroll();
if (scroll >= shrinkHeader) {
$('#navbar').addClass('shrink');
} else {
$('#navbar').removeClass('shrink');
}
});

function getCurrentScroll() {
return window.pageYOffset || document.documentElement.scrollTop;
}
});
* {
margin: 0;
padding: 0;
font-family: helvetica;
}

body {
height: 300vh;
}

h1 {
text-align: center;
font-size: 5rem;
top: 2vh;
position: relative;
}

h2 {
text-align: center;
font-weight: 300;
margin-top: 3vh;
font-size: 1rem;
}

ul {
list-style: none;
text-align: center;
}

ul li {
display: inline-table;
margin-left: 0.25vw;
padding: 0.25vw;
}

a {
color: #fff;
text-transform: uppercase;
text-decoration: none;
letter-spacing: 0.15em;

display: inline-block;
padding: 15px 20px;
position: relative;
}

#navbar {
text-align: center;
width: 100%;
color: white;
z-index: 999;
transition: 0.3s;
}

#navwrapper {
background: linear-gradient(250deg, #0061ff, #60efff);
background-size: 400% 400%;
height: 250px;
}

#navbar.shrink {
height: 130px;
line-height: 18px;
position: fixed;
background-color: lightgray !important;
transition: 0.3s;
}

#navbar.shrink li {
font-size: 1.2rem;
margin-top: 1.5vh;
transition: 0.3s;
}

#navbar.shrink a {
color: #343a40 !important;
}

#navbar.shrink a:after {
background: none repeat scroll 0 0 transparent;
bottom: 0;
content: "";
display: block;
height: 2px;
position: absolute;
background: #343a40 !important;
}

#navbar.shrink h1 {
font-size: 4rem;
color: #343a40;
top: 2vh;
transition: 0.3s;
}
  <nav id="navwrapper">
<script class="cssdeck" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="javascript/navbar fixed.js"></script>
<div id="navbar" class="navbar navbar-dark bg-primary navbar-fixed-top">
<h1 id="titel">HEADLINE</h1>
<ul>
<li class="nav-item"><a href="#home" class="nav-link">
<h2>content1</h2>
</a></li>
<li class="nav-item"><a href="#wir-ueber-uns" class="nav-link">
<h2>content2</h2>
</a></li>
<li class="nav-item"><a href="#aktionen" class="nav-link">
<h2>content3</h2>
</a></li>
<li class="nav-item"><a href="#terminvereinbarung" class="nav-link">
<h2>content4</h2>
</a></li>
<li class="nav-item"><a href="#infos" class="nav-link">
<h2>content5</h2>
</a></li>
</ul>
</div>
</nav>

<section id="content1">
<div>
Lorem ipsum
</div>
</section>

<section id="content2">
<div>
Lorem ipsum
</div>
</section>

<section id="content3">
<div>
Lorem ipsum
</div>
</section>

<section id="content4">
<div>
Lorem ipsum
</div>
</section>

<section id="content5">
<div>
Lorem ipsum
</div>
</section>

最佳答案

希望这是有帮助的。我花了几个小时才把它拉下来。甚至在 stackoverflow 上搜索

 <!DOCTYPE html>
<html>
<title>Menu animation using javascript</title>
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>
#navi{
position: absolute;
top: 40px;
left: 3%;
width: 25%;
}

.menuItem{
margin-bottom: 5px;
width: 100%;
}

.menuPage{
border: 1px solid #000000;
background-color: #dcdcdc;
text-align: center;
padding: 2px 4px;
}
</style>

<body>

<div id="navi">

<div id="Ldon" class="menuItem" onclick="openCity('London', 'Ldon')"><p class="menuPage">London</p>
<div id="London" class="city" style="display:none">
<h2>London</h2>
<p>London is the capital city of England.</p>
</div>
</div>

<div id="Pris" class="menuItem" onclick="openCity('Paris', 'Pris')"><p class="menuPage">Paris</p>
<div id="Paris" class="city" style="display:none">
<h2>Paris</h2>
<p>Paris is the capital of France.</p>
</div>
</div>

<div id="Tkyo" class="menuItem" onclick="openCity('Tokyo', 'Tkyo')"><p class="menuPage">Tokyo</p>
<div id="Tokyo" class="city" style="display:none">
<h2>Tokyo</h2>
<p>Tokyo is the capital of Japan.</p>
</div>
</div>

</div>

<script>
function openCity(cityName, newPos) {
var i;
var y = document.getElementsByClassName("menuItem");
var x = document.getElementsByClassName("city");
var moevePos = document.getElementById(newPos);

for (i = 0; i < y.length; i++) {
y[i].style.position = "";
y[i].style.top = "";
}

for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}

document.getElementById(cityName).style.display = "block";
moevePos.style.position = "absolute";
moevePos.style.top = "80px";
}
</script>

</body>
</html>

关于javascript - 如何隐藏并同时显示另一个部分(类似于 Accordion ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67005760/

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