我想要在 div 中加载内容。我找到了很多 jQuery 滚动插件,但它们都是用于整个页面的加载。当滚动到 div 底部时,我想要在现有的 div 中加载内容。有人能帮忙吗?
var loading = false;//to prevent duplicate
function loadNewContent(){
$.ajax({
type:'GET',
url: url_to_new_content
success:function(data){
if(data != ""){
loading = false;
$("#div-to-update").html(data);
}
}
});
}
//scroll DIV's Bottom
$('#div-to-update').on('scroll', function() {
if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
if(!loading){
loading = true;
loadNewContent();//call function to load content when scroll reachs DIV bottom
}
}
})
//scroll to PAGE's bottom
var winTop = $(window).scrollTop();
var docHeight = $(document).height();
var winHeight = $(window).height();
if ((winTop / (docHeight - winHeight)) > 0.95) {
if(!loading){
loading = true;
loadNewContent();//call function to load content when scroll reachs PAGE bottom
}
}
<div data-src="transparent.png" url="http://stackoverflow.com"></div>
beforeLoad
回调函数:jQuery(function($) {
$("div[url]").lazy({
beforeLoad: function (element) {
element.load(element.attr("url"));
}
});
});
<div>
内容。<script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazy/1.7.1/jquery.lazy.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazy/1.7.1/plugins/jquery.lazy.ajax.min.js"></script>
准备你的 div 元素:(注意 data-loader
和 data-src
属性)
<div data-loader="ajax" data-src="ajax.html"></div>
$(function() {
$("div[data-src]").Lazy();
});
data-src
中的内容将在用户滚动到 <div>
时加载。更多信息在此处。
$("#div-to-update").html(data);
将内容加载到DIV
中。我已经更新了DIV
滚动的代码。 - Merlin