简体   繁体   English

我如何优化我的jQuery以获得最佳性能?

[英]How can I optimize my jQuery for maximum performance?

I was wondering whether somebody could provide me with an optimized version of my code, for I realized it tends to run extremely slowly in ie6 under Windows. 我想知道是否有人可以为我提供代码的优化版本,因为我意识到它在Windows下的ie6中运行速度非常慢。 I think I read somewhere that using .bind() for the click event in my code would help, but I am not sure how to go about it... 我想我读过某个地方,在我的代码中对点击事件使用.bind()会有所帮助,但我不确定如何去做...

Here is the relevant javascript : 这是相关的javascript:

        var buttonToGlow = $(".buttonGlow");
    buttonToGlow.effect("pulsate", {}, 850);

    $(".buttonGlow").hover(function(){
        $(this).filter(':not(:animated)').effect("pulsate", {times:1}, 350);
    });

    $(".buttonGlow").each(function(){
        var currentTitle = $(this).attr("title");
        $(this).click(function() {
            TINY.box.show({html:''+ currentTitle +''});
        });
    });

And here is the link to the test page I've put together. 这是我放在一起的测试页链接

Thanks for any help ! 谢谢你的帮助 !

That looks pretty good to me. 这对我来说看起来不错。

I guess if you want to make it look fast in IE6 you could change the jQuery effect to a animated GIF, or you could disable the effect entirely. 我猜想如果要使其在IE6中快速显示,可以将jQuery效果更改为动画GIF,或者可以完全禁用该效果。 IMO there's nothing wrong with a slightly visually degraded effect for older browsers. IMO对于较旧的浏览器来说,视觉效果略有下降是没有问题的。

I'd be willing to bet that most IE6 users are starting to experience widespread problems on the web due to their browser versions at this point. 我愿意打赌,由于此时的浏览器版本,大多数IE6用户开始在网络上遇到普遍的问题。 Personally, I do not take IE6 into consideration anymore when developing new websites, but this may not be an option for you. 就个人而言,在开发新网站时我不再考虑IE6,但这可能不是您的选择。 :( :(

Let's minimize jQuery calls, first :) 首先,让我们最小化jQuery调用:)
Also, you are making a different "click" handler for each glowing button. 同样,您为每个发光按钮都设置了不同的“单击”处理程序。 You can make only one for all of them - like this: 您只能为所有人制作一个-像这样:

var buttonToGlow = $(".buttonGlow");
buttonToGlow.effect("pulsate", {}, 850);

buttonToGlow.hover(function(){
    $(this).filter(':not(:animated)').effect("pulsate", {times:1}, 350);
});

buttonToGlow.click(function() {
    TINY.box.show({html:''+ $(this).attr('title') +''});
});

Also, the :not(:animated) call has to be slow in IE6. 此外,:不是(:动画)调用必须是在IE6缓慢。 Let's change it to something simpler? 让我们将其更改为更简单的内容?

Here's a more concise version: 这是更简洁的版本:

var pulse = function(){
    $(this).stop().effect("pulsate", {times:1}, 350);
};
var display_title = function() {
    TINY.box.show({html: $(this).attr('title')});
};
$(".buttonGlow").effect("pulsate", {}, 850).hover(pulse).click(display_title);

The performance under IE6 probably has something to do with its ability to handle the animation. IE6下的性能可能与其处理动画的能力有关。 However, there are some changes you can make to your code. 但是,您可以对代码进行一些更改。 Instead of attaching an event listener to each .buttonGlow , you can delegate it from the parent element: 您可以从父元素委托它,而不是将事件侦听器附加到每个.buttonGlow

$('.canvas').delegate('.buttonGlow', 'click', function() {
  TINY.box.show({ html: $(this).attr('title') });
});

This might result in a marginal performance improvement, and will make it easier to dynamically insert nodes if you need to. 这可能会导致边际性能提高,并且在需要时可以更轻松地动态插入节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM