为什么我的 Swiper 滑块在 Django 中不起作用?
Why isn't my Swiper slider working in Django?
到目前为止,设置 Swiper 已经轻而易举了。所有图像都完美地加载到容器中,并且控制台没有记录任何错误。
但是虽然导航按钮显示出来并且可以点击,但它们实际上不起作用。单击和拖动它们也不会。控制台显示所有图像都已加载。
其他人也有类似的问题,有效的解决方法是添加:
observer: true,
observeParents: true,
到初始化代码,这对我没有影响。
这是我的 HTML 代码
<head>
<link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.min.css">
</head>
<div class="swiper-container gallery-top">
<div class="swiper-wrapper">
{% for image in car.image_set.all %}
<div class="swiper-slide"><img src="{{ image.image.url }}" style="width: 100%; "></div>
{% endfor %}
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
</div>
</div>
...
<script src="https://unpkg.com/swiper/js/swiper.min.js"></script>
...
<script>
var galleryTop = new Swiper('.gallery-top', {
spaceBetween: 10,
loop:true,
loopedSlides: 5, //looped slides should be the same
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
observer: true,
observeParents: true,
observeSlideChildren: true,
});
</script>
更新
感谢 Pratik 的反馈,我已经更新了 js,但 Swiper 仍然无法浏览图像。没有控制台错误,它似乎已经死了。
Observer 和 ObserveParents 不是导航的一部分。
因此,请使用以下代码更新您的脚本。
<script>
var galleryTop = new Swiper('.gallery-top', {
spaceBetween: 10,
loop:true,
loopedSlides: 5, //looped slides should be the same
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
observer: true,
observeParents: true,
observeSlideChildren: true
});
</script>
** 这是渲染后的 HTML**
<head>
<link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.min.css">
</head>
...
<body>
<div class="container">
<div class="swiper-container gallery-top">
<div class="swiper-wrapper">
<div class="swiper-slide"><img src="/media/image_11.JPG" style="width: 100%; "></div>
<div class="swiper-slide"><img src="/media/image_12_YQm2sd3.JPG" style="width: 100%; "></div>
<div class="swiper-slide"><img src="/media/image_19.JPG" style="width: 100%; "></div>
<div class="swiper-slide"><img src="/media/image_16_pm7OKF2.JPG" style="width: 100%; "></div>
</div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
</div>
...
<!-- JavaScript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<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>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="https://unpkg.com/swiper/js/swiper.min.js"></script>
</body>
</body>
它对我有用,你可以在下面查看工作示例。
var galleryTop = new Swiper('.gallery-top', {
spaceBetween: 10,
loop:true,
loopedSlides: 5, //looped slides should be the same
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
observer: true,
observeParents: true,
observeSlideChildren: true
});
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<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">
<link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.min.css">
<title>Swiper Slider</title>
</head>
<body>
<div class="container">
<div class="swiper-container gallery-top">
<div class="swiper-wrapper">
<div class="swiper-slide">
<img src="http://www.placebear.com/700/500" style="width: 100%; ">
</div>
<div class="swiper-slide">
<img src="http://www.placebear.com/700/500" style="width: 100%; ">
</div>
<div class="swiper-slide">
<img src="http://www.placebear.com/700/500" style="width: 100%; ">
</div>
<div class="swiper-slide">
<img src="http://www.placebear.com/700/500" style="width: 100%; ">
</div>
</div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<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>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="https://unpkg.com/swiper/js/swiper.min.js"></script>
</body>
</html>
到目前为止,设置 Swiper 已经轻而易举了。所有图像都完美地加载到容器中,并且控制台没有记录任何错误。
但是虽然导航按钮显示出来并且可以点击,但它们实际上不起作用。单击和拖动它们也不会。控制台显示所有图像都已加载。
其他人也有类似的问题,有效的解决方法是添加:
observer: true,
observeParents: true,
到初始化代码,这对我没有影响。
这是我的 HTML 代码
<head>
<link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.min.css">
</head>
<div class="swiper-container gallery-top">
<div class="swiper-wrapper">
{% for image in car.image_set.all %}
<div class="swiper-slide"><img src="{{ image.image.url }}" style="width: 100%; "></div>
{% endfor %}
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
</div>
</div>
...
<script src="https://unpkg.com/swiper/js/swiper.min.js"></script>
...
<script>
var galleryTop = new Swiper('.gallery-top', {
spaceBetween: 10,
loop:true,
loopedSlides: 5, //looped slides should be the same
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
observer: true,
observeParents: true,
observeSlideChildren: true,
});
</script>
更新
感谢 Pratik 的反馈,我已经更新了 js,但 Swiper 仍然无法浏览图像。没有控制台错误,它似乎已经死了。
Observer 和 ObserveParents 不是导航的一部分。 因此,请使用以下代码更新您的脚本。
<script>
var galleryTop = new Swiper('.gallery-top', {
spaceBetween: 10,
loop:true,
loopedSlides: 5, //looped slides should be the same
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
observer: true,
observeParents: true,
observeSlideChildren: true
});
</script>
** 这是渲染后的 HTML**
<head>
<link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.min.css">
</head>
...
<body>
<div class="container">
<div class="swiper-container gallery-top">
<div class="swiper-wrapper">
<div class="swiper-slide"><img src="/media/image_11.JPG" style="width: 100%; "></div>
<div class="swiper-slide"><img src="/media/image_12_YQm2sd3.JPG" style="width: 100%; "></div>
<div class="swiper-slide"><img src="/media/image_19.JPG" style="width: 100%; "></div>
<div class="swiper-slide"><img src="/media/image_16_pm7OKF2.JPG" style="width: 100%; "></div>
</div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
</div>
...
<!-- JavaScript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<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>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="https://unpkg.com/swiper/js/swiper.min.js"></script>
</body>
</body>
它对我有用,你可以在下面查看工作示例。
var galleryTop = new Swiper('.gallery-top', {
spaceBetween: 10,
loop:true,
loopedSlides: 5, //looped slides should be the same
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
observer: true,
observeParents: true,
observeSlideChildren: true
});
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<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">
<link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.min.css">
<title>Swiper Slider</title>
</head>
<body>
<div class="container">
<div class="swiper-container gallery-top">
<div class="swiper-wrapper">
<div class="swiper-slide">
<img src="http://www.placebear.com/700/500" style="width: 100%; ">
</div>
<div class="swiper-slide">
<img src="http://www.placebear.com/700/500" style="width: 100%; ">
</div>
<div class="swiper-slide">
<img src="http://www.placebear.com/700/500" style="width: 100%; ">
</div>
<div class="swiper-slide">
<img src="http://www.placebear.com/700/500" style="width: 100%; ">
</div>
</div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<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>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="https://unpkg.com/swiper/js/swiper.min.js"></script>
</body>
</html>