0

後で関数を呼び出したときにアイテムがアニメーション化されるように、この動きをアニメーション化するにはどうすればよいですか? 私は通常使用します$(this).animateが、位置を計算しているため、これをラップする方法がわかりません。ありがとう。

jQuery.fn.center = function ()
{
    this.css("position","absolute");
    this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + 
        $(window).scrollTop()) + "px");
    this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + 
        $(window).scrollLeft()) + "px");
    return this;
}
4

2 に答える 2

0

アニメーション化するには、左/上の値で animate を使用する必要がありますが、最初に を使用して位置を絶対に設定しcss()ます。

        // can't easily animate change of css "position"
        // so we just set it directly
        elt.css('position', 'absolute');

        // animate the position
        elt.animate({
            left: x,
            top: y
        });

私はこれのためにフィドルを作成しました: http://jsfiddle.net/lingo/CkRUd/2/

フィドルでは、プラグインがより堅牢であることを確認するために、いくつかのことも整理しました。

于 2013-04-09T10:28:43.793 に答える