简体   繁体   中英

JavaScript and CSS: display a div over an element on mouseover

I want to display a div over an element (especially a link) when hovered, like in Facebook, when you hover a profile picture.

I know this could be done with JavaScript and CSS but have no exact idea.

Facebook's approach is to simply use CSS, which won't work in all browsers. In those browsers Facebook ditches the effect and always shows the element that should only display on hover:

#parent #child {
  display: none;
}
#parent:hover #child {
  display: block;
}

Use conditional CSS to set display: block as the default in IE7 and below.

This can actually be done with pure css, here is a simple example:

HTML:

<div id='outer'>
    <div id='button'>
        <!-- your element here -->
    </div>
    <div id='popup'>
        <!-- your popup menu here -->
    </div>
</div>

CSS:

#popup {
    visibility:hidden;
}

#outer:hover #popup {
    visibility:visible;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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