// -- Body Backgroud Slider Code
// -- JD French: May 27, 2011 --
$(document).ready(function() {
// VARIABLES //
btnWidth = "84"; //width of button(s) in pixels
btnHeight = "84"; //height of buttons(s) in pixels
leftButton = "#arrow-left"; //jQuery selector of left button
rightButton = "#arrow-right"; //jQuery selector of right button
padding = "7"; //pixel value of button padding
// LIST SLIDES //
var slides = [];
slides[1] = "home";
slides[2] = "about";
slides[3] = "services";
// END VARIABLES //
intSlides = slides.length - 1; //how many slides there are
//Hide other slides
for (i = 0; i<slides.length; i++) {
if(i != 1) { //don't hide first slide
$("#" + slides[i]).hide();
}
}
//SetButtons function
function setButtons() {
var height = $(window).height();
var width = $(window).width();
var hpos = width-btnWidth-padding + "px";
var vpos = height/2-btnHeight + "px";
//Arrows
$(leftButton).css("top", vpos);
$(leftButton).css("left", padding + "px");
$(rightButton).css("top", vpos);
$(rightButton).css("left", hpos);
}
//Call SetButtons
setButtons();
//Define .resize to call SetButtons
$(window).resize(function() {
setButtons();
});
//Cycling
currentObj = 1; //holds which slide is being displayed
$(leftButton).click(function() {
//Go back
newObj = currentObj-1;
if (newObj<1) {
newObj = intSlides; //if you cycle back from slide 1, go to slide intSlides (max)
}
newBg = "images/" + slides[newObj] + ".jpg"; //background image url - images/the ID.jpg
//Animation
$("body").fadeOut(1000, function() {
$("body").css("background", "url(" + newBg + ") no-repeat center center fixed").ready(function() {
$("#" + slides[currentObj]).hide();
$("#" + slides[newObj]).show();
$("body").fadeIn(1000);
//Change the currentObj
currentObj = newObj;
//Reposition the arrows in case of scrollbars
setButtons();
});
});
});
$(rightButton).click(function() {
//Go back
newObj = currentObj+1;
if (newObj>intSlides) {
newObj = 1; //if you cycle forward from intSlides (max), go to 1st slide
}
newBg = "images/" + slides[newObj] + ".jpg"; //background image url - images/the ID.jpg
//Animation
$("body").fadeOut(1000, function() {
$("body").css("background", "url(" + newBg + ") no-repeat center center fixed").ready(function() {
$("#" + slides[currentObj]).hide();
$("#" + slides[newObj]).show();
$("body").fadeIn(1000);
//Change the currentObj
currentObj = newObj;
//Reposition the arrows in case of scrollbars
setButtons();
});
});
});
});
92 lines of pure awesome, in my opinion.
What it needs:
• Two right and left buttons
• Container (div, span, etc) with an ID corresponding to the slide name
• Background image (images/id.jpg) that has a name that corresponds to the ID name
What it does:
• Positions the buttons perfectly vertically centered and up against either side of the page horizontally and adds/subtracts the value of padding so the buttons are symmetrical
• When the window is resized calls the positioning function again
• Cycles the slides
• Shows the next or previous slide by hiding slides[i-1] or slides[i+1] and showing slides[i]
• Uses .css() to change the body background to images/#id#.jpg
• Comes ready for new slides to be added into the slides array
Design I used it with is here: http://forr.st/~YuF
What do you think?
Update: Demo