Jquery Mouseenter/Mouse Leave image swap

时光总嘲笑我的痴心妄想 提交于 2020-01-14 03:18:13

问题


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


来源:https://stackoverflow.com/questions/1396715/jquery-mouseenter-mouse-leave-image-swap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!