当页面在每页上空闲3秒时,移动到下一页和后续页
Move to next and subsquent pages when page is idle for 3 seconds on each page
当页面空闲3秒时,有人能帮我提供一个自动在页面之间移动的脚本吗?目前,我所拥有的只能从一个页面移动到另一个页面,但我有4个页面,我希望它能影响所有页面。
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-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>
</head>
<body>
<div data-role="page" id="page1">
<div data-role="header">
<h1>Page1</h1>
</div>
<div data-role="content">This is page 1</div>
<div data-role="footer">
<h4>Footer</h4>
</div>
</div>
<p>
<div data-role="page" id="page2">
<div data-role="header">
<h1>Page 2</h1>
</div>
<div data-role="content">This is page 2</div>
<div data-role="footer">
<h4>Footer</h4>
</div>
</div>
</p>
<p>
<div data-role="page" id="page3">
<div data-role="header">
<h1>Page 3</h1>
</div>
<div data-role="content">This is page 3</div>
<div data-role="footer">
<h4>Footer</h4>
</div>
</div>
<p>
<div data-role="page" id="page4">
<div data-role="header">
<h1>Page 4</h1>
</div>
<div data-role="content">This is page 4</div>
<div data-role="footer">
<h4>Footer</h4>
</div>
</div>
</p>
</body>
</html>
Here's a fiddle to my page
http://jsfiddle.net/91wu20fr/
JQUERY
$(function () {
$(document).on("mousemove keypress", function () {
clearTimeout(goToNextPage);
goToNextPage = setTimeout(function () {
location.href = '#page2';
}, 3000);
});
});
编辑、更新
注意,div
不是p
元素的有效子元素。去除了作为div
元素亲本的p
元素
html
<!DOCTYPE html>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-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="script.js"></script>
<body>
<div data-role="page" id="page1">
<div data-role="header">
<h1>Page1</h1>
</div>
<div data-role="content">This is page 1</div>
<div data-role="footer">
<h4>Footer</h4>
</div>
</div>
<div data-role="page" id="page2">
<div data-role="header">
<h1>Page 2</h1>
</div>
<div data-role="content">This is page 2</div>
<div data-role="footer">
<h4>Footer</h4>
</div>
</div>
<div data-role="page" id="page3">
<div data-role="header">
<h1>Page 3</h1>
</div>
<div data-role="content">This is page 3</div>
<div data-role="footer">
<h4>Footer</h4>
</div>
</div>
<div data-role="page" id="page4">
<div data-role="header">
<h1>Page 4</h1>
</div>
<div data-role="content">This is page 4</div>
<div data-role="footer">
<h4>Footer</h4>
</div>
</div>
</body>
</html>
如果location
的hash
从#page1
开始,则可以使用Number()
、String.prototype.slice()
来使用现有的js
来增加页面。
将mousemove
、keypress
事件处理程序定义为命名函数,将.on()
替换为.one()
,以防止在每个mousemove
事件调用处理程序;在.one()
处理程序之外定义的goToNextPage
变量
js
$(function() {
location.href = "#page1"
var goToNextPage = null;
function updatePage(event) {
console.log(event)
var currentPage = Number(location.hash.slice(-1));
// if `currentPage` is less than 4
if (currentPage !== 4) {
goToNextPage = setTimeout(function() {
if ((currentPage + 1) <= 4) {
// set `location.href` to `#page` + `currentPage` + `1`
location.href = "#page" + (currentPage + 1);
console.log(location.hash);
// reset event handlers
$(document).one("mousemove keypress", updatePage);
}
}, 3000);
} else {
clearTimeout(goToNextPage)
}
}
$(document).one("mousemove keypress", updatePage);
});
plnkrhttp://plnkr.co/edit/tunX9CjEqNEZWxoxXU1b?p=preview
相关文章:
- 传递表单值并移动到另一页
- 使用滑动手势从一页移动到另一页
- 从页面上点击的链接中删除数据,然后移动到下一页&在CasperJS中重复
- 移动到对象的下一页
- 移动到 php 表的下一页而不刷新
- 使用smarty单击确认后移动下一页
- 从一页移动到另一页
- 当页面在每页上空闲3秒时,移动到下一页和后续页
- 当设置为导航栏固定顶部时,一页引导导航栏切换在移动模式下无法正常工作
- 页面刷新或移动到另一页后保留setTimeout()
- 如何在修改后将堆叠的图像移动到另一页
- 无法正常工作,请移动到另一页
- 重定向到移动网站的每一页或只是根索引
- jQuery移动向左/向右滑动到下一页不顺畅
- jQuery移动版-移动到下一页/上一页-点击后执行两次操作
- 如何使用jQuery访问已“移动”的DOM元素?绕着这一页
- 当手动键入url以移动到下一页时,Chrome加载脚本两次
- 页脚在移动到下一页时隐藏
- 我如何提交表单,并在同一时间移动到下一页
- 如何将文本从一页移动到另一页的形式