代码之家  ›  专栏  ›  技术社区  ›  Rich Bradshaw

编写回调函数

  •  7
  • Rich Bradshaw  · 技术社区  · 14 年前

    我使用jQuery编写如下内容:

        $('#current_image').fadeOut(function(){
            $('#current_image').attr('src',newImage).show();
        });
    

    这是可爱的,一旦 fadeOut

    我想在这里建立我自己的函数 淡出

        $('#current_image').customFadeOut(function(){
            $('#current_image').attr('src',newImage).show();
        });
    
    1 回复  |  直到 5 年前
        1
  •  3
  •   meder omuraliev    14 年前

    关键是将函数引用传递给您定义的原型方法,并将传递的函数绑定到 $.animate 或者任何你在里面使用的jquery动画函数。

    HTML格式:

    <div id="blah" style="color:#fff; opacity:1; background:black; height:50px; position:relative; ">fsdljfdsjfds</div>
    <a id="click" href="#">fjsd</a>
    

    $.fn.customFadeOut = function (callback) {
        return $(this).animate({
            opacity: 0.25,
            fontSize: "3em",
            height: 'toggle'
        }, 5000, function () {
            callback.apply(this)
        });
    
    }
    
    $('#click').click(function () {
        $('#blah').customFadeOut(function () {
            alert('hi')
    
        })
    
    })​
    

    Live Demo