开发者

Newbie jQuery question: Need slideshow to rotate automatically, not just when clicking navigation

This is my first post, so please forgive me if this question has been asked a mill开发者_如何学编程ion 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="&nbsp;" src="image1.jpg" />
                </div>
                <div class="mystuff">
                    <img alt="&nbsp;" src="image2.jpg" />
                </div>
                <div class="mystuff">
                    <img alt="&nbsp;" src="image3.jpg" />           
                </div>
                <div class="mystuff">
                    <img alt="&nbsp;" 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>


You've got two problems to solve here:

  1. Create a function that can determine the next frame.
  2. Call that function on a given interval.

Split out the code that displayes the current frame as its own function, as in showFrame().

Then make a function that determines which frame to call next, as in getNextFrame().

Finally, add buttons that call setInterval() and clearInterval() as needed, as in toggleSlideShow().

But once you get this working, make another attempt at Cycle or another such plugin.

    <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'));
        });
        //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 > 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);
            }
     }
    </script>

Here is the source for the start/stop buttons I added. You can put this where you like--I inserted it after the button div.

    <div id="controlButton">
        <a class="buttonStart" rel="Start" href="#">Start</a>
        <a class="buttonStop" rel="Stop" href="#">Stop</a>
    </div>

I also defined .buttonStart,.buttonStop in the CSS file.


Justin, check out the section on setInterval() on this page.

Basically, if you can isolate your code to cycle the pictures into a simple function call, you would just do:

setInterval("myFunction()", 500)

which would call your function every 500ms (.5 seconds). Now, things get a little trickier if you want to be able to stop the interval running. In that case, you need to get the "hInterval" (the interval handle) which is returned from setInterval. Then you can use that in a call to clearInterval()

So that would be done like this:

// set your interval and capture the handle
var hInt = setInterval("myFunction()", 500);

< blah blah blah > 

// clear the interval so it doesn't fire again
clearInterval(hInt);


It's not a solution, but if you really want to just add-on to the existing function, you'll most likely use setInterval or setTimeout to call a modified version of your existing function every x seconds.

And while this is certainly possible (I'm not expert enough to write this off the top of my head), it's something that's built right in to the jQuery Cycle plugin you mentioned. Which could be quite easily implemented into your existing code (I believe).

Sorry I can't offer better advice at the moment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