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:

$('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 :slight_smile:

$(‘#ranking li:#test’).offset().top is just a number so you add/remove other numbers from it.


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.