3
$( "body").scroll( function() {
   $( "#myDiv").load( "test.html");
});

With this syntax we can load content into a div when the user scrolls. But before inserting into the div I want to make sure that div is in the viewport area when the user scrolls down.

If yes then I would like to load external content into that div. Please help me to achieve my goal.

4
  • If you know how heigh the content is you could monitor the amount of pixels scrolled. Although it's probably a bit of an inelagant idea. Commented Jan 25, 2011 at 12:45
  • 3
    Doing significant work in scroll event handler is a bad idea! How bad? Enough to bring twitter to its knees :) Check out this blog post by John Resig - ejohn.org/blog/learning-from-twitter Commented Jan 25, 2011 at 12:48
  • Have a look at the viewport plugin. Commented Jan 25, 2011 at 12:50
  • stackoverflow.com/questions/704758/… Commented Jan 25, 2011 at 14:35

1 Answer 1

2
  $( "body").scroll( function() { 
    if (document.elementFromPoint(x,y) == $("#whatever")) { 
      $( "#myDiv").load( "test.html");
    }
  }
Sign up to request clarification or add additional context in comments.

2 Comments

can u please explain what it does if (document.elementFromPoint(x,y) == $("#whatever")) { $( "#myDiv").load( "test.html"); } how document.elementFromPoint(x,y) will work?
It returns the element that is under those co-ordinates, relative to the upper-left corner of your browser window/iframe. developer.mozilla.org/en/DOM/document.elementFromPoint You can find the position of a DIV by using things such as developer.mozilla.org/en/DOM/element.offsetTop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.