javascript - 我的 .on Swipe 在中断之前只能工作一次并且必须刷新所有内容 - JQuery Mobile / Javascript
问题描述
因此,当我单击我的图像时,我设法为我编写了一个脚本,它会显示一个弹出窗口,当我在图像上向左或向右滑动时,它会在我的 javascript 中显示隐藏的图像。但是,当我到达我的索引页面并转到房屋 html 时。当我单击照片时,在我必须进入我的 javascript 文件之前,滑动功能不起作用,并且基本上在它再次工作之前重写滑动功能,但是在我回到我的索引页面后会中断。
这是我网站的索引页:http: //titan.dcs.bbk.ac.uk/~aaldib01/mad/madfma/index.html
然后房屋 > 2 卧室梯田 > house1.html
有没有办法让我解决问题或改进我的 javascript 以使其不再成为问题?
谢谢你。
*注意我认为的问题在于图像代码的放置位置。(我已经删除了大部分其他代码,因为这不会影响它)
我尝试使用 .bind("swiperight", function() 但它给了我相同的结果。它工作一次然后在我转到 index.html > > house1.html 后不起作用
这是 house1.html 代码(data-role="content":
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=yes">
<link rel="stylesheet" href="css/themes/blue.min.css" />
<link rel="stylesheet" href="css/themes/jquery.mobile.icons.min.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="images.js"></script>
<title>House 1</title>
</head>
<body>
<div data-role="page" id="house1" data-theme="a" data-dom-cache="true">
<div data-role="content">
<a href="#popupImg" data-rel="popup" data-position-to="window" data-transition="pop">
<img src="pictures/houses/house1/image1.PNG"/ style="width: 50%;"/>
<div data-role="popup" id="popupImg" data-theme="a" class="ui-corner-all">
<a href="#" data-rel="back" data-role="button" data-theme="a" data-icon="delete" data-iconpos="notext" class="ui-btn-right">Close</a>
<img src="pictures/houses/house1/image1.PNG" style="width: 100%;" />
</a>
</div>
<div data-role="footer" data-position="fixed">
<div data-role="navbar" data-id="footernav">
<ul>
<li><a href="about.html">About</a></li>
<li><a href="">Favourites</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>
这是javascript文件:
$(document).ready(function () {
var i = 0;
var imgURL = [];
imgURL.push('pictures/houses/house1/image1.PNG');
imgURL.push('pictures/houses/house1/image2.PNG');
imgURL.push('pictures/houses/house1/image3.PNG');
imgURL.push('pictures/houses/house1/image4.PNG');
$("#house1").on("swiperight",function () {
if (i < (imgURL.length - 1)) {
i++
} else {
i = 0;
}
var imgStr = "<img src=" + imgURL[i] + " style='width:100%'>";
$('#popupImg').html(imgStr);
});
$("#house1").on("swipeleft",function () {
if (i > 0) {
i--
} else {
i = (imgURL.length - 1);
}
var imgStr = "<img src=" + imgURL[i] + " style='width:100%'>";
$('#popupImg').html(imgStr);
});
});
解决方案
问题是,image.js
如果您从索引页面开始并导航到 House 1,则不会加载您的脚本。您可以通过在直接加载 House 1(它会在那里)而不是开始时查看站点的源来检查这一点索引并浏览到 House 1(它将从 中丢失<head>
)。
原因是 jQuery Mobile 使用 AJAX 在页面之间导航。默认情况下,每个页面请求只会更新<body>
元素,而不更新元素<head>
(更多信息可以在这个jQuery Mobile 演示页面上找到)。
该页面上引用了一些解决方案:
构建 jQuery Mobile 站点时最简单的方法是在每个页面的头部引用相同的样式表和脚本集。如果您需要为特定页面加载特定的脚本或样式,我们建议将逻辑绑定到
pageinit
事件(详细信息如下)以在创建特定页面时运行必要的代码(可以由其id
属性或其他一些方法)。
body
页面特定脚本的另一种方法是在未定义元素时在元素末尾data-role=page
或在第一个data-role=page
元素内包含脚本。
最终,由您决定哪种方法对您的特定用例最有意义。
推荐阅读
- c++ - 如何在不与接口交互的情况下调用 C++ Windows 桌面应用程序中的函数
- angular - 更改角度路由器 URL 方案
- android - 如何在毕加索中添加缓存功能?
- dart - Flutter 自定义范围滑块
- python - 将 sqlContext 数据帧转换为 pandas 数据帧时出错
- sql - 如何通过在函数/过程中传递两个值来返回多列
- spring-boot - 无法使用 Spring WebFlux 和 ReactiveCassandraRepository 编写自定义查询
- perl - 摘要::SHA 不能使用 sha512256_hex
- google-cloud-dataflow - 在 Python 中使用 Apache Beam/Dataflow 读取 UTF-16 csv
- logstash - Logstash 从现有的 json 创建新的 json