Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You've got two problems to solve here:</p> <ol> <li>Create a function that can determine the next frame.</li> <li>Call that function on a given interval.</li> </ol> <p>Split out the code that displayes the current frame as its own function, as in <code>showFrame()</code>.</p> <p>Then make a function that determines which frame to call next, as in <code>getNextFrame()</code>.</p> <p>Finally, add buttons that call <code>setInterval()</code> and <code>clearInterval()</code> as needed, as in <code>toggleSlideShow()</code>. </p> <p>But once you get this working, make another attempt at Cycle or another such plugin. </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')); }); //handle the start/stop clicks $('#controlButton a').click(function(){toggleSlideShow($(this).attr('rel')); }); }); // 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); } // Start and stop the slide show. // The important part here are the setInterval and // clearInterval functions. function toggleSlideShow(state){ // using the same technique for the start // and stop buttons $('#controlButton a').each(function(){ $(this).removeClass('active'); if($(this).hasClass('button'+state)){ $(this).addClass('active')} }); if(state == "Start") { //Store the timeoutId for later... timeoutId = setInterval("getNextFrame()",1000); // time in milliseconds } else { //...so it can be used to stop the show. clearInterval(timeoutId); } } &lt;/script&gt; </code></pre> <p>Here is the source for the start/stop buttons I added. You can put this where you like--I inserted it after the <code>button</code> div.</p> <pre><code> &lt;div id="controlButton"&gt; &lt;a class="buttonStart" rel="Start" href="#"&gt;Start&lt;/a&gt; &lt;a class="buttonStop" rel="Stop" href="#"&gt;Stop&lt;/a&gt; &lt;/div&gt; </code></pre> <p>I also defined <code>.buttonStart,.buttonStop</code> in the CSS file.</p>
 

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