简体   繁体   English

悬停在另一个div上时使div出现

[英]Making a div appear upon hovering on another div

I have searched a lot for this, I can not find an answer. 我为此进行了很多搜索,找不到答案。 I want the div SubTopicInfo1 to appear when hovering over the div subTopicNav1. 我希望将div SubTopicInfo1悬停在div subTopicNav1上时出现。 I need it to be only CSS. 我只需要CSS。

Here is what I have: 这是我所拥有的:

Fiddle 小提琴

HTML: HTML:

<div id="NavBar">

                <div id="Mainbutton">
                <center><h2 style="margin-top: 7px;"> Main </h2></center>
                </div>

                <div id="topic1button">
                <center><h2 style="margin-top: 7px;"> Skiing </h2></center>
                </div>

                <div id="topic2button">
                <center><h2 style="margin-top: 12px;"> Movies </h2></center>
                </div>


        </div>

        <div id="Main">

                <div id="IntroImage">

                </div>

                <div id="Title">
                        <h2 id="Titlemain"> TYLER POTTS</h2>
                </div>

                <div id="SubTopicNav">

                        <div id="subTopicNav1">
                                <center><h2>About Me</h2></center>
                        </div>

                        <div id="subTopicNav2">
                                <center><h2>Skiing</h2></center>
                        </div>

                        <div id="subTopicNav3">
                                <center><h2>Movies</h2></center>
                        </div>

                </div>

                <div id="SubTopicInfo">

                        <div id="SubTopicInfo1">
                        <h1> Test </h1>
                        </div>

                        <div id="SubTopicInfo2">

                        </div>

                        <div id="SubTopicInfo3">

                        </div>

                </div>

                <div id="Footer">

                </div>

        </div>

CSS: CSS:

#NavBar
{
width: 750px;
height: 40px;
background-color: white;
margin: 0 auto;
border-top: 5px solid #FFD640;
border-bottom: 5px solid #FFD640;
}


#topic2button
{
width: 100px;
height: 45px;
margin-top:-50px;
margin-left: 215px;
float:left;
}

#topic2button:hover
{
background-color: #FFD640;
}

#Mainbutton
{
width: 100px;
height: 45px;
margin-top:;
margin-left: 315px;
float:left;
}

#Mainbutton:hover
{
background-color: #FFD640;
}

#topic1button
{
width: 100px;
height: 45px;
float: left;
}

#topic1button:hover
{
background-color: #FFD640;
}

#Main
{
width: 750px;
height: 1000px;
margin: 0 auto;
background-color: white;
}

#IntroImage
{
width: 750px;
height: 150px;
background-image:url("http://skiersedgeproshop.com/wp/wp-content/uploads/2013/11/snow-mountain-ski1.jpg")
}

#IntroImage:hover
{
opacity: 0.8;
}

#Title
{
width: 500px;
height: 40px;
border-top: 5px solid #FFD640;
border-bottom: 5px solid #FFD640;
margin:0 auto;
margin-top: 10px;
}

#Titlemain
{
margin-left: 170px;
margin-top: 6px; 
}

#SubTopicNav
{
width: 150px;
height: 400px;
border-top: 5px solid #FFD640;
border-bottom: 5px solid #FFD640;
margin-top: 25px;
float:left;
}

#subTopicNav1
{
margin-top: 60px;
width: 150px;
height: 50px;
}

#subTopicNav2
{
margin-top: 60px;
width: 150px;
height: 50px;
}

#subTopicNav3
{
margin-top: 60px;
width: 150px;
height: 50px;
}

#subTopicNav1:hover 
{
margin-top: 60px;
width: 150px;
height: 50px;
background-color: #FFD640;
}

#subTopicNav2:hover
{
margin-top: 60px;
width: 150px;
height: 50px;
background-color: #FFD640;
}

#subTopicNav3:hover
{
margin-top: 60px;
width: 150px;
height: 50px;
background-color: #FFD640;
}

#SubTopicInfo
{
width: 600px;
height: 400px;
border-top: 5px solid #FFD640;
border-bottom: 5px solid #FFD640;
margin-top: 25px;
float: left;
}

#SubTopicInfo1
{
display:none;
background-color: #FFD640;
}

#subTopicNav1:hover + #SubTopicInfo1
{
display: block;
}

Thanks. 谢谢。

You wont be able to do so with just CSS in your situation. 在您所处的环境中,仅使用CSS就无法做到这一点。

#subTopicNav1:hover + #SubTopicInfo1 {}

Works if #SubTopicInfi1 is next to (after #subTopicInfo1 closing tag) the container. 如果#SubTopicInfi1在容器旁边(在#subTopicInfo1关闭标记之后),则可以使用。

explained here: 在这里解释:

How to affect other elements when a div is hovered 当div悬停时如何影响其他元素

So you probably should look into a solution with Jquery/javascript! 因此,您可能应该考虑使用Jquery / javascript解决方案!

Please try something like this: 请尝试这样的事情:

(Hide the extra info panel by default, then make it visible on :hover like below) (默认情况下,隐藏额外的信息面板,然后使其在:hover上可见,如下所示)

<style>
.link {
        display: block;
}

.link:hover p{
        visibility: visible;
}
.info{
        visibility: hidden;
}

</style>

<body>
        <div class="link">
                <a href="#"> Text Here </a>
                <p class="info"> Info Here</p>
        </div>
</body>

Here is the working Demo http://jsfiddle.net/H9fGP/1/ maybe its not the exact example. 这是可以正常工作的演示http://jsfiddle.net/H9fGP/1/ but I hope it helps :) 但我希望它会有所帮助:)

the main thing is that the sub info has absolute position 最主要的是,子信息具有绝对位置

I rearanged the html and added some css. 我重新整理了html并添加了一些CSS。

.container
{
 border-top: 5px solid #FFD640;

 margin-top: 25px;
}

#SubTopicNav 
{
  position:relative;
}
#SubTopicNav > div > div
{
  position:absolute;
  left:200px;
  top:0px;
  display:none;
  width: 600px;
  height: 400px;
  margin-top: 25px;
  float: left;
}
#SubTopicInfo
{
  display:none;
  background-color: #FFD640;
}

#SubTopicNav > div:hover >div
{
 display: block;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM