[英]JavaScript/jQuery optimization
我刚刚在http://www.pepkarsten.com/artdirection上看到了我的新投资组合网站。
它是一个加载图像的页面(带有加载器动画,预加载和键盘快捷键)。
这是JavaScript代码(使用jQuery)。 如何优化?
$(document).ready(function() {
function page(slide,width,height,color) {
this.slide=slide;
this.width=width;
this.height=height;
this.color=color;
};
var pages=[
new page('alutech1',900,675,'1d486b'),
new page('alutech2',900,675,'00ea00'),
new page('mane3',675,900,'74878e'),
new page('mane4',675,900,'74878e'),
new page('mane1',900,675,'6ecb00'),
new page('topfit_zen',900,675,'203400'),
new page('topfit_muscu',900,675,'01acb3'),
new page('topfit_stval',900,675,'525962'),
new page('arles_1',636,900,'fb926d'),
new page('arles_2',636,900,'c12f2f'),
new page('arles_3',636,900,'cdc6b4'),
new page('topsol',900,633,'f7e700'),
new page('wak',900,675,'78f900')
];
var imgDir='img/';
var slidePrefix='pepkarsten_';
var slideExt='.jpg';
$.autoMouseOver();
$.blurLinks();
function prevPageNumber() {
return currentPage>0?currentPage-1:pages.length-1;
};
function nextPageNumber() {
return currentPage<pages.length-1?currentPage+1:0;
};
function displayPage(n) {
$('#nav-top')
.css('background-color','#'+pages[n].color);
$('#slide')
.addClass('loading')
.find('img')
.css('visibility','hidden')
.css('width',pages[n].width)
.css('height',pages[n].height)
.unbind('load')
.load(function() {
$(this)
.css('visibility','visible');
$('#slide')
.removeClass('loading');
$.preloadImg(imgDir+slidePrefix+pages[nextPageNumber()].slide+slideExt,imgDir+slidePrefix+pages[prevPageNumber()].slide+slideExt);
})
.attr('src',imgDir+slidePrefix+pages[n].slide+slideExt);
currentPage=n;
};
function homePage() {
displayPage(0);
};
function nextPage() {
displayPage(nextPageNumber());
};
function prevPage() {
displayPage(prevPageNumber());
};
homePage();
$('#home')
.onclick(homePage)
.shortcut('up');
$('#next')
.onclick(nextPage)
.shortcut('right');
$('#prev')
.onclick(prevPage)
.shortcut('left');
$('#slide')
.onclick(nextPage);
$('#contact')
.email('info','pepkarsten.com')
.hover(
function() {$('#tip-contact').slideDown(200)},
function() {$('#tip-contact').stop(true,true).hide()});
$('#linkedin')
.onclick(function() {
window.open('http://www.linkedin.com/in/pepkarsten');
})
.hover(
function() {$('#tip-linkedin').slideDown(200)},
function() {$('#tip-linkedin').stop(true,true).hide()});
});
(function($){
var imgCache=new Array();
$.preloadImg=function() {
for(var i=0; i<arguments.length; i++) {
var img=new Image();
img.src=arguments[i];
imgCache[img.src]=img;
}
};
$.autoMouseOver=function(outStr,overStr) {
if(!overStr) var outStr='-out.', overStr='-over.';
$('img[src*='+ outStr +']')
.each(function() {$.preloadImg($(this).attr("src").replace(outStr,overStr))})
.hover(
function() {$(this).attr("src",$(this).attr("src").replace(outStr,overStr))},
function() {$(this).attr("src",$(this).attr("src").replace(overStr,outStr))});
};
$.blurLinks=function() {
$("a").focusin(function() {
this.blur();
});
};
$.fn.onclick=function(f) {
$(this).click(function() {
f();
return false;
});
return this;
};
$.address=function(u,d) {
return u+'@'+d;
};
$.fn.email=function(u,d,s,b) {
var l='mailto:'+$.address(u,d);
if(s||b) {
l+='?';
if(s) {
l+='subject='+s;
if(b) l+='&';
};
if(b) l+='body='+b;
};
$(this).click(function() {
window.open(l);
return false;
});
return this;
};
$.fn.shortcut=function(key) {
var code={'left':37,'up':38,'right':39,'down':40};
var $this=$(this);
$(document).keydown(function(e) {
if(e.keyCode==code[key]) {
$this.click();
return false;
};
});
window.focus();
return this;
};
})(jQuery);
实际答案:无需优化,您可以顺利工作预加载,最大限度地减少连接速度慢的影响。 代码中任何速度缺陷的影响可以忽略不计,网络优化正确完成,这在这种情况下很重要。
理论答案:如果你真的担心性能然后不使用jQuery,你只是没有制作真正优化的JavaScript所需的低级控制,你通常最终会有很多混淆的开销,因为看起来很简单jQuery函数实际上可能具有复杂的实现,因此花费了大量时间。
为了记录,我会说你是我见过的第一个可以编码的设计师。 当然还有其他人可以将一些命令粘在一起,但看起来你真的知道自己在做什么。 这条道路的建议,因为我认为你是可以管理它的类型:无论人们告诉你什么,质疑它,试着寻找相反的证据,并在必要时做你自己的研究。
编辑:关于jQuery与JavaScript
正如我所看到的,jQuery的最大优势在于它修复了很多浏览器差异,因此您不必担心代码是否适用于所有浏览器。 jQuery也做了很多“魔术”,它可以使编码变得更容易,但魔术通常需要花费大量的速度,在很大程度上取决于你使用哪些功能,以及你如何使用它们。 您可以轻松地在脚本执行上投入10倍,但大多数代码仍然受到DOM操作的限制,而jQuery不会减慢速度。
我不是jQuery的语法的风扇,它是非常符合使用卷积关闭一切,并使用关键字的当前趋势, this
尽可能地。 这当然不是说如果你使用jQuery就必须编写这样的不可读代码,但很难不去那个方向。 如果你写一些大的东西我会更喜欢JavaScript,因为可读性是一个更大的问题。
只有少数小事跳出来。 总的来说,Chrome看起来很流畅。
在我谈到这些之前,我建议您对代码进行评论 。 一年后,当你回去尝试做出改变时,它会让你更轻松! 此外,您可能想要考虑以一种合理的方式排序您的功能(例如,按字母顺序排列,或者您能想到的任何其他有意义的东西......它们似乎没有我能说出的任何顺序)
我注意到的一件事是你尝试通过仅使用var $this = $(this);
为每个实例创建一次$(this)
jQuery对象来优化代码var $this = $(this);
。 即使$(this)
仅使用一次也可以使用它,这很好但不必要,但你忘了在少数情况下这样做。
例如,你有:
.....hover(function() {
$(this).attr("src",$(this).attr("src").replace(outStr,overStr))},
function() {
$(this).attr("src",$(this).attr("src").replace(overStr,outStr))});
这可能是:
.....hover(function() {
var $this = $(this);
$this.attr("src",$this.attr("src").replace(outStr,overStr))},
function() {
var $this = $(this);
$this.attr("src",$this.attr("src").replace(overStr,outStr))});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.