Edward
Edward

Reputation: 3091

jQuery mouseenter simple effect

I'm trying to fade a <div class="front"> outand a <div class="back"> in on hover, and when users hovers out I need to revert , fade out back and fade in front, however my <div class="back"> does not fadeout on mouseleave.

Sorry the the newbie question but i'm not sure why this code isn't working. It is because I need to trigger the mouseenter on the back event when the front fades out?

http://jsfiddle.net/8xo8c5pn/

HTML

$(".front").mouseenter(function() {
    $(this).removeClass('on').addClass('off');
    $(this).next().removeClass('off').addClass('on');
});

$(".back").mouseleave(function() {
    $(this).removeClass('on').addClass('off');
    $(this).prev().removeClass('off').addClass('on');
});
.on{
    display:inherit;
}
.off{
    display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<figure>
  <div class="front">
   <img src="http://vignette3.wikia.nocookie.net/jadensadventures/images/5/54/Pokemon-Ash-s-Pikachu-Riley-Sir-Aaron-s-Lucarios-pokemon-guys-10262907-563-579.jpg/revision/latest?cb=20120902022940">
    <div class="cover">
       <div class="">Hello</div>
     </div>
 </div>
 <div class="back off">
<div class="back box-style-1 off-color-bg">
 <i class="fa fa-leaf"></i><p></p>
 <h2>im the back</h2>
<p>Some text</p>
 </div>
</div>

 </figure>

Upvotes: 0

Views: 48

Answers (3)

Azad
Azad

Reputation: 5272

$(document).ready(function() {
  $(".back").hide(); //hide back initially
  $(".front").mouseenter(function() {
    $(".back").show();
    $(this).hide();
  });

  $(".back").mouseleave(function() {
    $(".front").show();
    $(this).hide();
  });

});
.back,
.front {
  border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<figure>
  <div class="front">
    <img src="http://vignette3.wikia.nocookie.net/jadensadventures/images/5/54/Pokemon-Ash-s-Pikachu-Riley-Sir-Aaron-s-Lucarios-pokemon-guys-10262907-563-579.jpg/revision/latest?cb=20120902022940">
    <div class="cover">
      <div class="">Hello</div>
    </div>
  </div>
  <div class="back off">
    <div class="back box-style-1 off-color-bg">
      <i class="fa fa-leaf"></i>
      <p></p>
      <h2>im the back</h2>
      <p>Some text</p>
    </div>
  </div>

Upvotes: 1

AldoZumaran
AldoZumaran

Reputation: 572

try this, Use hover function instead mouseenter and leave:

<div class"album">
    <div class"front">
    </div>
    <div class"cover" style="display:none;">
    </div>
</div>

$( ".album" ).hover(
function() {  // IN
    $(this).find('.front').hide();
    $(this).find('.cover').show();
},function() { // OUT
    $(this).find('.front').show();
    $(this).find('.cover').hide();
});

Upvotes: 0

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

you have 2 element with class back so use .back.off instead of just .back

try this

$( ".front" ).mouseenter(function() {
    $(this).removeClass('on').addClass('off');
    $(this).next('.back.off').removeClass('off').addClass('on');
});

$( ".back.off" ).mouseleave(function() {
    $(this).removeClass('on').addClass('off');
    $(this).prev('.front').removeClass('off').addClass('on');
});

DEMO

Upvotes: 1

Related Questions