stizzle
stizzle

Reputation: 17

Simple automated slideshow in jQuery

i have a simple slideshow which is working okay so far. it consists of a pager and the images to display. both are an unordered list:

<ul id="keyvisualpager">
    <li><a><span>Show Keyvisual 1</span></a></li>
    <li><a><span>Show Keyvisual 2</span></a></li>
    <li><a><span>Show Keyvisual 3</span></a></li>               
</ul>

<ul id="keyvisualslides">
    <li><img src="index_files/mbr_keyvisual1.jpg" alt="Keyvisual" /></li>
    <li><img src="index_files/mbr_keyvisual2.jpg" alt="Keyvisual" /></li>
    <li><img src="index_files/mbr_keyvisual3.jpg" alt="Keyvisual" /></li>
</ul>

The according jQuery code is:

$('#keyvisualpager li a').click(function () { 

    // get position of a element
    var mbr_index = $(this).parent().prevAll().length;
    var mbr_targetkeyvisual = mbr_index + 1;

    // hide current image and show the target image
    $('#keyvisualslides li:visible').hide();        
    $('#keyvisualslides li:nth-child('+mbr_targetkeyvisual+')').show()

    // remove active class from current indicator and add the same class to target indicator
    $('#keyvisualpager li a').removeClass('keyvisualactive');
    $('#keyvisualpager li:nth-child('+mbr_targetkeyvisual+') a').addClass('keyvisualactive');

});

initially all images are set to display: none; ... upcon clicking a a link in #keyvisualpager the according image is then displayed. at the same time the indicator changes accordingly.

now, my problem:

apart from this mode of navigating through the images i need the whole slideshow to automatically advance. how can i achieve that:

a) the next image is shown after lets say 5 seconds and

b) the class ".keyvisualactive" is added to the according a element in #keyvisualpager

note: unfortunately i have to use jquery 1.2.1, updating is not an option.

thanks for your help guys

EDIT

i am now using this code. it almost works. but after the last image in the set is displayed: how can i tell it to start over with the first image? thanks!

var reload = setInterval(function(){
    // get position of a element
    var mbr_index = $('#keyvisualpager li .keyvisualactive').parent().prevAll().length;
    var mbr_targetkeyvisual = mbr_index + 2;
    // alert(mbr_index+'//'+mbr_targetkeyvisual)

    // hide current image and show the target image
    $('#keyvisualslides li:visible').hide();        
    $('#keyvisualslides li:nth-child('+mbr_targetkeyvisual+')').show();

    // remove active class from current indicator and add the same class to target indicator
    $('#keyvisualpager li a').removeClass('keyvisualactive');
    $('#keyvisualpager li:nth-child('+mbr_targetkeyvisual+') a').addClass('keyvisualactive');

}, 2000);

Upvotes: 1

Views: 903

Answers (2)

Manuputy
Manuputy

Reputation: 3

how to make this be automated to, or we can add navigation next and prev

