所以我的页面上有一个bootstrap轮播,我正在尝试根据当前显示的幻灯片来更改页面内容。我在javascript变量中有当前的幻灯片索引,但无法将值传递给php。所以我现在用GET获取值,但php变量的值不会改变,除非我刷新页面,但是当我刷新页面时幻灯片重置为第一个。 即使页面刷新后,我怎么能让旋转木马停留在同一张幻灯片上?谢谢
这是我的旋转木马:
<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>
这是我的剧本:
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'));
});
}
});
提前谢谢你!
答案 0 :(得分:2)
您可以将当前幻灯片编号存储为本地变量。
localStorage.setItem('slide', activeSlideNumber)
然后,当文档加载时,检查存储中是否存在幻灯片变量,如下所示。
$(function() {
var slide = localStorage.getItem('slide');
if (slide === null) {
// start from the top
} else {
// start from slide number in storage
}
});
答案 1 :(得分:0)
$('#myCarousel').on('slid.bs.carousel', function () {
var currentSlide = $('#myCarousel div.active').index();
sessionStorage.setItem('lastSlide', currentSlide);
});
if(sessionStorage.lastSlide){
$("#myCarousel").carousel(sessionStorage.lastSlide*1);
}
这对我来说非常合适!