Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm using the Bootstrap Carousel as header of a website in PHP, is there any way to keep the memory of the slide reached when I change the page of my website? So in the next page it would start with the next slide and not from the start again.

Feel free to ask me for detailed parts, I have just put the standard carousel code in each page at the top (including one PHP file).

Update1: Thank you for the answer, I've created a session in order to save which slide the carousel should start with. Now the question is: how I can get the current slide number? (because the carousel cycles after x seconds and I need to get the last slide showed).

Solved: @Andrea Ajek thank you very much for the answer! It solved my question also without using PHP sessions!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
242 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use the HTML5 WebStorage to save the last slide index. Assuming that your carousel has an id attribute equal to "myCarousel", this code should work flawlessly:

$('#myCarousel').on('slid.bs.carousel', function () {
    var currentSlide = $('#myCarousel div.active').index();
    sessionStorage.setItem('lastSlide', currentSlide);
});

The event 'slid.bs.carousel' occurs when the carousel has finished sliding from one item to another. To check if there is a slide index stored in sessionStorage and initialize correctly the carousel, you can use the following code:

if(sessionStorage.lastSlide){
      $("#myCarousel").carousel(sessionStorage.lastSlide*1);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...