Search code examples
jquerycycle2

JQuery/Cycle2 - Go to URL and then call specific slide


I am trying to link to a specific slide from another page.

The code i am using (within document.ready) is:

$('#goto1').click(function() { 
    window.location.href = "page.html#projects";
    setTimeout(function () {
    $('.cycle-slideshow').cycle(3); 
    return false;
    }, 5000);   
});

It goes to the URL in question, but then doesnt change the cycle-slideshow div to slide 3 - in fact nothing happens once redirected.

Obviously I am missing something - any ideas?


Solution

  • Used cycle2 built in functionality.

    ie.

    var slideshow = $('.cycle-slideshow');
    maxSlides = slideshow.data('cycle.opts');
    
    slideshow.on({
    'cycle-update-view': function(event, optionHash, outgoingSlideEl, incomingSlideEl, forwardFlag) {
        UpdateTitles();
    }
    });
    
    function UpdateTitles(){
    var currentSlide = slideshow.data('cycle.opts').currSlide;
    activeSlide = $(".cycle-slide-active");
    activeTitle = activeSlide.data('title');
    
    if(currentSlide == 0){
        nextTitle = activeSlide.next().data('title');
        prevTitle = '';
    }
    else if(currentSlide == maxSlides-1){
        prevTitle = activeSlide.prev().data('title');
        nextTitle = $(".cycle-slide").eq(0).data('title');
    }
    else {
        nextTitle = activeSlide.next().data('title');
        prevTitle = activeSlide.prev().data('title');
    }
    
    $('#prev').html(prevTitle);
    $('#next').html(nextTitle);
    }
    
    UpdateTitles();
    
    });