Hi everyone,
This is my first post, so please forgive me if this question has been asked a million times. I'm a self professed jQuery hack and I need a little guidance on taking this script I found and adapting it to my needs.
Anyway, what I'm making is an image slide show with navigation. The script I found does this, but does not automatically cycle through the images. I'm using jQuery 1.3.2 and would rather stick with that than using the newer library. I would also prefer to edit what is already here rather than start from scratch.
Anywho, here's the html:
<div id="slideshow-container">
<div id="myslide">
<div class="cover">
<div class="mystuff">
<img alt=" " src="image1.jpg" />
</div>
<div class="mystuff">
<img alt=" " src="image2.jpg" />
</div>
<div class="mystuff">
<img alt=" " src="image3.jpg" />
</div>
<div class="mystuff">
<img alt=" " src="image4.jpg" />
</div>
</div> <!-- end of div cover -->
</div> <!-- end of div myslide -->
<div id="button">
<a class="button1 active" rel="1" href="#">1</a>
<a class="button2" rel="2" href="#">2</a>
<a class="button3" rel="3" href="#">3</a>
<a class="button4" rel="4" href="#">4</a>
</div> <!-- end of div button-->
</div><!-- end of slideshow-container -->
And here's the jQuery:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/JavaScript">
$(document).ready(function (){
$('#button a').click(function(){
var integer = $(this).attr('rel');
$('#myslide .cover').css({left:-820*(parseInt(integer)-1)}).hide().fadeIn(); /*----- Width of div #mystuff (here 820) ------ */
$('#button a').each(function(){
$(this).removeClass('active');
if($(this).hasClass('button'+integer)){
$(this).addClass('active')}
});
});
});
</script>
Here's where I got the script: http://www.webdeveloperjuice.com/2010/04/07/create-lightweight-jquery-fade-manual-slideshow/
Again, if this question is too basic for this site please let me know and possibly provide a reference link or two. Thanks a ton!
Edit: I actually tried Matt's suggestion earlier but was unclear on the syntax of using setInterval with what I was working with. Here is the code that I was working with earlier, that isolates the image change. When this is loaded, the initial image loads with a fade animation but nothing else happens.
<script type="text/JavaScript">
$(document).ready(function(){
var integer = $(this).attr('rel');
$('#myslide .cover').css({left:-820*(parseInt(integer)-1)}).hide().fadeIn(); /*----- Width of div #mystuff (here 820) ------ */
});
</script>
Obviously this doesn't include setInterval() but that's because, frankly, I'm clueless about the syntax and application of setInterval().
Solution provided by nate and adapted slightly:
<script>
// Consider calculating these values instead of hard-coding them.
// The "currrentFrame" is the id with the "active" class,
// and the totalFrames could be counted with jQuery.
var currentFrame = 1;
var totalFrames = 4;
var timeoutId;
$(document).ready(function (){
// Handle the numbered button clicks
$('#button a').click(function(){showFrame($(this).attr('rel'));
});
//automatically handle slide rotation
$('document').ready(function(){getNextFrame($(this).attr('rel'));
setInterval("getNextFrame()",3000); // time in milliseconds
});
});
// I put the contents of the original click handler here
// so that it could be called by both the button and the timer.
function showFrame(integer){
$('#myslide .cover').css({left:-820*(parseInt(integer)-1)}).hide().fadeIn();
$('#button a').each(function(){
$(this).removeClass('active');
if($(this).hasClass('button'+integer)){
$(this).addClass('active')}
});
// storing the current frame globally
currentFrame = integer;
};
// determine the next frame using the
// currentFrame global. Again, a better way
// to do this could be to calculate the current
// frame with jquery...
function getNextFrame() {
currentFrame = parseInt(currentFrame);
currentFrame++;
if(currentFrame > totalFrames)
currentFrame = 1;
showFrame(currentFrame);
}
</script>