php Laravel中的动态旋转木马不工作仅显示一个图像

r6vfmomb  于 2023-04-28  发布在  PHP
关注(0)|答案(4)|浏览(122)

我试图在laravel中创建一个动态的carousel,但我只看到一个图像。我一直在寻找解决方案,从许多小时,但我仍然卡住了
我试着改变我的代码从这个解决方案动态轮播在PHP Laravel只显示一个图像,但没有工作
home.blade.php代码

<div id="myCarousel" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0"     class="active">                                                                   </li>
</ol>

<div class="carousel-inner" role="listbox">
@foreach($sliders as $slider)
<div class="carousel-item active">
<img src="{{url('images', $slider->image)}}" class="d-block w-100"  alt="..."> 
</div>
@endforeach

<a class="carousel-control-prev" href="#myCarousel" role="button"  data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true">     </span>
    <span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#myCarousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>

master.blade.php代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-  scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="icon" href="{{asset('favicon.ico')}}">
<title>1233</title>

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"   integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script  src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link rel="stylesheet"   href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.  css" integrity="sha384-  Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"   crossorigin="anonymous">
<script     src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.  min.js" integrity="sha384-   ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"        crossorigin="anonymous"></script>

</head>
<body>

@include('front.nav')
@yield('content')
@include('front.footer')
</body>
</html>

任何帮助将不胜感激。

f4t66c6m

f4t66c6m1#

问题是为什么它不工作是因为循环

@foreach($sliders as $slider)
  <div class="carousel-item active">
     <img src="{{url('images', $slider->image)}}" class="d-block w-100"  alt="..."> 
  </div>
@endforeach

active类添加到所有图像。你只需要在一个图像上激活,你可以使用像这样的一些php解决方法在一个图像上设置为激活。

<div id="myCarousel" class="carousel slide" data-ride="carousel">
    <ol class="carousel-indicators">
        <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
    </ol>
    <div class="carousel-inner">
        @foreach($sliders as $key => $slider)
        <div class="carousel-item {{$key == 0 ? 'active' : '' }}">
            <img src="{{url('images', $slider->image)}}" class="d-block w-100"  alt="..."> 
        </div>
        @endforeach
    </div>
    <a class="carousel-control-prev" href="#myCarousel" role="button"  data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true">     </span>
        <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next" href="#myCarousel" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
</div>

你也忘了关闭<div class="carousel-inner"></div>标签。

jchrr9hc

jchrr9hc2#

<div class="carousel-item {{$loop->iteration == 1 ? 'active' : ''}}">
qncylg1j

qncylg1j3#

<head>部分中,
jquery脚本应该首先编写,然后是bootstrap。如果代码是正确的,并且之前显示了一个图像,那么现在应该可以工作了。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
t2a7ltrp

t2a7ltrp4#

我现在不知道你是否还需要解决方案,但问题就在这里

<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0"     class="active">                                                                   </li>
</ol>

你应该把它改成:

<ol class="carousel-indicators">
@foreach($sliders as $key => $slider)
<li data-target="#myCarousel" data-slide-to="{{$loop->index}}"     class="active">                                                                   </li>
@endforeach
</ol>

相关问题