so i have a bootstrap carousel on my page and i’m trying to change the content of the page depending on which slide in currently showed. i have the current slide index in a javascript variable but can’t transfer the value to php. so i am now getting the value with GET but the php variable’s value don’t change unless i refresh the page, but when i do refresh the page the slides reset to the first one. how can i let the carousel stay on the same slide even after the page is refreshed? thank you
this is my carousel:
<div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="false"> <ol class="carousel-indicators"> <li data-target="#myCarousel" data-slide-to="0" class="active"></li> <li data-target="#myCarousel" data-slide-to="1" ></li> <li data-target="#myCarousel" data-slide-to="2" ></li> <li data-target="#myCarousel" data-slide-to="3"></li> <li data-target="#myCarousel" data-slide-to="4"></li> <li data-target="#myCarousel" data-slide-to="5"></li> <li data-target="#myCarousel" data-slide-to="6"></li> <li data-target="#myCarousel" data-slide-to="7"></li> <li data-target="#myCarousel" data-slide-to="8"></li> <li data-target="#myCarousel" data-slide-to="9"></li> <li data-target="#myCarousel" data-slide-to="10"></li> </ol> <div class="carousel-inner"> <div class="item active"> <img src="images/wisp.png" alt="WISP" style="width:100%;"> </div> <div class="item"> <img src="images/linkme.png" alt="linkme" style="width:100%;"> </div> <div class="item"> <img src="images/zainent.png" alt="zain" style="width:100%;"> </div> <div class="item"> <img src="images/tahadi.png" alt="tahadi" style="width:100%;"> </div> <div class="item"> <img src="images/onelife.png" alt="onelife" style="width:100%;"> </div> <div class="item"> <img src="images/alavina.png" alt="alavina" style="width:100%;"> </div> <div class="item"> <img src="images/iha.png" alt="iha" style="width:100%;"> </div> <div class="item"> <img src="images/runnuts.png" alt="runnuts" style="width:100%;"> </div> <div class="item"> <img src="images/oliviafred.png" alt="oliviafred" style="width:100%;"> </div> <div class="item"> <img src="images/bandicoot.png" alt="bandicoot" style="width:100%;"> </div> <div class="item"> <img src="images/bassama.png" alt="bassama" style="width:100%;"> </div> </div> <a class="left carousel-control" href="#myCarousel" data-slide="prev" onclick="return chooseit()"> <span class="glyphicon glyphicon-chevron-left"></span> </a> <a class="right carousel-control" href="#myCarousel" data-slide="next" onclick="return chooseit()"> <span class="glyphicon glyphicon-chevron-right"></span> </a>
and this is my script:
function chooseit() { var numbit = $('div.active').index() + 1; history.pushState(null, null, '/work.php?id='+numbit); } $(function(){ var carousel = $('.carousel ul'); var carouselChild = carousel.find('li'); var clickCount = 0; itemWidth = carousel.find('li:first').width()+1; //Including margin //Set Carousel width so it won't wrap carousel.width(itemWidth*carouselChild.length); //Place the child elements to their original locations. refreshChildPosition(); //Set the event handlers for buttons. $('.btnNext').click(function(){ clickCount++; //Animate the slider to left as item width carousel.finish().animate({ left : '-='+itemWidth },300, function(){ //Find the first item and append it as the last item. lastItem = carousel.find('li:first'); lastItem.remove().appendTo(carousel); lastItem.css('left', ((carouselChild.length-1)*(itemWidth))+(clickCount*itemWidth)); }); }); $('.btnPrevious').click(function(){ clickCount--; //Find the first item and append it as the last item. lastItem = carousel.find('li:last'); lastItem.remove().prependTo(carousel); lastItem.css('left', itemWidth*clickCount); //Animate the slider to right as item width carousel.finish().animate({ left: '+='+itemWidth },300); }); function refreshChildPosition(){ carouselChild.each(function(){ $(this).css('left', itemWidth*carouselChild.index($(this))); }); } function refreshChildPositionNext(){ carouselChild.each(function(){ leftVal = parseInt($(this).css('left')); }); } });
thank you in advance !
Advertisement
Answer
$('#myCarousel').on('slid.bs.carousel', function () { var currentSlide = $('#myCarousel div.active').index(); sessionStorage.setItem('lastSlide', currentSlide); }); if(sessionStorage.lastSlide){ $("#myCarousel").carousel(sessionStorage.lastSlide*1); }
this worked perfectly for me !