gpt4 book ai didi

javascript - 在 html 中使用多个页面时,$(window).scroll 不会触发事件

转载 作者:行者123 更新时间:2023-12-03 10:18:21 25 4
gpt4 key购买 nike

我想在长页面中实现 float 操作按钮。当我编写简单的 html 页面时它效果很好。但是,当我将多个页面写入一个 html 时,它无法正常工作。在内部页面之间跳转后,它不会触发 $(window).scroll 事件。我将展示简化的源代码。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Floating Action Button Test</title>
<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>
<script>
jQuery(document).ready(function() {
var offset = 220;
var duration = 500;
jQuery(window).scroll(function() {
if (jQuery(this).scrollTop() > offset) {
jQuery('.back-to-top').fadeIn(duration);
} else {
jQuery('.back-to-top').fadeOut(duration);
}
});

jQuery('.back-to-top').click(function(event) {
event.preventDefault();
jQuery('html, body').animate({scrollTop: 0}, duration);
return false;
})
});
</script>

<!-- PAGEA -->

<section data-role="page" id="pagea">
<header data-role="header">
<h1>PAGE A</h1>
<div class="ui-btn-right">
<a id="logoutButton" class="headerButton" href="#pageb" data-role="button">PAGE B</a>
</div>
</header>

<div data-role="content">
<h1>CONTENT</h1>
<h2>AAA</h2>
<h2>BBB</h2>
<h2>CCC</h2>
<h2>DDD</h2>
<h2>EEE</h2>
<h2>FFF</h2>
<h2>HHH</h2>
<h2>III</h2>
<h2>JJJ</h2>
<h2>KKK</h2>
<h2>LLL</h2>
<h2>MMM</h2>
<h2>NNN</h2>
<h2>OOO</h2>
<h2>PPP</h2>
<h2>QQQ</h2>
<h2>RRR</h2>
<h2>SSS</h2>
<h2>TTT</h2>
</div><!--main-->

<footer data-role="footer">
FOOTER
</footer>

</section>

<!-- PAGEB -->

<section data-role="page" id="pageb">
<header data-role="header">
<h1>INDEX</h1>
<div class="ui-btn-right">
<a id="logoutButton" class="headerButton" href="#pagea" data-role="button">PAGE A</a>
</div>
</header>

<div data-role="content">
<h1>CONTENT</h1>
<h2>aaa</h2>
<h2>bbb</h2>
<h2>ccc</h2>
<h2>ddd</h2>
<h2>eee</h2>
<h2>fff</h2>
<h2>ggg</h2>
<h2>hhh</h2>
<h2>iii</h2>
<h2>jjj</h2>
<h2>kkk</h2>
<h2>lll</h2>
<h2>mmm</h2>
<h2>nnn</h2>
<h2>ooo</h2>
<h2>ppp</h2>
<h2>qqq</h2>
<h2>rrr</h2>
<h2>sss</h2>
</div><!--main-->

<footer data-role="footer">
FOOTER
</footer>

</section>

<!-- ############################################################## -->

<a href="#" class="back-to-top">Back To Top</a>

</body>
<style>
.back-to-top {
position: fixed;
bottom: 2em;
right: 0px;
text-decoration: none;
color: #000000;
background-color: rgba(235, 235, 235, 0.80);
font-size: 12px;
padding: 1em;
display: none;
}

.back-to-top:hover {
background-color: rgba(135, 135, 135, 0.50);
}
</style>
</html>

加载此页面时,通常会触发滚动事件并显示 float 操作按钮。但是,跳转到“Page B”后,滚动事件将不再被触发。

有人可以告诉我出了什么问题吗?

最佳答案

您好,我已经检查过了,您遇到的问题是当您单击页面 B 时,jQuery(window).scroll 不起作用,因为位置发生了变化,只需修复此更改 jQuery(window).scroll 到 jQuery(document).scroll,它将起作用

jQuery(document).ready(function() {
var offset = 220;
var duration = 500;
//here you should change jQuery(window) to jQuery(document) like this
jQuery(document).scroll(function() {
if (jQuery(this).scrollTop() > offset) {
jQuery('.back-to-top').fadeIn(duration);
} else {
jQuery('.back-to-top').fadeOut(duration);
}
});



jQuery('.back-to-top').click(function(event) {
event.preventDefault();
jQuery('html, body').animate({scrollTop: 0}, duration);
return false;
})
});

关于javascript - 在 html 中使用多个页面时,$(window).scroll 不会触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29760778/

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