Scrolling functions


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //native elem.scrollIntoView() works similar to an #anchor, putting top-left of element at top-left of viewport
  2.  
  3. //set document scroll position
  4. //optionally, specify a particular window/frame
  5. function setDocumentScrollPosition(top, left, windowFrame)
  6. {
  7. windowFrame = windowFrame || window;
  8. var w = new WindowDimensions(windowFrame);
  9. if(isNaN(top)) top = w.window.scroll.top;
  10. if(isNaN(left)) left = w.window.scroll.left;
  11.  
  12. //note: window.pageX/YOffset is read-only; the other two seem to depend on whether the browser is in quirks mode or not
  13. windowFrame.document.body.scrollLeft = left;
  14. windowFrame.document.documentElement.scrollLeft = left;
  15. windowFrame.document.body.scrollTop = top;
  16. windowFrame.document.documentElement.scrollTop = top;
  17. }
  18.  
  19. //set element scroll position
  20. function setScrollPosition(elem, top, left)
  21. {
  22. elem.scrollLeft = left;
  23. elem.scrollTop = top;
  24. }
  25. var scrollUP, panDown, scrollDown, panUp, scrollLeft, panRight, scrollRight, panLeft;
  26. scrollUp = panDown = function(elem, distance){ elem.scrollTop -= distance; };
  27. scrollDown = panUp = function(elem, distance){ elem.scrollTop += distance; };
  28. scrollLeft = panRight = function(elem, distance){ elem.scrollLeft -= distance; };
  29. scrollRight = panLeft = function(elem, distance){ elem.scrollLeft += distance; };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.