I need to implement lazy loading for multiple ajax pages on my website. I want the ajax content to load dynamically as the user scrolls down the page, similar to infinite scroll or lazy image loading.
$('.lazy_content').on('load', function (e) {
data_url = $(this).attr("data-url");
data_id = $(this).attr("data-target-id");
$.ajax({
url: data_url,
type: "POST",
beforeSend: function () {
$(".loaderDiv").show();
$("#" + data_id).html("");
},
success: function (data) {
$(data).each(function(index, el) {
$(".loaderDiv").hide();
$("#" + data_id).html(data);
});
}
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="lazy_content" data-url="/ajax/comments/aurum-didyma-spa-beach-resort" data-target-id="comments-content">
<h4>COMMENTS</h4>
<div id="comments-content" class="content-target"></div>
</div>