2 回答

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊
這是一個(gè)重寫
您想要顯示無(wú)/塊而不是占用空間的可見(jiàn)性
香草JS
window.addEventListener("load",() => {
document.querySelector("header").addEventListener("click",(e) => {
const tgt = e.target;
e.currentTarget.querySelector("a.active").classList.remove("active")
tgt.classList.add("active")
if (tgt.tagName === "A") {
const id = tgt.href.split("#")[1];
[...document.querySelectorAll("main section")].forEach(section => {
section.classList.toggle("show",section.id === id)
})
}
});
document.querySelector(".active").click()
})
* {
margin: 0;
padding: 0;
}
html {
font-size: 100%;
}
body {
height: 100vh;
width: 100vw;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
header {
position: absolute;
top: 5%;
width: 100%;
display: flex;
justify-content: center;
}
header a {
margin: 1rem;
}
main {
width: 100vw;
display: flex;
flex-direction: column;
align-items: center;
align-items: center;
}
section {
position: static;
top: 50%;
}
@media (min-width: 640px) {
body {
font-size: 1rem;
}
}
@media (min-width: 960px) {
body {
font-size: 1.2rem;
}
}
@media (min-width: 1100px) {
body {
font-size: 1.5rem;
}
}
section { display:none }
a { text-decoration:none }
.active { text-decoration: underline }
.show { display:block }
<header>
<a href="#about" class="active">About</a> <a href="#work">Work</a> <a href="#blog">Blog</a>
</header>
<main>
<section id="about" >
<p>Developer, providing modern and responsive web development.</p>
</section>
<section id="contact">
<a href="mailto:macdevh@gmail.com">macdevh@gmail.com</a>
<div id="social">
<a href="https://instagram.com/machooper">I</a>
<a href="https://twitter.com/mac_hooper">T</a>
<a href="https://gitlab.com/macdevh">G</a>
</div>
</section>
<section id="work">
<div class="card">
<img id="card-img" src="https://via.placeholder.com/150">
<p id="card-title">Portfolio</p>
</div>
</section>
<section id="blog">
<div class="card">
<img id="card-img" src="https://via.placeholder.com/150">
<p id="card-title">Blog</p>
</div>
</section>
</main>
jQuery
$(function() {
$("header").on("click", "a",function(e) {
const $parent = $(this).closest("header");
$("a",$parent).removeClass("active")
$(this).addClass("active")
const id = this.href.split("#")[1];
$("main section").each(function() {
$(this).toggle(this.id === id)
})
});
$(".active").click()
})
* {
margin: 0;
padding: 0;
}
html {
font-size: 100%;
}
body {
height: 100vh;
width: 100vw;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
header {
position: absolute;
top: 5%;
width: 100%;
display: flex;
justify-content: center;
}
header a {
margin: 1rem;
}
main {
width: 100vw;
display: flex;
flex-direction: column;
align-items: center;
align-items: center;
}
section {
position: static;
top: 50%;
}
@media (min-width: 640px) {
body {
font-size: 1rem;
}
}
@media (min-width: 960px) {
body {
font-size: 1.2rem;
}
}
@media (min-width: 1100px) {
body {
font-size: 1.5rem;
}
}
section { display:none }
a { text-decoration:none }
.active { text-decoration: underline }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
<a href="#about" class="active">About</a> <a href="#work">Work</a> <a href="#blog">Blog</a>
</header>
<main>
<section id="about" >
<p>Developer, providing modern and responsive web development.</p>
</section>
<section id="contact">
<a href="mailto:macdevh@gmail.com">macdevh@gmail.com</a>
<div id="social">
<a href="https://instagram.com/machooper">I</a>
<a href="https://twitter.com/mac_hooper">T</a>
<a href="https://gitlab.com/macdevh">G</a>
</div>
</section>
<section id="work">
<div class="card">
<img id="card-img" src="https://via.placeholder.com/150">
<p id="card-title">Portfolio</p>
</div>
</section>
<section id="blog">
<div class="card">
<img id="card-img" src="https://via.placeholder.com/150">
<p id="card-title">Blog</p>
</div>
</section>
</main>

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
我通常使用jQuery和自定義 HTML5 屬性來(lái)實(shí)現(xiàn)這些目的。
在 CSS 中,我創(chuàng)建了一個(gè)名為“hide”的類:
.hide?{?display:?none?!important;?}
在 HTML5 代碼中,您可以為部分包含附加屬性(例如data-toggleable):
<section?id="work"?class="hide"?data-toggleable="true">
然后在 JS 代碼中,您可以使用單個(gè) jQuery 構(gòu)造隱藏將data-toggleable屬性設(shè)置為 true 的所有元素:
$(this).find('[data-toggleable="true"]').addClass('hide');
這一行將找到所有將data-toggleable屬性設(shè)置為“true”的 HTML 元素,并使它們不可見(jiàn)。最棒的是,它不會(huì)兩次分配該類(如果該元素已經(jīng)具有“hide”類,jQuery 將不會(huì)再次分配它)。最后,您可以僅顯示您想要顯示的部分 ID:
$('#work').removeClass('hide');
- 2 回答
- 0 關(guān)注
- 165 瀏覽
添加回答
舉報(bào)