制作.animate功能

时间:2016-04-27 21:32:05

标签: jquery

尝试将jquery .animate变成一个函数,以便占用更少的空间并让我更容易阅读

function anim(object, values, end) { // Problem with the "end" part
    $(object).animate (values, {
        duration: 1000,
        quene: false,
        specialEasing: {top: "easeOutQuad"},
        complete: function() end // <-- here
    })
})

我尝试将其设为{end},但它会起作用。还尝试将结尾更改为其他名称,但仍无效。

1 个答案:

答案 0 :(得分:1)

您只需要在调用时传入代表complete事件的函数。因此,您的anim()函数应如下所示:

// The end parameter is simply a function that will be mapped to your completion event
function anim(object, values, end) { 
    $(object).animate (values, {
        duration: 1000,
        queue: false,
        specialEasing: {top: "easeOutQuad"},
        // This will map the completion event to your function that was
        // passed in
        complete: end 
    })
}

要实际调用该函数,您只需要传入选择器来定位元素,即一系列值

anim('#your-element', { ... }, function(){ alert('Done!'); });