简体   繁体   中英

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> 

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

 $(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> 

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();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM