【问题标题】:Image gallery swiper not showing on jQueryMobile pagejQueryMobile 页面上未显示图片库 swiper
【发布时间】:2014-05-09 21:06:30
【问题描述】:

我在我的 jQueryMobile 页面上使用 Swiper by idangero.us

我的 HTML 文件由两个“页面”组成。其中一个应该有一个图片库。它可以很好地查看图像,并且滑块可以正常工作,无需 jQueryMobile。但是一旦我使用 jQueryMobile,图像就不再显示了。

我发现的最相似的问题是:Swiper not working in Jquery Mobile 但这并没有帮助我找到解决方案。有人可以向我解释为什么这不起作用吗?

这是我的 HTML:

  <!doctype html>
  <html lang="en">
  <head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="css/idangerous.swiper.css">   
  <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" />   

  <style>
/* Demo Styles */
html {
  height: 100%;
}
body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 13px;
  line-height: 1.5;
  position: relative;
  height: 100%;
}
.swiper-container {
  width: 100%;
  height: 100%;
  color: #fff;
  text-align: center;
}
.swiper-slide .title {
  font-style: italic;
  font-size: 42px;
  margin-top: 80px;
  margin-bottom: 0;
  line-height: 45px;
}
.pagination {
  position: absolute;
  z-index: 20;
  left: 10px;
  bottom: 10px;
}
.swiper-pagination-switch {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 8px;
  background: #222;
  margin-right: 5px;
  opacity: 0.8;
  border: 1px solid #fff;
  cursor: pointer;
}
.swiper-visible-switch {
  background: #aaa;
}
.swiper-active-switch {
  background: #fff;
}
  </style>
</head>
<body>

<!--  Start page-->
  <div data-role="page" id="page_start">

            <article data-role="content">
                <ul data-role="listview" data-inset="true" >            
                    <li>
                        <a href="#page2">
                            <h1>Cabmontering</h1>
                                <img src="favicon.ico" alt="Min Pin" />
                                <p></p>
                        </a>
                    </li>  
                </ul>
            </article>
        </div> <!--End of startpage-->

  <!--Page 2-->
        <div data-role="page" id="page2">  
              <div class="swiper-container">
                <div class="swiper-wrapper">

                  <div class="swiper-slide">      
                    <img src="bilder/cabmontering/3_2.png" width="100%"/>
                  </div>

                  <div class="swiper-slide">
                    <img src="bilder/cabmontering/4_2.png" width="100%"/>
                  </div>

                  <div class="swiper-slide">
                    <img src="bilder/cabmontering/6_2.png" width="100%"/>
                  </div>

                  <div class="swiper-slide">
                    <img src="bilder/cabmontering/1.png" width="100%"/>
                  </div>

                  <div class="swiper-slide">
                    <img src="bilder/cabmontering/2.png" width="100%"/>
                  </div>

                </div>
                <div class="pagination"></div>
              </div> <!--End of "swiper_wrapper"-->
          </div> <!--End of page 2-->


  <script src="js/jquery-1.10.1.min.js"></script>
  <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script> 
  <script src="js/idangerous.swiper-2.1.min.js"></script>  
  <script>
  var mySwiper = new Swiper('.swiper-container',{
    pagination: '.pagination',
    paginationClickable: true,
    loop: true
  })
  </script>
</body>
</html>

【问题讨论】:

    标签: html jquery-mobile swiper


    【解决方案1】:

    插件根本没有被初始化,你需要在pageshow中包裹swiper代码,并且只需要一次.one()。其次,将JS库放在head中。

    <head>
      <script src="js/jquery-1.10.1.min.js"></script>
      <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script> 
      <script src="js/idangerous.swiper-2.1.min.js"></script>  
      <script>
        $(document).one("pageshow", "#page2", function () {
          var mySwiper = new Swiper('.swiper-container',{
              pagination: '.pagination',
              paginationClickable: true,
              loop: true
          });
        });
      </script>
    </head>
    

    【讨论】:

    • 但是,如果我有多个页面,其中包含不同图像的 swiper 怎么办? “第二个刷卡器”只会显示为普通图像。
    • 做同样的事情,只需在 pageshow 事件中更改页面 id。
    • @Zerato 你还有什么问题吗?
    • 感谢您的提问。不,它现在工作正常。我为另一个页面 (#page3) 创建了另一个 Swiper (mySwiper2) 并创建了一个名为 swiper-container2 的新类,用于第二个 swiper。
    猜你喜欢
    • 2020-11-07
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多