I created a slider that fades images automatically but you can also click to see the next or previous image. It already works how it should. But I think my code is really heavy. I am not experienced with JavaScript / jQuery so it would be nice if someone could help me to tidy up my code.
// helper to change the slider background image
var changeImage = function(id, image){
$(id).css('background-image', 'url('+image+')');
};
// auto change slider elements over time
$(window).load(function() {
var images = [ 'http://mysite.com/image1',
'http://mysite.com/image2',
'http://mysite.com/image3',
'http://mysite.com/image4',
'http://mysite.com/image5',
'http://mysite.com/image6'
];
var texts = [
'<h1>Text 1</h1>',
'<h1>Text 2</h1>',
'<h1>Text 3</h1>',
'<h1>Text 4</h1>',
'<h1>Text 5</h1>',
'<h1>Text 6</h1>'
];
var hrefs = [ 'http://mysite.com/link1',
'http://mysite.com/link2',
'http://mysite.com/link3',
'http://mysite.com/link4',
'http://mysite.com/link5',
'http://mysite.com/link6'
];
var srcs = [ 'http://mysite.com/image1',
'http://mysite.com/image2',
'http://mysite.com/image3',
'http://mysite.com/image4',
'http://mysite.com/image5',
'http://mysite.com/image6'
];
// counter variables for images, texts and links
var i = 0;
var t = 0;
var h = 0;
var s = 0;
var x = 0;
var inter = "";
var again = "";
var hrefChange = document.getElementById('theHref');
// register next slide click
document.getElementById("clickablen").onclick = function() {
clearInterval(inter);
changeBackgroundNext();
}
// register previous slide click
document.getElementById("clickablep").onclick = function() {
clearInterval(inter);
changeBackgroundPrev();
}
// Init sequence, loading the first image
$("#wrapper_bottom").css("opacity", 0);
changeImage('#wrapper_bottom', images[i]);
changeBackground();
// slider functionality - general function
function changeBackground() {
if ( i >= 6 ) { i = 0; }
if ( t >= 6 ) { t = 0; }
if ( h >= 6 ) { h = 0; }
if ( s >= 6 ) { s = 0; }
x = 0;
$("#wrapper_bottom").css("opacity", 0);
changeImage('#wrapper_bottom', images[i]);
document.getElementById("myspan").innerHTML=texts[t];
$('#wrapper_bottom').animate({"opacity": 1}, 1000, function(){
hrefChange.setAttribute('href', hrefs[h]);
changeImage('#wrapper_top', images[i], 1);
if (++i >= images.length) { i = 0; x = 1}
$("#wrapper_bottom").css("opacity", 0);
changeImage('#wrapper_bottom', images[i]);
if (++t >= texts.length) { t = 0; }
if (++h >= hrefs.length) { h = 0; }
if (++s >= srcs.length) { s = 0; }
});
// fade content background image
if (srcs[s]) {
$('#thesrc').fadeOut(300, function(){
$(this).attr('src',srcs[s]).bind('onreadystatechange load', function(){
if (this.complete) $(this).fadeIn(300);
});
});
}
}
// repeat this function every 10 seconds
inter = setInterval(changeBackground, 10000);
// slider functionality - click next function
function changeBackgroundNext() {
clearInterval(again);
again = setInterval(changeBackground, 10000);
if ( i == 6 ) { i = 0; }
if ( t == 6 ) { t = 0; }
if ( h == 6 ) { h = 0; }
if ( s == 6 ) { s = 0; }
x = 0;
document.getElementById("myspan").innerHTML=texts[t];
$('#wrapper_bottom').animate({"opacity": 1}, 0, function(){
hrefChange.setAttribute('href', hrefs[h]);
changeImage('#wrapper_top', images[i], 1);
$("#wrapper_bottom").css("opacity", 0);
if (++i >= images.length) { i = 0; x = 1;}
changeImage('#wrapper_bottom', images[i]);
if (++t >= texts.length) { t = 0; }
if (++h >= hrefs.length) { h = 0; }
if (srcs[s]) {
$('#thesrc').fadeOut(0, function(){
$(this).attr('src',srcs[s]).bind('onreadystatechange load', function(){
if (this.complete) $(this).fadeIn(0);
});
});
if (++s >= srcs.length) { s = 0; }
}
});
}
// slider functionality - click previous function
function changeBackgroundPrev() {
clearInterval(again);
again = setInterval(changeBackground, 10000);
i=i-2;
t=t-2;
h=t-2;
s=s-2;
if ( i <= -1 ) { i = 5; }
if ( t <= -1 ) { t = 5; }
if ( h <= -1 ) { h = 5; }
if ( s <= -1 ) { s = 5; }
if (x == 1) { i = 4; t = 4; h = 4; s = 4; }
document.getElementById("myspan").innerHTML=texts[t];
$('#wrapper_bottom').animate({"opacity": 1}, 0, function(){
hrefChange.setAttribute('href', hrefs[h]);
changeImage('#wrapper_top', images[i], 1);
$("#wrapper_bottom").css("opacity", 0);
changeImage('#wrapper_bottom', images[i]);
if (srcs[s]) {
$('#thesrc').fadeOut(0, function(){
$(this).attr('src',srcs[s]).bind('onreadystatechange load', function(){
if (this.complete) $(this).fadeIn(0);
});
});
}
x = 0;
++i;
++t;
++h;
++s;
});
}
});