3 回答

TA貢獻1831條經驗 獲得超4個贊
雖然我想您可以純粹CSS
出于此目的而使用,但付諸JS
實踐以達到預期結果要容易得多。
<!DOCTYPE html>
<html>
<head>
? ? <title></title>
? ? <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
? ? <link >
? ? <link rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
? ? <link rel="stylesheet" type="text/css" href="style.css">
? ? <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
? ? <style>
? ? ? ? .PinDiv{
? ? ? ? ? ? display: none;
? ? ? ? }
? ? </style>
</head>
<body>
? ? <div>
? ? ? ? <div>
? ? ? ? ? ? <section>
? ? ? ? ? ? ? ? <figure>
? ? ? ? ? ? ? ? ? ? <i class="HoverPin" onmouseover="document.getElementById('PinDiv1').style.display='block';" onmouseout="document.getElementById('PinDiv1').style.display='none';">1</i>
? ? ? ? ? ? ? ? ? ? <i class="HoverPin" onmouseover="document.getElementById('PinDiv2').style.display='block';" onmouseout="document.getElementById('PinDiv2').style.display='none';">2</i>
? ? ? ? ? ? ? ? ? ? <i class="HoverPin" onmouseover="document.getElementById('PinDiv3').style.display='block';" onmouseout="document.getElementById('PinDiv3').style.display='none';">3</i>
? ? ? ? ? ? ? ? ? ? <i class="HoverPin" onmouseover="document.getElementById('PinDiv4').style.display='block';" onmouseout="document.getElementById('PinDiv4').style.display='none';">4</i>
? ? ? ? ? ? ? ? </figure>
? ? ? ? ? ? </section>
? ? ? ? </div>? ? ? ? ? ??
? ? ? ? <div id="PinDiv1" class="PinDiv">
? ? ? ? ? ? <h3>Text item 1</h3>
? ? ? ? </div>
? ? ? ? <div id="PinDiv2" class="PinDiv">
? ? ? ? ? ? <h3>Text item 2</h3>
? ? ? ? </div>
? ? ? ? <div id="PinDiv3" class="PinDiv">
? ? ? ? ? ? <h3>Text item 3</h3>
? ? ? ? </div>
? ? ? ? <div id="PinDiv4" class="PinDiv">
? ? ? ? ? ? <h3>Text item 4</h3>
? ? ? ? </div>
? ? </div>
</body>
</html>
使用JQuery我們可以稍微清理一下:
<!DOCTYPE html>
<html>
<head>
? ? <title></title>
? ? <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
? ? <link >
? ? <link rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
? ? <link rel="stylesheet" type="text/css" href="style.css">
? ? <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
? ? <style>
? ? ? ? .PinDiv{
? ? ? ? ? ? display: none;
? ? ? ? }
? ? </style>
</head>
<body>
? ? <div>
? ? ? ? <div>
? ? ? ? ? ? <section>
? ? ? ? ? ? ? ? <figure>
? ? ? ? ? ? ? ? ? ? <i class="HoverPin" onmouseover="$('#PinDiv1').css('display','block');" onmouseout="$('#PinDiv1').css('display','none');">1</i>
? ? ? ? ? ? ? ? ? ? <i class="HoverPin" onmouseover="$('#PinDiv2').css('display','block');" onmouseout="$('#PinDiv2').css('display','none');">2</i>
? ? ? ? ? ? ? ? ? ? <i class="HoverPin" onmouseover="$('#PinDiv3').css('display','block');" onmouseout="$('#PinDiv3').css('display','none');">3</i>
? ? ? ? ? ? ? ? ? ? <i class="HoverPin" onmouseover="$('#PinDiv4').css('display','block');" onmouseout="$('#PinDiv4').css('display','none');">4</i>
? ? ? ? ? ? ? ? </figure>
? ? ? ? ? ? </section>
? ? ? ? </div>? ? ? ? ? ??
? ? ? ? <div id="PinDiv1" class="PinDiv">
? ? ? ? ? ? <h3>Text item 1</h3>
? ? ? ? </div>
? ? ? ? <div id="PinDiv2" class="PinDiv">
? ? ? ? ? ? <h3>Text item 2</h3>
? ? ? ? </div>
? ? ? ? <div id="PinDiv3" class="PinDiv">
? ? ? ? ? ? <h3>Text item 3</h3>
? ? ? ? </div>
? ? ? ? <div id="PinDiv4" class="PinDiv">
? ? ? ? ? ? <h3>Text item 4</h3>
? ? ? ? </div>
? ? </div>
</body>
</html>
如果你想讓它們保持開放,你可以這樣做:
<!DOCTYPE html>
<html>
<head>
? ? <title></title>
? ? <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
? ? <link >
? ? <link rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
? ? <link rel="stylesheet" type="text/css" href="style.css">
? ? <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
? ? <style>
? ? ? ? .PinDiv{
? ? ? ? ? ? display: none;
? ? ? ? }
? ? </style>
</head>
<body>
? ? <div>
? ? ? ? <div>
? ? ? ? ? ? <section>
? ? ? ? ? ? ? ? <figure>
? ? ? ? ? ? ? ? ? ? <i class="HoverPin" onmouseover="$('#PinDiv1').css('display','block');" onmouseout="$('.PinDiv:not(#PinDiv1)').css('display','none');">1</i>
? ? ? ? ? ? ? ? ? ? <i class="HoverPin" onmouseover="$('#PinDiv2').css('display','block');" onmouseout="$('.PinDiv:not(#PinDiv2)').css('display','none');">2</i>
? ? ? ? ? ? ? ? ? ? <i class="HoverPin" onmouseover="$('#PinDiv3').css('display','block');" onmouseout="$('.PinDiv:not(#PinDiv3)').css('display','none');">3</i>
? ? ? ? ? ? ? ? ? ? <i class="HoverPin" onmouseover="$('#PinDiv4').css('display','block');" onmouseout="$('.PinDiv:not(#PinDiv4)').css('display','none');">4</i>
? ? ? ? ? ? ? ? </figure>
? ? ? ? ? ? </section>
? ? ? ? </div>? ? ? ? ? ??
? ? ? ? <div id="PinDiv1" class="PinDiv">
? ? ? ? ? ? <h3>Text item 1</h3>
? ? ? ? </div>
? ? ? ? <div id="PinDiv2" class="PinDiv">
? ? ? ? ? ? <h3>Text item 2</h3>
? ? ? ? </div>
? ? ? ? <div id="PinDiv3" class="PinDiv">
? ? ? ? ? ? <h3>Text item 3</h3>
? ? ? ? </div>
? ? ? ? <div id="PinDiv4" class="PinDiv">
? ? ? ? ? ? <h3>Text item 4</h3>
? ? ? ? </div>
? ? </div>
</body>
</html>

