[英]jQuery .hover() performance doubts
While I know some jQuery to simplify front-end manipulation programming, I also aware of some of some "best practices" to level up the performance on browser, like caching through var
and always descend from ID selectors. 虽然我知道一些jQuery可以简化前端操作编程,但我也知道一些“最佳实践”来提高浏览器的性能,例如通过var
缓存,并且总是从ID选择器派生。 But I have doubts about this piece of code. 但是我对这段代码有疑问。 Could it be done better? 可以做得更好吗?
Preamble: This code animates several blocks inside the (already cached) selector through .hover(). 序言:此代码通过.hover()动画化(已缓存的)选择器中的几个块。
The HTML is something like this: HTML是这样的:
<div clas="block-link red">(...)</div>
<div clas="block-link yellow">(...)</div>
<div clas="block-link magenta">(...)</div>
<div clas="block-link moradopelusa">(...)</div>
And the jQuery to animate at hovering inside one of them: jQuery可以将鼠标悬停在其中之一上:
var cached_blocks = jQuery(.block-link);
jQuery(cached_blocks).hover(function(){
var this_block = jQuery(this),
this_block_text = jQuery(this_block).children(div.text),
this_block_image = jQuery(this_block).children(div.image),
this_block_link = jQuery(this_block).children(div.link),
this_block_link_icon = jQuery(this_block_link).children(a.icon);
/* Animate in; show everything */
jQuery(this_block_text).animate(..);
jQuery(this_block_image).animate(..);
jQuery(this_block_link).animate(..);
jQuery(this_block_link_icon).animate(..);
}, function() {
var this_block = jQuery(this),
this_block_text = jQuery(this_block).children(div.text),
this_block_image = jQuery(this_block).children(div.image),
this_block_link = jQuery(this_block).children(div.link),
this_block_link_icon = jQuery(this_block_link).children(a.icon);
/* Animate out; put everything where they belong */
jQuery(this_block_text).animate(..);
jQuery(this_block_image).animate(..);
jQuery(this_block_link).animate(..);
jQuery(this_block_link_icon).animate(..);
});
I'm seeing that I am declaring variables again after hover, but I don't know any technique to not doing again. 我看到我在悬停后再次声明了变量,但是我不知道有什么技术可以避免再次执行。 Anyway, even that way it works like a charm. 无论如何,即使那样,它也像魅力一样起作用。
Update: Fixed Code #1 更新:固定代码1
var cached_blocks = jQuery(.block-link);
jQuery(cached_blocks).on('mouseenter mouseleave', function(){
var this_block = jQuery(this),
this_block_text = jQuery(this_block).children(div.text),
this_block_image = jQuery(this_block).children(div.image),
this_block_link = jQuery(this_block).children(div.link),
this_block_link_icon = jQuery(this_block_link).children(a.icon);
/* Animate in; show everything */
this_block_text.animate(e.eventType(...));
this_block_image.animate(e.eventType(...));
this_block_link.animate(e.eventType(...));
this_block_link_icon.animate(e.eventType(...));
});
Note 1: Yes, i'm using cached_blocks in other part of the code. 注意1:是的,我在代码的其他部分中使用了cached_blocks。
You seem to be repeating code for no good reason? 您似乎无缘无故重复代码?
var cached_blocks = jQuery('.block-link');
cached_blocks.on('mouseenter mouseleave', function(e) {
var this_block = jQuery(this),
this_block_text = this_block.children('div.text'),
this_block_image = this_block.children('div.image'),
this_block_link = this_block.children('div.link'),
this_block_link_icon = this_block.children('a.icon');
this_block_text.animate({something: (e.type==='mouseenter' ? 0 : 400)});
this_block_image.animate({top: (e.type==='mouseenter' ? 10 : 200)});
this_block_link.animate({left: (e.type==='mouseenter' ? 300 : 40)});
this_block_link_icon.animate({right: (e.type==='mouseenter' ? 0 : 1400)});
});
Unless you are using the variables containing the text/image/link... elements somewhere else aswell, caching them just to use with animate() on the next line is a waste of space. 除非您还在其他地方使用包含text / image / link ...元素的变量,否则将它们缓存为仅与下一行的animate()一起使用会浪费空间。
I think you can bind mouseout
and mouseover
with the same callback closure ($element.on("mouseout mouseover", callback))
, and detect which event is inside the closure with something like e.type == "mouseout"
. 我认为您可以将mouseout
和mouseover
与相同的回调闭包($element.on("mouseout mouseover", callback))
绑定,并使用e.type == "mouseout"
类的东西来检测闭包内部有哪个事件。
In your code you are doing 在您的代码中,您正在做
this_block_text = jQuery(this_block).children(div.text)
and then 接着
jQuery(this_block_text).animate(..);
why not this? 为什么不呢?
this_block_text.animate(..)
this_block_text
is already a jQuery object this_block_text
已经是一个jQuery对象
jQuery(this_block_text).animate(..);
jQuery(this_block_image).animate(..);
jQuery(this_block_link).animate(..);
jQuery(this_block_link_icon).animate(..);
there is no need to write them as jQuery(...) because this_block_text, this_block_image etc. are already jQuery objects. 不需要将它们编写为jQuery(...),因为this_block_text,this_block_image等已经是jQuery对象。
this_block_text.animate()
will run. 会跑。
Also using $.() instead of jQuery() is better and cleaner. 另外,使用$。()代替jQuery()更好更清洁。
And $('div.block-link') is faster than $('.block-link') 并且$('div.block-link')比$('。block-link')更快
The thing which stands out the most in this code is how often you are calling jQuery
. 在此代码中最突出的是您调用jQuery
频率。 If you already have a jQuery object, there is no need to call jQuery again. 如果您已经有一个jQuery对象,则无需再次调用jQuery。
This: 这个:
var cached_blocks = jQuery(".block-link");
jQuery(cached_blocks).hover(...);
does the same thing as: 与以下功能相同:
var cached_blocks = jQuery(".block-link");
cached_blocks.hover(...);
If you're not going to reuse cached_blocks
there's not going to be any performance improvement from having it, so you could simplify the code to just: 如果您不打算重用cached_blocks
,那么使用它不会带来任何性能改进,因此您可以将代码简化为:
jQuery(".block-link").hover(...);
If the elements are already on the page, I'd simplify the code down to: 如果元素已经在页面上,我将代码简化为:
var links = jQuery(".block-link");
var parts = {
text: links.children("div.text"),
image: links.children("div.image"),
link: links.children("div.link"),
icon: links.children("a.icon")
};
links.hover(function() {
parts.text.animate(..);
parts.image.animate(..);
parts.link.animate(..);
parts.icon.animate(..);
}, function() {
parts.text.animate(..);
parts.image.animate(..);
parts.link.animate(..);
parts.icon.animate(..);
});
Without knowing what sort of animations you want to do, I can't simplify it any more, but I suspect you won't need so many calls to animate either. 在不知道要制作哪种动画的情况下,我无法再对其进行简化,但是我怀疑您也不需要那么多的调用来制作动画。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.