为什么我的Swiper滑块在Django中不起作用?

时间:2019-10-10 09:14:39

标签: javascript html django image-gallery swiper

到目前为止,设置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仍未浏览图像。没有控制台错误,似乎已经死了。

2 个答案:

答案 0 :(得分:3)

观察者 ObserveParents 不是导航的一部分。 因此,请使用以下代码更新脚本。

<?php session_start(); $_SESSION["name"]="ali"; ?> <input type="button" onclick="unset()" value="click"> <script> function my(){ var a=23; if(a==23){ <?php unset($_SESSION["name"])?> } else{ } } </script>

**这是呈现的HTML **

<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>

答案 1 :(得分:3)

它正在为我工​​作,您可以在工作示例下面查看。

  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>