问题
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