How to NOT reset Bootstrap Carousel slides

被刻印的时光 ゝ 提交于 2020-01-07 09:02:09

问题


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!!


回答1:


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


来源:https://stackoverflow.com/questions/41368117/how-to-not-reset-bootstrap-carousel-slides

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!