views:

20

answers:

3

I have several .box div including short text (<p>) and a thumbnail. I'm trying to change opacity of text within a .box div when any part of the .box is hovered, but I don't want the image affected. So far I've got this:

jQuery(document).ready(function(){
$(".box").fadeTo("fast", 0.6);

$(".box").hover(function(){
$(this).fadeTo("fast", 1.0); 
},function(){
$(this).fadeTo("fast", 0.6);
});
});

I know it must be quite simple, but I'm stuck. Thanks for help.

+1  A: 

User $(".box p") selector

fantactuka
It's very simple and elegant, unfortunately it only works when you hover over the p area.
ntlk
A: 

Use .find() to get just the <p> inside to apply fade to, like this:

jQuery(function(){
  $(".box p").fadeTo("fast", 0.6);

  $(".box").hover(function(){
    $(this).find("p").fadeTo("fast", 1.0); 
  },function(){
    $(this).find("p").fadeTo("fast", 0.6);
  });
});

This uses $(".box p") to only fade the <p> elements initially, then in the hover (for the entire box) we're finding the same <p> elements inside. If you did $(".box p").hover(...) only hovering on the <p> itself would work.

Nick Craver
Brilliant! That works perfectly. It's exactly what I've been looking for. Thank you!
ntlk
A: 

You could probably place the thumbnail image outside the box you're trying to fade or use a more precise selector like $('.box p'). Depends if you want to have the box decoration itself fade, or just the text.

this might also work:

<div class="container">
    <img id="thumbnail" src="" style="float: left; /* or position abosolute"/>
    <div id="box">
        <p>text</p>
    </div>
</div>
<script type="text/javascript">
     $(document).ready(function() {
         // $('.box p').fadeIn('fast'); 
         // or:
         $('.box').fadeIn('fast');
     });
</script>
thomasmalt