Reputation: 280
Hello everyone, I have problem with Swiper on JQM multipage document.
I'm using Swiper by http://www.idangero.us/sliders/swiper/ in my JQM project.
I can have the slide display and working fine on my First Page of my Multipage JQM document structure. But when ever I try to put the slider to other JQM page other than the First Page, the slides 'Will Load and Display' BUT 'Not Working correctly'.
Description of Not Working Correctly: The loading error will cause incorrect width of the whole slider, the content/list not completely loaded and swipe gesture not working correctly, as it should stop at each slide when swipe/drag left-right.
Please help, thank you.
The following two example, first I load the Swiper on First Page of JQM document and its working as it should:
Here is the JSfiddle for Working Slider [Page One]: http://jsfiddle.net/h73k2/11/
<!DOCTYPE html><!--HTML5 doctype-->
<html>
<head>
<title>Your New Application</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0" />
<link rel="stylesheet" href="css/idangerous.swiper.css">
<link rel="stylesheet" href="css/idangerous.swiper.3dflow.css">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/appstore.css">
<link rel="stylesheet" href="css/jquery.mobile-1.4.0.css">
</head>
<body>
<!-- Start of first page -->
<div data-role="page" id="page1">
<div data-role="header">
<h1>Page One</h1>
</div><!-- /header -->
<div data-role="content">
<div class="swiper-container featured">
<div class="swiper-wrapper">
<div class="swiper-slide" style="background-image:url(img/big/1.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/2.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/3.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/4.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/5.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/6.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/7.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/1.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/2.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/3.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/4.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/5.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/6.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/7.jpg)">
</div>
</div>
</div>
<p>I'm first in the source order so I'm shown as the page.</p>
<p>View internal page called <a href="#page2">Page Two</a></p>
</div><!-- /content -->
<div data-role="footer">
<h4>Page Footer</h4>
</div><!-- /footer -->
</div><!-- /page -->
<!-- Start of second page -->
<div data-role="page" id="page2">
<div data-role="header">
<h1>Page Two</h1>
</div><!-- /header -->
<div data-role="content">
<p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p>
<p><a href="#page1">Back to Page One</a></p>
</div><!-- /content -->
<div data-role="footer">
<h4>Page Footer</h4>
</div><!-- /footer -->
</div><!-- /page -->
<!-- content goes here-->
<script src="js/jquery.min.js"></script>
<script src="js/jquery.mobile-1.4.0.min.js"></script>
<!-- Don't forget to get the latest Swiper and scrollbar version here-->
<script src="js/idangerous.swiper-2.0.min.js"></script>
<script src="js/idangerous.swiper.3dflow-2.0.js"></script>
<script src="js/appstore.js"></script>
</body>
</html>
JSfiddle for Not-Working Slider [Page Two]: http://jsfiddle.net/jgYGJ/8/
Not working on the following page after moved the Swiper DIV to Page Two:
<!DOCTYPE html><!--HTML5 doctype-->
<html>
<head>
<title>Your New Application</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0" />
<link rel="stylesheet" href="css/idangerous.swiper.css">
<link rel="stylesheet" href="css/idangerous.swiper.3dflow.css">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/appstore.css">
<link rel="stylesheet" href="css/jquery.mobile-1.4.0.css">
</head>
<body>
<!-- Start of first page -->
<div data-role="page" id="page1">
<div data-role="header">
<h1>Page One</h1>
</div><!-- /header -->
<div data-role="content">
<p>I'm first in the source order so I'm shown as the page.</p>
<p>View internal page called <a href="#page2">Page Two</a></p>
</div><!-- /content -->
<div data-role="footer">
<h4>Page Footer</h4>
</div><!-- /footer -->
</div><!-- /page -->
<!-- Start of second page -->
<div data-role="page" id="page2">
<div data-role="header">
<h1>Page Two</h1>
</div><!-- /header -->
<div data-role="content">
<div class="swiper-container featured">
<div class="swiper-wrapper">
<div class="swiper-slide" style="background-image:url(img/big/1.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/2.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/3.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/4.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/5.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/6.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/7.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/1.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/2.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/3.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/4.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/5.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/6.jpg)">
</div>
<div class="swiper-slide" style="background-image:url(img/big/7.jpg)">
</div>
</div>
</div>
<p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p>
<p><a href="#page1">Back to Page One</a></p>
</div><!-- /content -->
<div data-role="footer">
<h4>Page Footer</h4>
</div><!-- /footer -->
</div><!-- /page -->
<!-- content goes here-->
<script src="js/jquery.min.js"></script>
<script src="js/jquery.mobile-1.4.0.min.js"></script>
<!-- Don't forget to get the latest Swiper and scrollbar version here-->
<script src="js/idangerous.swiper-2.0.min.js"></script>
<script src="js/idangerous.swiper.3dflow-2.0.js"></script>
<script src="js/appstore.js"></script>
</body>
</html>
JS to initiate swiper:
$(function(){
//Featured Slide
var featuredSwiper = $('.featured').swiper({
slidesPerView:'auto',
centeredSlides: true,
initialSlide:7,
tdFlow: {
rotate : 30,
stretch :10,
depth: 150
}
})
//Thumbs
$('.thumbs-cotnainer').each(function(){
$(this).swiper({
slidesPerView:'auto',
offsetPxBefore:25,
offsetPxAfter:10,
calculateHeight: true
})
})
//Banners
$('.banners-container').each(function(){
$(this).swiper({
slidesPerView:'auto',
offsetPxBefore:25,
offsetPxAfter:10
})
})
})
Code update:
<!DOCTYPE html><!--HTML5 doctype-->
<html>
<head>
<title>Your New Application</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0" />
<script src="js/jquery.min.js"></script>
<script src="js/jquery.mobile-1.4.0.min.js"></script>
<!-- Don't forget to get the latest Swiper and scrollbar version here-->
<script src="js/idangerous.swiper-2.0.min.js"></script>
<script src="js/idangerous.swiper.3dflow-2.0.js"></script>
<!-- <script src="js/appstore.js"></script>-->
<script>
$(document).on("pagecontainerbeforehide", function (e, ui) {
var activeP = ui.nextPage;
var activeID = ui.nextPage[0].id;
if (activeID == "page2" && $(".swiper-container", activeP).length === 0) {
$("body .swiper-container")
.removeClass("swiper-hidden")
.prependTo($(".ui-content", activeP));
}
});
</script>
<link rel="stylesheet" href="css/idangerous.swiper.css">
<link rel="stylesheet" href="css/idangerous.swiper.3dflow.css">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/appstore.css">
<link rel="stylesheet" href="css/jquery.mobile-1.4.0.css">
</head>
<body>
<!-- Start of first page -->
<div data-role="page" id="page1">
<div data-role="header">
<h1>Page One</h1>
</div>
<!-- /header -->
<div data-role="content">
<p>I'm first in the source order so I'm shown as the page.</p>
<p>View Slider on <a href="#page2">Page Two</a>
</p>
</div>
<!-- /content -->
<div data-role="footer">
<h4>Page Footer</h4>
</div>
<!-- /footer -->
</div>
<!-- /page -->
<!-- Start of second page -->
<div data-role="page" id="page2">
<div data-role="header">
<h1>Page Two</h1>
</div>
<!-- /header -->
<div data-role="content">
<p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p>
<p><a href="#page1">Back to Page One</a>
</p>
</div>
<!-- /content -->
<div data-role="footer">
<h4>Page Footer</h4>
</div>
<!-- /footer -->
</div>
<!-- /page -->
<!-- content goes here-->
<div class="swiper-container featured swiper-hidden">
<div class="swiper-wrapper">
<div class="swiper-slide" style="background-image:url(http://furkidtag.com/img/big/1.jpg)"></div>
<div class="swiper-slide" style="background-image:url(http://furkidtag.com/img/big/2.jpg)"></div>
<div class="swiper-slide" style="background-image:url(http://furkidtag.com/img/big/3.jpg)"></div>
<div class="swiper-slide" style="background-image:url(http://furkidtag.com/img/big/4.jpg)"></div>
<div class="swiper-slide" style="background-image:url(http://furkidtag.com/img/big/5.jpg)"></div>
<div class="swiper-slide" style="background-image:url(http://furkidtag.com/img/big/6.jpg)"></div>
<div class="swiper-slide" style="background-image:url(http://furkidtag.com/img/big/7.jpg)"></div>
<div class="swiper-slide" style="background-image:url(http://furkidtag.com/img/big/1.jpg)"></div>
<div class="swiper-slide" style="background-image:url(http://furkidtag.com/img/big/2.jpg)"></div>
<div class="swiper-slide" style="background-image:url(http://furkidtag.com/img/big/3.jpg)"></div>
<div class="swiper-slide" style="background-image:url(http://furkidtag.com/img/big/4.jpg)"></div>
<div class="swiper-slide" style="background-image:url(http://furkidtag.com/img/big/5.jpg)"></div>
<div class="swiper-slide" style="background-image:url(http://furkidtag.com/img/big/6.jpg)"></div>
<div class="swiper-slide" style="background-image:url(http://furkidtag.com/img/big/7.jpg)"></div>
</div>
</div>
</body>
</html>
Upvotes: 2
Views: 1931
Reputation: 31732
You need to initialize "swiper" on pagecontainershow
event. This event cant be bound to a specific page, so you need to retrieve active Page's id and compare it with id of the page containing "swiper".
Place the below code in head
and don't wrap it in $(function () {});
.
$(document).on("pagecontainershow", function () {
$.mobile.pageContainer.pagecontainer("getActivePage").find('.featured').swiper({
slidesPerView: 'auto',
centeredSlides: true,
initialSlide: 7,
tdFlow: {
rotate: 30,
stretch: 10,
depth: 150
}
});
});
To retrieve active page's id
$(document).on("pagecontainershow", function () {
/* page's object */
var active = $.mobile.pageContainer.pagecontainer("getActivePage");
/* page's id */
var activePage = $.mobile.pageContainer.pagecontainer("getActivePage")[0].id;
if( activePage == "targetPageID") {
$(".featured", active).swiper({
slidesPerView: 'auto',
centeredSlides: true,
initialSlide: 7,
tdFlow: {
rotate: 30,
stretch: 10,
depth: 150
}
});
}
});
Upvotes: 2