SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    scrollTo specific list item plus 240 pixels?

    I'm trying to do this: Get a specific list item scroll to it. This I do with this which works perfectly:
    Code:
    $('html, body').animate({
    	scrollTop:$('#ranking li:#test').offset().top}, 100);
    });
    Now, this makes the li show at the top, but actually I want to show it exactly at the middle of the screen. Lets say that the screenheight is 480px (iPhone) I would like to add pixels to the offset... Can this be done and if yes... How?

    Thanks in advance :-)

  2. #2
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,107
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    $('#ranking li:#test').offset().top is just a number so you add/remove other numbers from it.

    Code javascript:
    var getTop = function() {
      var top = $('#ranking li:#test').offset().top - ($(window).height() / 2);
      return top > 0 ? top : 0;
    }
    $('html, body').animate({
      scrollTop: getTop(), 100);
    });
    Something like that should work, that last line just makes sure top is a positive number.


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
  •