我想提供这个通用模板解决方案,它扩展了易江提供的正确解决方案。
额外的好处包括:
- 支持悬停在任何元素类型或多个元素上;
- 弹出窗口可以是任何元素类型或元素集,包括对象;
- 自记录代码;
- 确保弹出窗口出现在其他元素之上;
- 如果您要从数据库生成 html 代码,则需要遵循的坚实基础。
在 html 中放置以下结构:
<div class="information_popup_container">
<div class="information">
<!-- The thing or things you want to hover over go here such as images, tables,
paragraphs, objects other divisions etc. -->
</div>
<div class="popup_information">
<!-- The thing or things you want to popup go here such as images, tables,
paragraphs, objects other divisions etc. -->
</div>
</div>
在 css 中放置以下结构:
div.information_popup_container {
position: absolute;
width:0px;
height:0px;
/* Position Information */
/* Appearance Information */
}
div.information_popup_container > div.information {
/* Position Information */
/* Appearance Information */
}
div.information_popup_container > div.popup_information {
position: fixed;
visibility: hidden;
/* Position Information */
/* Appearance Information */
}
div.information_popup_container > div.information:hover + div.popup_information {
visibility: visible;
z-index: 200;
}
需要注意的几点是:
- 由于 div.popup 的位置设置为固定(也适用于绝对),因此内容不在文档的正常流程中,这使得可见属性可以正常工作。
- z-index 设置为确保 div.popup 出现在其他页面元素之上。
- information_popup_container 设置为小尺寸,因此无法悬停。
- 此代码仅支持将鼠标悬停在 div.information 元素上。要支持将鼠标悬停在 div.information 和 div.popup 上,请参阅下面的悬停在弹出窗口上。
- 它已经过测试,在 Opera 12.16 Internet Explorer 10.0.9200、Firefox 18.0 和 Google Chrome 28.0.15 中按预期工作。
将鼠标悬停在弹出窗口上
作为附加信息。当弹出窗口包含您可能想要剪切和粘贴的信息或包含您可能想要与之交互的对象时,请先替换:
div.information_popup_container > div.information:hover + div.popup_information {
visibility: visible;
z-index: 200;
}
与
div.information_popup_container > div.information:hover + div.popup_information
,div.information_popup_container > div.popup_information:hover {
visibility: visible;
z-index: 200;
}
其次,调整 div.popup 的位置,使其与 div.information 重叠。几个像素足以确保 div.popup 在将光标移出 div.information 时可以接收悬停。
这在 Internet Explorer 10.0.9200 上无法正常工作,但在 Opera 12.16、Firefox 18.0 和 Google Chrome 28.0.15 上却可以正常工作。
有关弹出式多级菜单的完整示例,请参阅 fiddle http://jsfiddle.net/F68Le/。