views:

31

answers:

3

Hi,

I have the following code that automates a slideshow between the different slides as shown in the picture.

This is now producing a fadeIn whenever I click on the numbers shown on the pic, but I'd like to automate the transition betweeh 1 to 5 and when on 5 return to 1 again on a timed maner.

The screenshot is:

alt text

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/JavaScript">
$(document).ready(function (){
    $('#button a').click(function(){
        var integer = $(this).attr('rel');
        $('#myslide .cover').css({left:-960*(parseInt(integer)-1)}).hide().fadeIn(); 
        $('#button a').each(function(){
        $(this).removeClass('active');
            if($(this).hasClass('button'+integer)){
                $(this).addClass('active')}
        });
    });    
});
</script> 
+2  A: 

The following assumes you will remove the number buttons, and that your slides are numbered 1 to 5.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/JavaScript">
$(document).ready(function (){
    function showSlide(integer) {
        $('#myslide .cover').css({left:-960*(integer-1)}).hide().fadeIn(); 
        $('#button a').each(function(){
        $(this).removeClass('active');
            if($(this).hasClass('button'+integer)){
                $(this).addClass('active')}
        });
        setTimeout(function() {showSlide((integer % 5) + 1);}, 5000);
    }
    showSlide(1);
});
</script> 
sje397
Thanks sje397, this solution works perfectly except that invalidates the use of the buttons. Whenever I click on the buttons the clicks are ignored... How could I make both workeable?
Cy.
@Cy - save the return value from the `setTimeout` call - you can pass this to `clearTimeout` to stop the timer. Add a click handler that 1) stops the sequence as described, and 2) calls showSlide(n) with the appropriate value of n, to begin the sequance again at the chosen index
sje397
Amazing @sje397 this is perfect and neat solution!
Cy.
A: 

Hmm try something along the lines of

$(document).ready(function (){
    Slider = {
        current : 1;
        Init : function(after_user_click)
        {
            tm = after_user_click ? 6000 : 3000;
            setTimeout(function(){
                Slider.Change();
                Slider.Init(); //Restart the init process.
            },tm)
        },
        Change : function()
        {
            if(this.current == 5)
            {
                 $("#button a[rel='1']").click(); //Click the first
            }else
            {
                 $("#button a[rel='"+(this.current+1)+"']").click(); //Click the first
            }
        }
    }

    $('#button a').click(function(){
        var integer = $(this).attr('rel');
        $('#myslide .cover').css({left:-960*(parseInt(integer)-1)}).hide().fadeIn();
        Slider.Init(true);
        $('#button a').each(function(){
        $(this).removeClass('active');
            if($(this).hasClass('button'+integer)){
                $(this).addClass('active')}
        });
    });

    //Start the slider.
    Slider.Init();
});

Note: This is untested and of the top of my head!

RobertPitt
A: 

Solution by sje397 will work - here's alternative that will use callback function after fade-in

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/JavaScript">
$(document).ready(function (){
    function showSlide(integer) {
        $('#myslide .cover').css({left:-960*(integer-1)}).hide().fadeIn(function() {
            showSlide((integer % 5) + 1);
        }); 
        $('#button a').removeClass('active');
        $('#button a.button' + integer).addClass('active');
    }
    showSlide(1);
});
</script>
VinayC