You need something like this
function isScrolledIntoView(elem)
{
var docViewTop = $(window).scrollTop() + $(window).height(); //pixels to scroll to top + height of visible window
var elemTop = $(elem).offset().top; //pixels of the top of element from the start of page
return (elemTop <= docViewTop);
}
isScrolledIntoView("#xyz");
Use this in a combination of a local variable to fire only once;
var isFuncAlreadyFired = false;
$(window).scroll(function() {
if(!isFuncAlreadyFired && isScrolledIntoView("#xyz"))
{
//fire custom function;
isFuncAlreadyFired = true;
}
});
This function will tell you whether the element supplied to the function is visible or not.
Though I would not suggest but you can also unbind the scroll event from the document
once executed as
$(window).scroll(function() {
if(isScrolledIntoView("#xyz"))
{
//fire custom function;
$(document).unbind("scroll");
}
});