views:

120

answers:

1

problem can be seen here: http://www.studioimbrue.com/beta

The code:

$(document).ready(function(){
    $('div.caption').hide();
    $('.captions ul li img').hover(function(){
        $(this).siblings('div.caption').fadeIn('medium');
    }, function(){
        $(this).siblings('div.caption').fadeOut('medium');
    });
});

Not sure what's causing the problem... Everything seems to be set up correctly.

A: 

The problem is that when the caption appears, your mouse is no longer on the image - a mouseleave event is sent to the image and a mouseenter to the caption div. The former triggers the fadeout. You can solve that by placing both the image and the caption into a container element (e.g. a <div>) and applying the event handler on this container. Then no matter whether the caption is showing or not, the outer container will not receive a mouseleave.

EDIT: Here's a working example:

HTML:

<div class="captions" id="talktostrangers"> 
  <ul>
    <li> 
      <img src="image1.jpg"> 
      <div class="caption">Caption 1</div>
    </li>
    <li>
      <img src="image2.jpg"> 
      <div class="caption">Caption 2</div>
    </li>
  </ul>
</div>

Javascript:

$('.captions li').hover(function() {
  $('.caption', this).fadeIn();
}, function() {
  $('.caption', this).fadeOut();
});
Max Shawabkeh
I tried this a few ways before by instead of using ".captions ul li img" I reduced it to just ".captions ul li" and ".captions ul" but neither worked. I tried wrapping both the whole <li> itself and the img/caption div just now and referred to it as .mouse, but now nothing comes up. Check at http://www.studioimbrue.com/beta
steve
It seems you changed the html but not the Javascript (`siblings` is not appropriate anymore). I tried running `$('.mouse').mouseenter(function() {alert('Hello');});` on your page, and it works fine, alerting on mouse entry into the image.
Max Shawabkeh
I'm so confused now! I removed siblings and still nothing is happening... the code is here (and at http://www.studioimbrue.com/beta):$(document).ready(function(){ $('.caption').hide(); $('.captions ul li').hover(function(){ $(this)('.caption').fadeIn('medium'); }, function(){ $(this)('.caption').fadeOut('medium'); });});
steve
Edited in a full example.
Max Shawabkeh
Excellent! You rock.
steve