Note that there are some explanatory texts on larger screens.

plurals
  1. PONewbie jQuery question: Need slideshow to rotate automatically, not just when clicking navigation
    primarykey
    data
    text
    <p>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. </p> <p>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. </p> <p>Anywho, here's the html:</p> <pre><code> &lt;div id="slideshow-container"&gt; &lt;div id="myslide"&gt; &lt;div class="cover"&gt; &lt;div class="mystuff"&gt; &lt;img alt="&amp;nbsp;" src="image1.jpg" /&gt; &lt;/div&gt; &lt;div class="mystuff"&gt; &lt;img alt="&amp;nbsp;" src="image2.jpg" /&gt; &lt;/div&gt; &lt;div class="mystuff"&gt; &lt;img alt="&amp;nbsp;" src="image3.jpg" /&gt; &lt;/div&gt; &lt;div class="mystuff"&gt; &lt;img alt="&amp;nbsp;" src="image4.jpg" /&gt; &lt;/div&gt; &lt;/div&gt; &lt;!-- end of div cover --&gt; &lt;/div&gt; &lt;!-- end of div myslide --&gt; &lt;div id="button"&gt; &lt;a class="button1 active" rel="1" href="#"&gt;1&lt;/a&gt; &lt;a class="button2" rel="2" href="#"&gt;2&lt;/a&gt; &lt;a class="button3" rel="3" href="#"&gt;3&lt;/a&gt; &lt;a class="button4" rel="4" href="#"&gt;4&lt;/a&gt; &lt;/div&gt; &lt;!-- end of div button--&gt; &lt;/div&gt;&lt;!-- end of slideshow-container --&gt; </code></pre> <p>And here's the jQuery:</p> <pre><code> &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/JavaScript"&gt; $(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')} }); }); }); &lt;/script&gt; </code></pre> <p>Here's where I got the script: <a href="http://www.webdeveloperjuice.com/2010/04/07/create-lightweight-jquery-fade-manual-slideshow" rel="nofollow noreferrer">http://www.webdeveloperjuice.com/2010/04/07/create-lightweight-jquery-fade-manual-slideshow/</a></p> <p>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!</p> <p><strong>Edit:</strong> 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.</p> <pre><code> &lt;script type="text/JavaScript"&gt; $(document).ready(function(){ var integer = $(this).attr('rel'); $('#myslide .cover').css({left:-820*(parseInt(integer)-1)}).hide().fadeIn(); /*----- Width of div #mystuff (here 820) ------ */ }); &lt;/script&gt; </code></pre> <p>Obviously this doesn't include setInterval() but that's because, frankly, I'm clueless about the syntax and application of setInterval(). </p> <p><strong>Solution provided by nate and adapted slightly:</strong></p> <pre><code> &lt;script&gt; // 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 &gt; totalFrames) currentFrame = 1; showFrame(currentFrame); } &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload