I have just finished my new portfolio site visible at http://www.pepkarsten.com/artdirection.
It is a single page that loads images (with loader animation, preloading and keyboard shortcuts).
Here is the JavaScript code (using jQuery). How can it be optimized?
$(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);