$(function(){
  $('.slide-thumb .thumb').on("mouseenter",function(){
      var dataImage = $(this).data('image');
      $('.slide-jumbo .jumbo:visible').fadeOut(1000,function(){
         $('.slide-jumbo .jumbo[data-image="'+dataImage+'"]').fadeIn(500); 
      });
  });
});
    * { 
        font-family: Arial;
    }
    .slide-container {
        /*border: solid 1px;*/
        width: 500px;
        height: 360px;
    }
    .slide-jumbo {
        /*border: solid 1px;*/
        width: 500px;
        height: 300px;
        overflow: hidden;
    }
    .jumbo {
        position: relative;  
        display: inline-block;
        width: 500px;
        height: 300px;
        float: left;
    }
    .jumbo img, .thumb img {
        position: absolute;  
        left: 0;
    }
    .jumbo img {
        top: 0;
    }
    .thumb img {
        bottom: 0;
    }
    .jumbo-capt, .thumb-capt {
        width: 100%;
        background-color: rgba(0,0,0,0.8);  
        position: absolute;  
        color: #fff;  
        z-index: 100; 
      /*  -webkit-transition: all 300ms ease-out;  
        -moz-transition: all 300ms ease-out;  
        -o-transition: all 300ms ease-out;  
        -ms-transition: all 300ms ease-out;  
        transition: all 300ms ease-out;  */
        left: 0;  
        bottom: 0;
    }
    .slide-thumb {
        /*border: solid 1px;*/
        width: 500px;
        height: 60px;
        overflow: hidden;
    }
    .thumb {
        position: relative;  
        /*border: solid 1px;*/
        display: inline-block;
        width: 100px;
        height: 60px;
        overflow: hidden;
        float: left;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div class="slide-container">
    <div class="slide-jumbo">
        <div class="jumbo" data-image="1">
            <img src="images/2.png">
            <div class="jumbo-capt">
                <h3>Title 1</h3>
                <p>Summary 1</p>
            </div>
        </div>
        <div class="jumbo" data-image="2">
            <img src="images/3.png">
            <div class="jumbo-capt">
                <h3>Title 2</h3>
                <p>Summary 2</p>
            </div>
        </div>
        <div class="jumbo" data-image="3">
            <img src="images/9.png">
            <div class="jumbo-capt">
                <h3>Title 3</h3>
                <p>Summary 3</p>
            </div>
        </div>
        <div class="jumbo" data-image="4">
            <img src="images/7.png">
            <div class="jumbo-capt">
                <h3>Title 4</h3>
                <p>Summary 4</p>
            </div>
        </div>
        <div class="jumbo" data-image="5">
            <img src="images/9.png">
            <div class="jumbo-capt">
                <h3>Title 5</h3>
                <p>Summary 5</p>
            </div>
        </div>
    </div>
    <div class="slide-thumb">
        <div class="thumb" data-image="1">
            <img src="images/2small.png">
            <div class="thumb-capt">
                <p>
                    <strong>
                        Title 1
                    </strong>
                </p>
            </div>
        </div>
        <div class="thumb" data-image="2">
            <img src="images/3small.png">
            <div class="thumb-capt">
               <p>
                    <strong>
                        Title 2
                    </strong>
                </p>
            </div>
        </div>
        <div class="thumb" data-image="3">
            <img src="images/9small.png">
            <div class="thumb-capt">
                <p>
                    <strong>
                        Title 3
                    </strong>
                </p>
            </div>
        </div>
        <div class="thumb" data-image="4">
            <img src="images/7small.png">
            <div class="thumb-capt">
                <p>
                    <strong>
                        Title 4
                    </strong>
                </p>
            </div>
        </div>
        <div class="thumb" data-image="5">
            <img src="images/9small.png">
            <div class="thumb-capt">
                <p>
                    <strong>
                        Title 5
                    </strong>
                </p>
            </div>
        </div>
    </div>
</div>

Upvotes: 0

ooksanen
ooksanen

Reputation: 60

You can use JavaScript's setInterval() method to achieve this.

var reload = setInterval(function(){
            // do something
}, 5000);

The number is length of every pause in milliseconds.

To stop the slideshow, for example when a user selects an image, you can use clearInterval() method.

EDIT

Try something like this:

$('#keyvisualpager li a').click(function () {

    var reload = setInterval(function(){

        // get position of a element
        var mbr_index = $(this).parent().prevAll().length;
        var mbr_targetkeyvisual = mbr_index + 1;

        // hide current image and show the target image
        $('#keyvisualslides li:visible').hide();        
        $('#keyvisualslides li:nth-child('+mbr_targetkeyvisual+')').show()

        // remove active class from current indicator and add the same class to target indicator
        $('#keyvisualpager li a').removeClass('keyvisualactive');
        $('#keyvisualpager li:nth-child('+mbr_targetkeyvisual+') a').addClass('keyvisualactive');

    }, 5000);

    $('#pagerstop').click(function(){
        clearInterval(reload);
    }

});

EDIT 2

You have to keep track of image count and the index you are at (if I understood correctly you have this in your var mbr_targetkeyvisual?) so it should work like this (not tested):

// Image count
var content_length = $('#keyvisualslides').children().length;

var automate = setInterval(function(){
    if(index < content_length){
        $('#keyvisualslides li:visible').hide();        
        $('#keyvisualslides li:nth-child('+mbr_targetkeyvisual+')').show();
        mbr_targetkeyvisual++;
    }
    else{
        mbr_targetkeyvisual = 0;
        $('#keyvisualslides li:visible').hide();
        $('#keyvisualslides li:nth-child('+mbr_targetkeyvisual+')').show();
        mbr_targetkeyvisual++;
    }
}, 5000);

Upvotes: 1

Related Questions