简体   繁体   English

在 AJAX 响应上实现无限滚动

[英]Implementing infinite scroll on AJAX response

How to Scroll an element inside a Div.如何在 Div 内滚动元素。 There are multiple div and each div content an element有多个div,每个div内容一个元素

Like喜欢

< div class="parent" >< p >< span >< /span >< /p ></ div >

< div class="parent" >< p >< span >< /span >< /p >< / div >

< div class="parent" >< p >< span >< /span >< /p >< / div >

< div class="parent" >< p >< span >< /span >< /p >< / div >

and they are generated dynamically (infinite scroll)它们是动态生成的(无限滚动)

where < div class=" parent " > is self scrolling div.其中< div class=" parent " >是自滚动 div。

How to put < span >< / span > element in visible part of div when page load AND/OR more data load on ajax call?当页面加载和/或在ajax调用上加载更多数据时,如何将< span >< / span >元素放在div的可见部分?

See Images for reference...I want highlighted text always visible part of div when page load...or more data load from ajax..请参阅图像以供参考...我希望突出显示的文本在页面加载时始终可见的 div 部分...或从 ajax 加载更多数据...

Here we go:开始了:

1) You have to add id or class for each element 1)您必须为每个元素添加 id 或 class

2) With focus function you may put element in visible part of div. 2) 使用焦点功能,您可以将元素放在 div 的可见部分。 You must to handle setVisibleElement() function document onload event.您必须处理 setVisibleElement() 函数文档 onload 事件。

<script>
function setVisibleElement(){
var parentId = document.getElementById("getParentId");
var element = document.getElementById("yourElementId");
parentId.style.overflow = "auto";
element.focus();
}
</script>

or you may use one of jQuery scroll plugin from here: enter link description here或者您可以从这里使用 jQuery 滚动插件之一:在此处 输入链接描述

Hope it helps;)希望能帮助到你;)

A few suggestions that I'd like to make first on the UI that you have chosen for infinite scrolling.我想首先在您为无限滚动选择的 UI 上提出一些建议。

  1. You need to have only one scrollable parent div.您只需要一个可滚动的父 div。

  2. To make the scroll happen :要使滚动发生:

     // CSS .parent { overflow: auto; } /* This makes sure that it's your parent div that can be scrolled Not the whole page. */
  3. Making the AJAX call:进行 AJAX 调用:

     function getData() { $.ajax({ url: "your url", method: 'GET' }) .done(function(data) { $.each(data, function(i, val) { $(".parent").append("<span>"+val+"</span>"); } }); };
  4. create a function for the scroll:为滚动创建一个函数:

     var scrollTracker = function() { if($(window).scrollTop() + $(window).height() >= $(document).height()) { getData(); } }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM