Bootstrap Carousel在Laravel Blade中激活第12个物品

时间:2019-01-25 14:27:44

标签: javascript jquery laravel twitter-bootstrap

我正在使用Bootstrap轮播显示月份和年份。我想显示两年内的月份和年份。从当前月份一年过去的一年和从当前月份一年以后的一年。

对于当前情况,即从2018年1月到2020年1月。这是我的代码:

HTML

<!-- Slideshow container -->
<div class="slideshow-container">
    <!-- Full-width images with number and caption text -->
    @for($i=-12;$i<=12;$i++)
    <div class="mySlides fade @if($i==0) {{'active'}} @endif" style="display: @if($i==0) {{'block'}} @endif;">
        <div class="numbertext" >{{ date('F - Y', strtotime('today -'.$i.'month')) }}</div>
    </div>
    @endfor
    <!-- Next and previous buttons -->
    <a class="prev"  onclick="plusSlides(1)">&#10094;</a>
    <a class="next" onclick="plusSlides(-1)">&#10095;</a>
</div>

JAVASCRIPT

<script type="text/javascript">

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  if (n > slides.length) {slideIndex = 1}    
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";  
    slides[i].classList.remove("active");  
  }
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block";  
  slides[slideIndex-1].className += " active";
}

</script>

就像我的输出看起来一样(以便您可以更好地理解我的问题): Screenshot of my app

正如您在我的代码中看到的那样,我试图使当前月份和年份的幻灯片处于活动状态并显示块,但始终选择“ 2020年1月”。

让我知道我在哪里做错了。

1 个答案:

答案 0 :(得分:0)

为了获得序列... 2018年1月至2020年1月...您的循环是错误的。更改为:

 @for($i=-12;$i<=12;$i++)
<div class="mySlides fade @if($i==0) {{'active'}} @endif" style="display: @if($i==0) {{'block'}} @endif;">
    <div class="numbertext" >{{ date('F - Y', strtotime('today -'.$i.'month')) }}</div>
</div>
@endfor

收件人:

@for($i=-12;$i<=12;$i++)
<div class="mySlides fade @if($i==0) {{'active'}} @endif" style="display: @if($i==0) {{'block'}} @endif;">
    <div class="numbertext" >{{ date('F - Y', strtotime('today +'.$i.'month')) }}</div>
</div>
@endfor

strtotime

中使用+而不是-

此外,此样式属性是错误的:

@if($i==0) {{'block'}} @endif;"

使用else部分以设置

以下是结构已更改的代码段:

#carousel-example {
    width: 100%;
    height: auto;
    padding: 50px;
}
.numbertext {
    color:black;
    display:flex;
    align-items:center;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<div id="carousel-example" class="carousel slide" data-ride="carousel">
    <div class="row">
        <div class="col-xs-offset-3 col-xs-6">
            <div class="carousel-inner">
                <!-- Full-width images with number and caption text -->
                <div class="item">
                    <div class="numbertext"><div><p>January - 2018</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>February - 2018</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>March - 2018</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>April - 2018</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>May - 2018</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>June - 2018</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>July - 2018</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>August - 2018</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>September - 2018</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>October - 2018</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>November - 2018</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>December - 2018</p></div></div>
                </div>
                <div class="item active">
                    <div class="numbertext"><div><p>January - 2019</p></div></div>
                </div>
                <div class="item ">
                    <div class="numbertext"><div><p>February - 2019</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>March - 2019</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>April - 2019</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>May - 2019</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>June - 2019</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>July - 2019</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>August - 2019</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>September - 2019</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>October - 2019</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>November - 2019</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>December - 2019</p></div></div>
                </div>
                <div class="item">
                    <div class="numbertext"><div><p>January - 2020</p></div></div>
                </div>
            </div>
        </div>
    </div>
    <a class="left carousel-control" href="#carousel-example" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left"></span>
    </a>
    <a class="right carousel-control" href="#carousel-example" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right"></span>
    </a>
</div>