在悬停在父元素上时,显示子元素(下拉导航) [英] During hover on parent element, show child (dropdown nav)
本文介绍了在悬停在父元素上时,显示子元素(下拉导航)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图做一个愚蠢的水平导航栏,下拉一些项目。我决定这样做只是把下拉在一个div标签。这是很容易改变,我只是不喜欢在html一边重。
I'm trying to make a stupid horizontal nav bar with a drop-down on some of the items. The way I decided to do it is just by putting the drop-down in a div tag. This is easily changeable, i just don't like to go heavy on the html side.
基本上,我只想让我的下拉工作,当你悬停在父元素。
Basically I just want my drop down to work when you hover over the parent element. Additional css is going to be used to make it pretty and positioned better.
这是我的js:
var dropdown = $('.dropdown');
var parent = dropdown.parent();
$(parent).hover(
function () {
dropdown.css('display', 'block');
}
);
这是我的css:
div.nav {
text-align: center;
}
div.nav > ul > li {
margin-top: 15px;
text-align: center;
font-size: 1.25em;
}
div.nav > ul > li {
display: inline-block;
list-style-type: none;
}
div.nav a {
padding: 1em;
}
div.dropdown {
display: none;
background-color: black;
position: absolute;
}
这是我的html:
<div class="nav">
<ul>
<li><a href="index.html">Home</a></li>
<li>
<a href="game.html">Sample Game</a>
<div class="dropdown">
<a href="index.html">About it</a>
<br>
<a href="index.html">Game</a>
</div>
</li>
<li><a href="solutions.html">TP Solutions</a></li>
<li><a href="projects.html">Projects</a></li>
<li><a href="contact.html">Contact Me</a></li>
</ul>
<div class="clear"></div>
推荐答案
您不应使用parent
You should not be using "parent" as a variable name, as it's a reserved word.
$(document).ready(function() {
var $dropdown = $('.dropdown'),
$parent = $dropdown.parent();
$parent.on("mouseover",
function () {
$dropdown.css('display', 'block');
}
);
$parent.on("mouseout",
function () {
$dropdown.css('display', 'none');
}
);
});
这篇关于在悬停在父元素上时,显示子元素(下拉导航)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文