Jquery Mouseenter/Mouse Leave image swap

2019-06-06 07:55发布

问题:

So I wrote this little bit of code to try out some new way of doing an image swap for purposes of preloading and I am having a bit of trouble.

My problem is that I have a container with the images that has some padding and text, but the activation of my rollover only happens when someone rolls over the image, instead of the container. I must have some small bit wrong, hopefully someone can help me out. Still learning!

Here is the html:

<div class="projectThumb">
    <img src="/img/aeffect_button_static.gif" width="146" height="199" class="static" name="aeffect" alt="" />        
    <img src="/img/aeffect_button_rollover.jpg" width="146" height="199" class="rollover" name="aeffect" alt="" />
    <p class="title">A.EFFECT: Film Poster</p>
</div>

Here is the jquery:

$(document).ready(function(){
    $(".rollover").hide();
$(".projectThumb").mouseenter(
        function(){
            $(this).attr(".static").hide();
        }, 
        function(){
            $(this).attr(".rollover").show();
        });
$(".projectThumb").mouseleave(
        function(){
            $(this).attr(".rollover").hide();
        },
        function(){
            $(this).attr(".static").show();
        });
});

回答1:

I think you're looking for hover:

$(document).ready(function(){
    $(".rollover").hide();
    $(".projectThumb").hover(
    function(){
        $(this).find(".static,.rollover").toggle();
    }, 
    function(){
        $(this).find(".static,.rollover").toggle();
    });
});

Both mouseenter and mouseleave only take one argument, but you're defining two callback functions.



回答2:

Why not try:

$(".projectThumb").bind("mouseenter mouseleave", function(e) {
    $(this).toggle();
});