javascript - 滚动期间的不正确行为,根据代码的链接方式显示

标签 javascript jquery

我真的很难想出一个好标题,我已经尽力了。

当我尝试将 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/

相关文章:

javascript - 具有多个条件的 Mongoose "Find"

javascript - 在 mongodb 中使用 $addtoset 将元素添加到数组

java - 关于 XSS 的一些说明

jquery - 单击时阻止 jquery TABS 向上跳跃/滚动?

javascript - 创建一个空的 jQuery 对象 : $({}) or $()

jquery 小问题我需要帮助

javascript - 如何使用javascript和ajax调用为文件上传添加进度条?

javascript - 如何在 xsl 中调用外部 javascript 函数?

javascript - 删除功能未按预期删除

javascript - 悬停时以矩形突出显示整个标签区域