我怎样才能提高这个jquery动画代码的性能

发布于 2024-12-16 23:25:25 字数 2568 浏览 3 评论 0原文

我编写了一些代码,当我将鼠标悬停在某个元素上时,会在其周围添加四个箭头并循环播放动画,以便它们前后移动。

这是为了直观地表明该元素是可拖动的。我使用 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();
    }
});

I've written some code that when I hover over an element, adds four arrows around it and loops an animation so that they move back and forwards.

This is to visually show that the element is draggable. I am using the hoverIntent plugin to only draw the arrows when the user intends to hover over the element. I am also using jQueryUI for the element positioning.

Appending an item to the document and then animating it is something I have never done before and I am certain that I have coded this poorly causing the performance to be less than optimal.

How could I go about making this code perform better?

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();
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆で 2024-12-23 23:25:25

我发现您多次选择相同的元素。我建议您将它们存储为局部变量并调用该变量:

var arrow_top = $('#toparrow');

删除代码块中的空白:

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



function slidedrag() {

最后,避免过度动画。

I see you're selecting the same elements more than once. I suggest you to store them as local variable and recall the variable:

var arrow_top = $('#toparrow');

Remove over white space in you code block:

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



function slidedrag() {

Last, avoid doing over animations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文