我正在尝试在laravel中创建一个动态轮播,但是我只能看到一张图像。我一直在寻找解决方案,但是我仍然陷入困境
我已尝试通过此解决方案Dynamic carousel in PHP Laravel only shows one image更改代码,但没有成功
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>
任何帮助将不胜感激。
答案 0 :(得分:1)
其不起作用的问题是因为循环
@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>
标签。
答案 1 :(得分:0)
在EA - Education - Higher Ed Administration
部分,
jquery脚本应首先编写,并以引导程序结尾。 如果代码正确并且较早显示单个图像,则应该可以正常工作。
Job Titles:
Academic Program Assistant
Assistant Director
Assistant University Archivist
Grad Research Assistant, Chief of Staff
Instructor, Graduate Programs
Library Clerk 2
Program Associate
Percent of Total:
10.3%
答案 2 :(得分:0)
<div class="carousel-item {{$loop->iteration == 1 ? 'active' : ''}}">