TA貢獻1820條經驗 獲得超9個贊
你可以嘗試這個快速的 css 技巧來實現你所需要的:)
CSS:
.hoverable:not(:hover) + .show-on-hover {
display: none;
}
HTML:
<div style="display: flex">
<i class="hover pin 1">1
<div class="hide">
<h3>Text item 1</h3>
</div>
</i>
<i class="hover pin 2">2
<div class="hide">
<h3>Text item 2</h3>
</div>
</i>
<i class="hover pin 3">3
<div class="hide">
<h3>Text item 3</h3>
</div>
</i>
<i class="hover pin 4">4
<div class="hide">
<h3>Text item 4</h3>
</div>
</i>
</div>
.hide {
display: none;
}
i:hover > .hide {
display: block;
}
<div style="display: flex">
<i class="hover pin 1">1
<div class="hide">
<h3>Text item 1</h3>
</div>
</i>
<i class="hover pin 2">2
<div class="hide">
<h3>Text item 2</h3>
</div>
</i>
<i class="hover pin 3">3
<div class="hide">
<h3>Text item 3</h3>
</div>
</i>
<i class="hover pin 4">4
<div class="hide">
<h3>Text item 4</h3>
</div>
</i>
</div>

TA貢獻1830條經驗 獲得超3個贊
如果您不想使用 JS(發布的其他答案也可以),您可以使用:target選擇器。需要注意的是用戶需要單擊該元素。
div.text {
display: none;
}
div:target {
display: block;
}
.pin {
margin-left: 15px;
}
<div>
<div>
<section>
<figure>
<a href="#text1" class="pin">1</i>
<a href="#text2" class="pin">2</i>
<a href="#text3" class="pin">3</i>
<a href="#text4" class="pin">4</i>
</figure>
</section>
</div>
<div class="text" id="text1">
<h3>Text item 1</h3>
</div>
<div class="text" id="text2">
<h3>Text item 2</h3>
</div>
<div class="text" id="text3">
<h3>Text item 3</h3>
</div>
<div class="text" id="text4">
<h3>Text item 4</h3>
</div>
</div>
- 3 回答
- 0 關注
- 242 瀏覽
添加回答
舉報