SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Controlling visibility via scrolling

    I have a table full of inputs and there are times when I want to set the focus to a specific input but if the table is large enough or the window is small enough, the input may be scrolled out of view. I know I can do inputElement.focus() to put the input cursor in the element but can I determine if the element is in the view and force the window to scroll so that it is?

  2. #2
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your elements contain ids, you can simply add a hash to the current URL, like this:
    Code javascript:
    window.location.hash = "myId";

    The URL now looks like "www.yourdomain.com/#myId", and will automatically scroll to the element with id "myId".

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks but...

    ... I'd rather not change the URL that shows up in the address bar or that's used when the user reloads the page.

  4. #4
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't browsers automatically scroll down if you give focus to a form control?

    You can use scrollIntoView(), which seems to be supported by IE5+/Win, Firefox, Netscape 7.0+, Opera 9+, and Safari 2+.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kravvitz View Post
    Don't browsers automatically scroll down if you give focus to a form control?
    At least not Firefox 2.x.

    You can use scrollIntoView(), which seems to be supported by IE5+/Win, Firefox, Netscape 7.0+, Opera 9+, and Safari 2+.
    That sounds promising. I'll try it when I get back to the office.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks

    scrollIntoView() was just the thing.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •