简体   繁体   English

jQuery:将每个函数转换为for循环

[英]jQuery: Converting each function to for loop

I recently had received the help of you great people on stackoverflow to help me move a background image the appropriate distance on a mouseover event. 最近,我在stackoverflow上得到了大家的帮助,以帮助我在mouseover事件上将背景图像移动适当的距离。 This works great, but the problem is that I'm concerned how optimal it is using the each function. 这很好用,但是问题是我担心使用每个函数的最佳程度。

I'm hoping I can get a brief explanation on how to convert this particular code to a for loop as I'm interested in the optimization benefits, but I'm not quite understanding the difference in syntax for how to convert it to a for loop. 我希望可以对如何将特定代码转换为for循环进行简短的解释,因为我对优化的好处很感兴趣,但是我不太了解如何将其转换为for的语法差异。环。

var xPosition = -195;
$('div.style-swatches ul li').each(function(){
    $(this).mouseenter(function(){
        $(this).closest('div.chip-style').find('div.chip-preview').css("background-position", (xPosition - ($(this).index() * 195)) + "px 0");
    });
});

Simply don't use the .each() 只是不要使用.each()

Demo fiddle 演示小提琴

$("div.style-swatches li").mouseenter(function() {
     $(this).closest('div.chip-style').find('div.chip-preview').css("background-position", (xPosition - ($(this).index() * 195)) + "px 0");
});

Never optimize without benchmarks. 没有基准就永远不要优化。 First profile your code, collect real data, see what function call really takes up a lot of time/memory and then optimize the discovered performance bottlenecks. 首先分析您的代码,收集实际数据,查看哪个函数调用确实占用大量时间/内存, 然后优化发现的性能瓶颈。

In your particular case, I'd expect the DOM queries to take a few orders of magnitude more time than the loop construct. 在您的特殊情况下,我希望DOM查询比循环结构花费更多的时间。 You could think about simplifying your CSS queries (eg changing div.style-swatches ul li to .style-swatches li if appropriate), use the native DOM instead of wrapping everything in a jQuery object,... 您可以考虑简化CSS查询(例如,将div.style-swatches ul li更改为div.style-swatches ul li .style-swatches li ),使用本机DOM而不是将所有内容包装在jQuery对象中,...

As some other answers already pointed out, you don't actually need the loop at all, as .mouseenter() already does that (and uses a .each() loop internally). 正如其他答案已经指出的那样,您实际上根本不需要循环,因为.mouseenter()已经做到了(并在内部使用.each()循环)。

Any selection uses a for loop under the covers. 任何选择都使用for循环。 Basically, any time you see $(".css-selector") think, "For all matching elements". 基本上,任何时候您看到$(“。css-selector”)都认为“对于所有匹配的元素”。 In your code the .each(...) just makes the process more explicit. 在您的代码中,.each(...)仅使过程更明确。

If you really wanted to break this out into a for loop, you could use your selector and then index the elements directly, a la: 如果您确实想将其分解为for循环,则可以使用选择器,然后直接为元素建立索引,例如:

var elems = $('div.style-swatches ul li');
for (var i = 0; i < elems.length; ++i){
  $(elems[i]).mouseenter(function(){
        $(this).closest('div.chip-style').find('div.chip-preview').css("background-position", (xPosition - ($(this).index() * 195)) + "px 0");
    });

}

But again, since jQuery already does this itself, it's doubtful you'll see any beneficial performance impact. 但是,由于jQuery本身已经执行了此操作,因此您是否会看到任何有益的性能影响,都值得怀疑。

The jquery selector returns an array. jQuery选择器返回一个数组。 You can just iterate on it 您可以对其进行迭代

var xPosition = -195;
var elements = $('div.style-swatches ul li');
for(var i = 0; i < elements.length; i++) {
    $(elements[i]).mouseenter(function() {
        $(this).closest('div.chip-style').find('div.chip-preview').css("background-position", (xPosition - ($(this).index() * 195)) + "px 0");
    });
}

$('div.style-swatches ul li') returns and array of items ..just loop it $('div.style-swatches ul li')返回和项数组..just循环它

var xPosition = -195;
var items     = $('div.style-swatches ul li');

for (var i = 0, l = items.length; i < l; i++) {
    $(items[i]).mouseenter(function(){
        $(items[i]).closest('div.chip-style').find('div.chip-preview').css("background-position", (xPosition - ($(items[i]).index() * 195)) + "px 0");
    });
}

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

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