gpt4 book ai didi

javascript - slider 仅在第一页 (jQM) 上工作,但在第二页、第三页和其他页面上失败

转载 作者:行者123 更新时间:2023-11-27 23:58:34 26 4
gpt4 key购买 nike

大家好,我在 JQM 多页文档上遇到 Swiper 问题。

我正在使用 http://www.idangero.us/sliders/swiper/ 的 Swiper在我的 JQM 元素中。

我可以在我的多页 JQM 文档结构的第一页上显示幻灯片并正常工作。但是,当我尝试将 slider 放到第一页以外的其他 JQM 页面时,幻灯片“将加载并显示”但“无法正常工作”。

无法正确工作的描述:加载错误会导致整个 slider 的宽度不正确,内容/列表未完全加载以及滑动手势无法正常工作,如左右滑动/拖动时,它应该停在每张幻灯片上。

请帮忙,谢谢。

以下两个示例,首先我在 JQM 文档的第一页上加载了 Swiper 并且它应该正常工作:

这是工作 slider 的 JSfiddle [第一页]: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>

用于不工作 slider 的 JSfiddle [第二页]: http://jsfiddle.net/jgYGJ/8/

在将 Swiper DIV 移至第二页后无法处理以下页面:

<!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 发起滑动:

$(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
})
})
})

代码更新:

Screenshot of the slider

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

最佳答案

您需要在 pagecontainershow 事件上初始化“swiper”。此事件无法绑定(bind)到特定页面,因此您需要检索事件页面的 id 并将其与包含“swiper”的页面的 id 进行比较。

将以下代码放在 head 中,不要将其包装在 $(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
}
});
});

Demo

获取事件页面的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
}
});
}
});

Demo

关于javascript - slider 仅在第一页 (jQM) 上工作,但在第二页、第三页和其他页面上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22550239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com