Scrolling to an element when outside of the viewport with prototypejs


Often times you’ll have be playing, say, videos with a list of videos down the page. If the list gets too long the video player becomes out of sight as the user scrolls down. This can be a big confuser to users: they click on a link to see a video and they don’t see anything happen because the video player is out of sight. Here’s what you can do: scroll to the video player if it is out of sight like so:

if ($("videoplayer").viewportOffset()[1] < 0) {
 Effect.ScrollTo('videoplayer', {duration: 0.2});
}

Requires both prototype and effects.js in scriptaculous.

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • Slashdot
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Share/Bookmark
  1. No comments yet.
(will not be published)