gpt4 book ai didi

javascript - 当每页页面空闲 3 秒时移至下一页和后续页面

转载 作者:太空狗 更新时间:2023-10-29 15:13:28 25 4
gpt4 key购买 nike

谁能帮我提供一个当页面空闲 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>&nbsp;
<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>&nbsp;
<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>&nbsp;
<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>

如果 locationhash 开始于 #page1 可以使用 Number() , String。 prototype.slice() 使用现有的 js 增加页面。

mousemovekeypress 事件处理程序定义为命名函数,将 .one() 替换为 .on() 防止处理程序在每个 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);
});

plnkr http://plnkr.co/edit/tunX9CjEqNEZWxoxXU1b?p=preview

关于javascript - 当每页页面空闲 3 秒时移至下一页和后续页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34305884/

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