Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

php - How to NOT reset Bootstrap Carousel slides

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

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

1 Reply

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...