Home » Javascript » Selecting random image in array and set as 0 or start point jquery

Selecting random image in array and set as 0 or start point jquery

Posted by: admin July 11, 2018 Leave a comment

Questions:

I have an array of images (gallery) on a page, you click on one to append it to a modal and then click a next or previous button to cycle through the array, or close the modal and select a new one etc. The problem is it works fine when starting from the first image in the array and cycling through but I cannot work out how to select another image half way though for example and then cycle from that point forwards and backwards. I know there are plugin solutions but I am trying to learn it myself.

$(document).ready(function() {

    var images_source = $('.linked_page').map(function(){ return this.src; }).get();

    $(document).on("click",".linked_page", function(event) {

        page_index = $(".linked_page").index(this);
        var starting_page = $(this).map(function(){ return this.src; }).get();

        event.stopPropagation();

        $('#read_the_paper').css("display","block").appendTo("body");
        $(".modal_content_linked_page_full").attr("src",starting_page);
        $(".magnify_large").css({'background-image' : 'url("'+starting_page+'")','background-repeat': 'no-repeat'});

        $(document).on('click','.next', function(event) {
            index = (index==page_index.length-1)?0:(index+1);       
            $(".modal_content_linked_page_full").attr('src',images_source[index]);
        });

    });

});
Answers: