我正在尝试停止fadeTo()动画。我有4张图片,其中3张(未悬停)应该变暗,并且悬停的透明度为1。不幸的是,到目前为止,我编写的代码都会使每张图片,包括在此“亮起之前悬停一转” ”。相反,我只希望它一开始保持在100%的状态,因为其他所有因素都会使网站显得无响应。

到目前为止,我有:

$("#submenu").load("submenu.html", function(){
    function darken(){;
    $("#submenu li").find("img").fadeTo( 100, 0.20  );
    }

    function brighten(){
$("#submenu li").find("img").fadeTo( 100, 1.00  );
    }

    $('#submenu li').each(function(index) {

            $(this).hover(
                  function () {
                    darken();

            //       $(this).stop(1,0).fadeTo(100, 0.20);
// if I do this ^ this image stays dark, even when hovered, and stays black when unhovered

           //        $(this).stop(1,0).fadeTo(100, 0.20);
// this ^ has no effect on the image whatsoever and it stays dark if I remove the line below

                $(this).find("img").fadeTo( 100, 1.00  );
// keeping this ^ line, it shows the behavior as stated in the question
                  },
                  function () {
                    brighten ();
                  }
            );


    });


我在stop()调用中使用(true,false)语句弄乱了很多,但是到目前为止,任何组合都没有获得成功。

最佳答案

好的,我仍然对Web开发人员一无所知(我是一个应用程序开发人员),但是我现在要做的就是将元素传递给darken()方法,这样它就知道“不”要进行什么变暗。待清理后再发布代码。

$("#submenu").load("submenu.html", function(){

    function darken(topkek){

        $.each($('#submenu ul li'), function( index, value ) {
            if(topkek.innerHTML!=$(this).html()){
                $(this).find("img").stop(true).fadeTo( 100, 0.40  );
            }
// I think one couldn't have done this worse than here but if it look stupid and it work, it ain't stupid
        });
    }


    function brighten(){
        $("#submenu li").find("img").stop(true).fadeTo( 450, 1.00  );
    }

    $('#submenu li').each(function(index) {

            $(this).hover(
                  function () {
                    darken(this);
                  },
                  function () {
                    brighten ();
                  }
            );


    });

    });

10-05 21:45