Miskate
I'll point a miskate in the HTML.
The second li
and onwards, the p
element should be closed first and then. But, in your markup a
is first closed and then p
which is semantically wrong.
Here's the formatted HTML for second li
.
<li id="two">
<a href="#">
<img src="bradsfamilyborder.png" class="first" />
<p>
<img src="bradsfamilyname.png" class="name"/>
</a>
</p> <!--- Did I closed this right? --->
</li>
It should be
<li id="two">
<a href="#"><img src="bradsfamilyborder.png" class="first" />
<p><img src="bradsfamilyname.png" class="name" /></a>
</p>
</li>
Review
The IDs an class names are very generic. Chances are that they will clash with other page/developers styles and will not work as expected. When working with jQuery and CSS, it's very important to name the things properly. You can namespace them with some prefix - nav-
in this case.
The class names name
and first
doesn't tell anything about what it contains or direct wrong message. family-name
and family-border-name
is much better.
$('.name').hide()
will hide the elements on page load causing flicker. I'll suggest...(see Improvements section below)
stop()
is used to stop ongoing animation. As no animation is used(or the code is not shown?), it can be removed.
Improvement
Here is the Demo of jfriend00's answer. Although, this is correct, this can be done without jQuery at all.
Note: Here, I'll use old markup provided in the question.
$('.name').hide();
can be written in CSS as
.name {
display: none;
}
This has better effect as compared to jQuery counterpart as it will not show flicker when CSS is included in <head>
.
And the hover()
can be written as below using :hover
pseudo-class and Adjacent Sibling Selector
.first:hover + p > .name {
display: block;
}
which basically says, when hover on .first
, select next sibling p
and it's direct children >
, having name
class.
Demo:Although, you cannot see images, you can see it works as expected when hover over an image.
#nav .family-border-name {
display: none;
}
#nav .family-name:hover + p > .family-border-name {
display: block;
}
<div id="nav">
<ul>
<li>
<a href="#"><img src="mikesfamilyborder.png" class="family-name" />
<p><img src="mikesfamilybordername.png" class="family-border-name" /></p>
</a>
</li>
<li>
<a href="#"><img src="bradsfamilyborder.png" class="family-name" />
<p><img src="bradsfamilyname.png" class="family-border-name" /></p>
</a>
</li>
<li>
<a href="#"><img src="brennerfamilyborder.png" class="family-name" />
<p><img src="ryanbfamilyname.png" class="family-border-name" /></p>
</a>
</li>
<li>
<a href="#"><img src="ryanfamilyborder.png" class="family-name" />
<p><img src="ryanlfamilyname.png" class="family-border-name" />
</p>
</a>
</li>
<li>
<a href="#"><img src="missydadfamilyborder.png" class="family-name" />
<p><img src="lackeysname.png" class="family-border-name" />
</p>
</a>
</li>
<li>
<a href="#"><img src="libbyfamilyborder.png" class="family-name" />
<p><img src="libbysname.png" class="family-border-name" />
</p>
</a>
</li>
</ul>
<p> Click Picture for Family page</p>
</div>