Jquery, mouseenter向下移动图像

jquery, mouseenter to move down image

本文关键字:移动 图像 mouseenter Jquery      更新时间:2023-09-26

我是JS新手,我正在努力提高我的JS技能,到目前为止一切都很顺利,直到现在,我必须在导航栏中移动img,当鼠标进入时向下移动,当它离开时向上移动。https://www.youtube.com/embed/8qKRf0cg3Co

我已经尝试了不同的方法来完成它,但我现在卡住了。现在,当我移动img时,它会一直往下移,当我离开时,它不会回到原来的位置。

$(document).ready(function (){ $('.foto').mouseenter(function () { $(this).animate({marginTop: '+=50'}, 200); }); $('.foto').mouseleave(function (){ $(this).animate({marginBottom: '-=50'}, 200); });

您需要在mouseleave中使用marginTop而不是marginBottom

$(document).ready(function (){
    $('.foto').mouseenter(function () {
        $(this).animate({marginTop: '+=50'}, 200);
    });
    $('.foto').mouseleave(function (){
        $(this).animate({marginTop: '-=50'}, 200);
        //               --^-- change here
    });
});