Skip to content

Instantly share code, notes, and snippets.

@brettchalupa
Created October 20, 2012 17:02
Show Gist options
  • Save brettchalupa/3924031 to your computer and use it in GitHub Desktop.
Save brettchalupa/3924031 to your computer and use it in GitHub Desktop.
aliceffekt's jquery visible image script
$(window).scroll(function () { visibleImg(); });
function visibleImg(){
viewHeight = $(window).height();
viewPort = $(window).scrollTop();
viewLength = viewHeight + viewPort;
$('a.image').each(function(index) {
imageId = $(this).attr("id");
imagePosition = $(this).offset();
imageLimit = viewPort - parseInt($(".image#"+imageId+" img").css("height"));
imageHeight = parseInt($(".image#"+imageId).css("height"));
// Response:Load
if($(this).has("img").length == 0 && imagePosition.top+imageHeight > viewPort && imagePosition.top < viewLength){
$(this).append("<img src='img/diary/"+imageId+".jpg' style='display:none'/>");
$(".image#"+imageId+" img").fadeIn(500);
}
// Response:Unload
else if($(this).has("img").length > 0 && imagePosition.top < imageLimit){
$(this).css("height",imageHeight);
$(".image#"+imageId+" img").remove();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment