最近,我在 stackoverflow 上得到了各位大神的帮助,帮助我在鼠标悬停事件中将背景图像移动到适当的距离。这很好用,但问题是我担心使用每个函数的最佳效果。
我希望能得到有关如何将此特定代码转换为 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");
});
});
最佳答案
不要使用 .each()
$("div.style-swatches li").mouseenter(function() {
$(this).closest('div.chip-style').find('div.chip-preview').css("background-position", (xPosition - ($(this).index() * 195)) + "px 0");
});
关于javascript - jQuery:将每个函数转换为 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19695144/