1 回答

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊
我想不出任何純 CSS 方法來做到這一點(diǎn),但可以使用一些 JavaScript 輕松完成,檢查哈希值是否為空,然后在有值時(shí)顯示和隱藏#home它。
window.onhashchange = checkHash;
checkHash();
function checkHash() {
var home = document.getElementById('home');
//Check if the hash is empty
if (window.location.hash.substring(1) == '') {
home.style.display = 'block';
} else {
home.style.display = 'none';
}
}
.section {
display: none;
}
.section:target {
display: block !important;
}
<div id="home" class="section">
<a href="#content">Content</a>
<a href="#somthingElse">Somthing Else</a>
<h3>Home</h3>
</div>
<div id="content" class="section">
<a href="#home">Home</a>
<h3>Content</h3>
</div>
<div id="somthingElse" class="section">
<a href="#home">Home</a>
<h3>Somthing Else</h3>
</div>
褪色
我用的position: absolute
是這樣它們會(huì)堆疊在一起。z-index: -1
將使所有其余部分保持清晰,以阻止指針事件重疊。opacity: 0
顯然是用于淡入淡出。
我更改了 JS 腳本以簡(jiǎn)化 CSS?,F(xiàn)在,當(dāng)您轉(zhuǎn)到時(shí),example.com/html.html
您會(huì)被重定向到example.com/html.html#home
(沒有后退按鈕的歷史記錄更改)。
window.onhashchange = checkHash;
checkHash();
function checkHash() {
//Check if the hash is empty
if (window.location.hash.substring(1) == '') {
history.replaceState(undefined, undefined, "#home")
}
}
.section {
position: absolute;
z-index: -1;
opacity: 0;
transition: opacity 0.5s;
}
.section:target {
z-index: 1;
opacity: 1;
}
<div id="home" class="section">
<a href="#content">Content</a>
<a href="#somthingElse">Somthing Else</a>
<h3>Home</h3>
</div>
<div id="content" class="section">
<a href="#home">Home</a>
<h3>Content</h3>
</div>
<div id="somthingElse" class="section">
<a href="#home">Home</a>
<h3>Somthing Else</h3>
</div>
- 1 回答
- 0 關(guān)注
- 164 瀏覽
添加回答
舉報(bào)