我真的很难想出一个好标题,我已经尽力了。
当我尝试将 Material 添加到页面底部时,问题就出现了。我在 jsfiddle 上尝试了以下内容,它有效!当我向下滚动新的 <div>
s 生成得很好,并且它们是在需要的时候/地方生成的。
所以我尝试在没有 jsfiddle 的情况下完成它,并将所有代码放在一页中:
<html>
<meta>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
var a = $('<a class="post" href="?">');
a.append('<div class="name_of_post">TITLE</div>');
a.append('<div class="descriptioin_of_post">DESCRIPTION</div>');
$('a.post').last().after(a);
console.log($(document).height(), $(window).scrollTop(), $(window).height());
}
});
});
</script>
<style>
.post{
position: relative;
display: inline-block;
width: 49%;
height: 150px;
margin-top: 20px;
margin-left: 2%;
background-color: #FFF;
box-shadow: rgba(0, 0, 0, 0.2) 1px 1px 3px;
overflow: hidden;
padding: 10px 20px;
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select:none;
user-select:none;
-o-user-select:none;
transition: outline 0.3s;
text-decoration: none;
outline: solid 5px transparent;
}
.post:hover{
outline: solid 5px #fc490a;
}
.post:first-child{
margin-top: 0;
margin-left: 0;
}
.post:nth-child(2){
margin-top: 0
}
.post:nth-child(2n+1){
margin-left: 0;
}
.img_of_post{
position: absolute;
left:0px;
width: 35%;
top: 0px;
}
.name_of_post{
font-family: SegoeUIRegular;
font-weight: bold;
font-size: 20px;
color:black;
padding-bottom: 8px;
margin-left: 36.5%;
line-height: 23px;
}
.descriptioin_of_post{
font-family: SegoeUIRegular;
font-weight: normal;
font-size: 13px;
color:#666666;
margin-left: 36.5%;
line-height: 17px;
}
</style>
</head>
<body>
<a class='post' href='.'>
<div class='name_of_post'>OLD DIV</div>
<div class='description_of_post'>old description</div>
</a>
<a class='post' href='.'>
<div class='name_of_post'>OLD DIV</div>
<div class='description_of_post'>old description</div>
</a>
<a class='post' href='.'>
<div class='name_of_post'>OLD DIV</div>
<div class='description_of_post'>old description</div>
</a>
<a class='post' href='.'>
<div class='name_of_post'>OLD DIV</div>
<div class='description_of_post'>old description</div>
</a>
</body>
</html>
请注意,它与 jsfiddle 上的代码完全相同,只有一些差异(添加了 <html>
标签,页面内置 css 并手动添加了脚本标签)。
问题从这里开始,出于某种原因 new <div>
s 仅在我向上滚动时生成。换句话说,我需要向上滚动并点击顶部,而不是向下滚动并点击底部。
为什么在单页 HTML 代码中会发生这种情况?我需要做什么才能像在 JSFiddle 中一样工作?
最佳答案
在你的代码中替换:
if($(window).scrollTop() + $(window).height() == $(document).height()) {
到
if($(window).scrollTop() + window.innerHeight == $(document).height()) {
关于javascript - 滚动期间的不正确行为,根据代码的链接方式显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35482119/