最近,我在stackoverflow上得到了大家的帮助,以帮助我在mouseover事件上将背景图像移动适当的距离。这很好用,但是问题是我担心使用每个函数的最佳程度。
我希望可以对如何将特定代码转换为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");
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句