简体   繁体   English

为什么向下滚动时不重新加载数据,而在无限滚动中返回页面顶部时为什么不重新加载数据?

[英]Why do not I reload the data when I scroll down and do it when I go back to the top of the page in my infinite scroll?

What I'm doing is an infinite scroll with jQuery, AJAX and PHP but when you have to load data, I reload the page when I go to the top of the page and not when it is almost at the bottom of the page, I leave my code of jQuery and AJAX. 我正在做的是使用jQuery,AJAX和PHP进行无限滚动的方法,但是当您必须加载数据时,我会在页面顶部而不是在页面底部时重新加载页面,离开我的jQuery和AJAX代码。 Thank you in advance! 先感谢您!

<script type="text/javascript">
            var start = 0;
            var limit = 5;
            var reachedMax = false;

            $(window).scroll(function () {
                if ($(window).scrollTop() == $(document).height() - $(window).height())
                    getData();
            });

            $(document).ready(function () {
               getData();
            });

            function getData() {
                if (reachedMax)
                    return;

                $.ajax({
                   url: 'publicaciones.php',
                   method: 'POST',
                   dataType: 'text',
                   data: {
                       getData: 1,
                       start: start,
                       limit: limit
                   },
                   success: function(response) {
                        if (response == "reachedMax")
                            reachedMax = true;
                        else {
                            start += limit;
                            $(".results").append(response);
                        }
                    }
                });
            }
        </script>

This is my PHP, I add the code to resolve my problem. 这是我的PHP,我添加了代码来解决我的问题。 I think my PHP its necessary. 我认为我的PHP是必要的。

<?php
    session_start();
    include ('../conexion.php');

    if(isset($_SESSION['id']))
    {
        $id = mysqli_real_escape_string($conexion, $_SESSION['id']);

        $infouser = mysqli_query($conexion, "SELECT * FROM users WHERE id = $id");  
        $use = mysqli_fetch_array($infouser);
    }

    if(isset($_POST['getData']))
    {   
        $start = mysqli_real_escape_string($conexion,$_POST['start']);
        $limit = mysqli_real_escape_string($conexion,$_POST['limit']);

        $consulta = mysqli_query($conexion, "SELECT * FROM publicaciones ORDER BY id_pub DESC LIMIT $start, $limit");   

        if(mysqli_num_rows($consulta) > 0)
        {   
            $response = "";

            while($data = mysqli_fetch_array($consulta)) {
                $response .= "
                    <div class='card mt-3 border-0 rounded-0'>
                        <div class='card-body'>
                            <div class='row'>
                                <div class='col-sm-2'>
                                    <div class='img-user rounded-circle'>
                                        <img class='img-fluid-pub' src='../avatars/".$use['avatar']."' alt='User Image'>
                                    </div>
                                </div>
                                <div class='col-sm-10'>
                                    <a class='text-muted h3' href='perfil.php?id=".$use['id']."'>".$use['usuario']."</a><h3>Seguir</h3>
                                    <p>".$data['fecha']."</p>
                                    <p>".$data['titulo']." Me gusta</p>
                                    <p id='descripcion'>".$data['descripcion']."</p>
                                    <p class='leermas' onclick='leerMas(this);'>Leer más...</p>             
                                    <video controls>
                                        <source src='../anadir-publicacion/publicaciones/".$data['video']."' type='video/mp4'>
                                        Your browser does not support HTML5 video.
                                    </video>
                                    <p>Kcal: ".$data['kcal']." CarbHid: ".$data['carbohidratos']." Prot: ".$data['proteinas']." Gras: ".$data['grasas']." Tiempo: ".$data['tiempo']."m Personas: ".$data['personas']." Dinero aprox: ".$data['precio']."€ Categoría: ".$data['categoria']."</p>
                                </div>
                            </div>
                        </div>
                    </div>  
                ";
            }

            exit($response);
        }
        else 
            exit('reachedMax');
    }   
?>

It is normal $(window).scrollTop() is equal to 0 at the start and $(document).height() is equal to $(window).height() so yes 0 == 0 at the top of your page 正常情况下, $(window).scrollTop()开头等于0,而$(document).height()等于$(window).height()所以是0 == 0在页面顶部

what you want is 你想要的是

 var nb_px_before_load = 100
    if($(window).innerHeight() - $(window).scrollTop() < nb_px_before_load){
        getData();
    }

You can easily see this with the devTools of chrome with breakpoints 您可以使用带有断点的chrome的devTools轻松看到这一点

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么当我在页面顶部滚动并单击附加表格的链接时,它会向下滚动 - Why when i scroll top my page and click link which append a form it scroll me down 为什么我的页面顶部按钮在我的 API 应用程序上滚动到顶部然后返回时会卡顿/滞后/粘滞,但在网页上却正常运行? - Why does my top of page button stutter/lag/stick when I scroll to top and then back down on my API app but acts normal on webpages? 当人们进入页面时,如何进行自动滚动 - How do i make a automatic scroll when people go on a page 当我向上滚动并在向上滚动或当我将鼠标悬停在上面时,如何使导航条变得透明? - How do i make my nav bar transparent when I scroll down and come back when I scroll up or when I hover my mouse over is? 完美滚动到顶部。 但是当我向下滚动并单击页面上的任意位置时,滚动再次移至顶部 - Perfectly scroll to top. but when i scroll down and click anywhere on page, again scroll moves to the top 我希望页面向下滚动,当我的 cursor 接近页面顶部或底部时自动顶部 - I want the page to scroll down, top automatically when my cursor is near the top or bottom of the page 为什么我打开固定导航菜单时 go 到页面顶部? - Why do I go to the top of the page when I open my fixed navigation menu? 在html和css中向下滚动时,如何使图像位于菜单栏后面? - How do I make the image go behind the menu bar when I scroll down in html & css? bootstrap-modal-向下滚动时如何阻止背景从顶部显示 - bootstrap-modal - How do I stop the background from showing at top when I scroll down 为什么在我滚动底部时无限滚动不起作用,而是在滚动顶部触发? - Why is Infinite scrolling not working when I scroll bottom, instead it triggers on scroll top?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM