I have a list of blog posts and the number is reaching 25+, but it's all in the one page so I need to try and build a lazy loader.
I have tried various plugins, but none are working
http://jsfiddle.net/tara_irvine/S9GGz/6/
JavaScript:
(function($) {
$.belowthefold = function(element, settings) {
var fold = $(window).height() + $(window).scrollTop();
return fold <= $(element).offset().top - settings.threshold;
};
$.abovethetop = function(element, settings) {
var top = $(window).scrollTop();
return top >= $(element).offset().top + $(element).height() - settings.threshold;
};
$.rightofscreen = function(element, settings) {
var fold = $(window).width() + $(window).scrollLeft();
return fold <= $(element).offset().left - settings.threshold;
};
$.leftofscreen = function(element, settings) {
var left = $(window).scrollLeft();
return left >= $(element).offset().left + $(element).width() - settings.threshold;
};
$.inviewport = function(element, settings) {
return !$.rightofscreen(element, settings) && !$.leftofscreen(element, settings) && !$.belowthefold(element, settings) && !$.abovethetop(element, settings);
};
$.extend($.expr[':'], {
"below-the-fold": function(a, i, m) {
return $.belowthefold(a, {threshold : 0});
},
"above-the-top": function(a, i, m) {
return $.abovethetop(a, {threshold : 0});
},
"left-of-screen": function(a, i, m) {
return $.leftofscreen(a, {threshold : 0});
},
"right-of-screen": function(a, i, m) {
return $.rightofscreen(a, {threshold : 0});
},
"in-viewport": function(a, i, m) {
return $.inviewport(a, {threshold : 0});
}
});
})(jQuery);
$(function() {
var previous = "";
$(window).bind("scroll", function(event) {
$(".blog-post:in-viewport").each(function() {
$(this).addClass("visible");
});
});
});
http://jsfiddle.net/tara_irvine/S9GGz/9/
http://jsfiddle.net/tara_irvine/S9GGz/13/
Is there a way to check the top value of a div and if it's in view then a class is added so the div becomes visible (page load the div is hidden)?
I have looked at these posts, but after trying out various solutions, none have worked for me.
Where am I going wrong?
jQuery Waypoints is a nice plugin for reacting on elements coming into view; they even have a lazy-loading example.