我正在将网站从Bootstrap版本3.x升级到版本4.3.1。我改写了旧的Bootstrap文件,还将jQuery脚本引用从1.x更改为3.3.1。
我的问题是这样的:在升级之前可以使用的jQuery现在无法使用。具体来说,我们有一个目前尚未运行的轮播。
控制台未显示任何错误。没有关于Javascript的错误或警告。
以下是轮播的HTML:
<div id="carousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel" data-slide-to="0" class="active"></li>
<li data-target="#carousel" data-slide-to="1"></li>
<li data-target="#carousel" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="/i/slider1.jpg" alt="Jobs for veterans testimonial">
<div class="carousel-caption slide1">I had put my resume on 7 other major job boards and the BEST results came from<br><em>Michael</em>
</div>
</div>
<div class="item">
<img src="/i/slider2.jpg" alt="Job offer former military testimonial">
<div class="carousel-caption slide2">I posted my resume on . I received a job offer in 2 days.<br> <em>C. Dean</em></div>
</div>
<div class="item">
<img src="/i/slider3.jpg" alt="better job testimonial">
<div class="carousel-caption slide3">Thanks to I found a better job within a matter of weeks.... I highly recommend – it truly works.<br><em>J. Spivey</em></div>
</div>
</div>
</div>
还有jQuery:
$(document).ready(function() {
$('.carousel').carousel({
interval: 8000,
pause: "hover"
})
});
如何使轮播再次工作?
答案 0 :(得分:1)
主要问题是item
包装器中的类carousel-item
必须替换为类carousel-inner
。另外,如果您阅读文档here,可能还需要考虑其他一些事情,例如:
1)向图像添加d-block
和w-100
类。
2)定义图像的width
和height
属性。
$(document).ready(function()
{
$('.carousel').carousel({
interval: 8000,
pause: "hover"
});
});
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<div id="carousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel" data-slide-to="0" class="active"></li>
<li data-target="#carousel" data-slide-to="1"></li>
<li data-target="#carousel" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="carousel-item active">
<img src="https://via.placeholder.com/600x300" alt="Jobs for veterans testimonial" width="600px" height="300px" class="d-block w-100">
<div class="carousel-caption slide1">
I had put my resume on 7 other major job boards and the BEST results came from<br><em>Michael</em>
</div>
</div>
<div class="carousel-item">
<img src="https://via.placeholder.com/600x300" width="600px" height="300px" alt="Job offer former military testimonial" class="d-block w-100">
<div class="carousel-caption slide2">
I posted my resume on . I received a job offer in 2 days.<br> <em>C. Dean</em>
</div>
</div>
<div class="carousel-item">
<img src="https://via.placeholder.com/600x300" alt="better job testimonial" width="600px" height="300px" class="d-block w-100">
<div class="carousel-caption slide3">
Thanks to I found a better job within a matter of weeks.... I highly recommend – it truly works.<br><em>J. Spivey</em>
</div>
</div>
</div>
</div>
使用text-center
上的carousel-item
类和图像上的img-fluid
来使图像在旋转木马上居中,以提高响应速度。
$(document).ready(function()
{
$('.carousel').carousel({
interval: 8000,
pause: "hover"
});
});
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<div id="carousel" class="carousel slide bg-warning" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel" data-slide-to="0" class="active"></li>
<li data-target="#carousel" data-slide-to="1"></li>
<li data-target="#carousel" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="carousel-item active text-center">
<img src="https://via.placeholder.com/600x300" alt="Jobs for veterans testimonial" width="600px" height="300px" class="img-fluid">
<div class="carousel-caption slide1">
I had put my resume on 7 other major job boards and the BEST results came from<br><em>Michael</em>
</div>
</div>
<div class="carousel-item text-center">
<img src="https://via.placeholder.com/600x300" width="600px" height="300px" alt="Job offer former military testimonial" class="img-fluid">
<div class="carousel-caption slide2">
I posted my resume on . I received a job offer in 2 days.<br> <em>C. Dean</em>
</div>
</div>
<div class="carousel-item text-center">
<img src="https://via.placeholder.com/600x300" alt="better job testimonial" width="600px" height="300px" class="img-fluid">
<div class="carousel-caption slide3">
Thanks to I found a better job within a matter of weeks.... I highly recommend – it truly works.<br><em>J. Spivey</em>
</div>
</div>
</div>
</div>