0
votes

I'm trying to use jQuery to reveal a hidden div by hovering over an another div. The hidden div should keep occupying its space always. This works fine when using visibility, like this:

$('.frase-1').mouseenter(function() {
    $('.field-1').css('visibility', 'visible');
});
$('.frase-1').mouseleave(function() {
    $('.field-1').css('visibility', 'hidden');
});

But I would like to use a fadeIn/fadeOut-effect, like this:

$('.frase-2').mouseenter(function() {
  $('.field-2').fadeIn(500);
});
$('.frase-2').mouseleave(function() {
  $('.field-2').fadeOut(500);
});

The problem with the latter solution is that the hidden div will be set to display:none, and this will alter the layout and flow of the page. I found an answer that suggested solving this by using animate({transparency}), like this:

$('.frase-3').mouseenter(function() {
  $('.field-3').animate({opacity:1});
});
$('.frase-3').mouseleave(function() {
  $('.field-3').animate({opacity:0});
});

This works only half: the hidden div is visible (where it shouldn't) when the page is loaded, and only disappears for the first time when hovered.

fadeTo is also suggested a number of times, but that seems to have cross-platform problems, so I didn't use it.

I've put all three solutions in this fiddle - is there anyone with a suggestion how to solve this?

Thanks!

2
Edited fiddle with final solutionRaoul Unger

2 Answers

0
votes

In solution 3, set the opacity to 0 within the CSS so that the element is hidden on load. Example: https://jsfiddle.net/ke0ympv7/82/

0
votes

Use your third solution with below CSS

.field-3{
  opacity:0;
}