2

要素にカーソルを合わせると、その周りに4つの矢印が追加され、アニメーションがループして前後に移動するコードを作成しました。

これは、要素がドラッグ可能であることを視覚的に示すためです。プラグインを使用してhoverIntent、ユーザーが要素にカーソルを合わせようとしている場合にのみ矢印を描画しています。要素の配置にもjQueryUIを使用しています。

ドキュメントにアイテムを追加してからアニメーション化することは、これまでに行ったことのないことであり、これをうまくコーディングしていないため、パフォーマンスが最適ではなくなっていると確信しています。

このコードのパフォーマンスを向上させるにはどうすればよいですか?

jQuery.fx.interval = 1;

// Add Hover arrows
$('.table-row.songnamecolumn').livequery(function($songRow) {
    var $songRow = $(this).closest('tr').children('td');
    var $appenditem = $(this);
    var $this = $(this);

    var loop = function loop(){
        $('#toparrow').animate({ top:'-=15px'},500).animate({ top:'+=15px'},100);
        $('#bottomarrow').animate({ top:'+=15px'},500).animate({ top:'-=15px'},100);
        $('#leftarrow').animate({ left:'-=15px'},500).animate({ left:'+=15px'},100);
        $('#rightarrow').animate({ left:'+=15px'},500).animate({ left:'-=15px'},100);
    }

    $(this).hoverIntent({
        sensitivity: 3, // How sensitive the hoverIntent should be
        interval: 200, // How often in milliseconds the onmouseover should be checked
        over: slidedrag, // Function to call when mouseover is called    
        timeout: 200, // How often in milliseconds the onmouseout should be checked
        out: slidedragback // Function to call when mouseout is called    
    }); 

    function slidedrag() {  
        $('<div id="toparrow"></div>'
        +'<div id="leftarrow"></div>'
        +'<div id="rightarrow"></div>'
        +'<div id="bottomarrow"></div>').appendTo($appenditem);

        $('#toparrow').position ({
            of: $this,
            my: 'center top',
            at: 'center top',
            offset: "0 -18"
        })

        $('#bottomarrow').position ({
            of: $(this),
            my: 'center bottom',
            at: 'center bottom',
            offset: "0 18"
        })

        $('#leftarrow').position ({
            of: $(this),
            my: 'left center',
            at: 'left center',
            offset: "-10 0"
        })

        $('#rightarrow').position ({
            of: $(this),
            my: 'right center',
            at: 'right center',
            offset: "10 0"
        })

        $('#toparrow, #bottomarrow, #leftarrow, #rightarrow').animate({opacity:'0.5'},600);
        setInterval(loop, 600);
    }

    function slidedragback() {
        clearInterval(loop);
        $('#toparrow, #bottomarrow, #leftarrow, #rightarrow').animate({opacity:'0'},600);
        $('#toparrow,#bottomarrow,#leftarrow,#rightarrow').remove();
    }
});
4

1 に答える 1

0

同じ要素を複数回選択しているようです。それらをローカル変数として保存し、変数を呼び出すことをお勧めします。

var arrow_top = $('#toparrow');

コード ブロック内の余白を削除します。

out: slidedragback // Function to call when mouseout is called    
}); 



function slidedrag() {

最後に、アニメーションをやり過ぎないようにします。

于 2011-11-18T16:26:16.860 に答える