Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scrollTop jQuery, scrolling to div with id?

So this is the current code I have

$(document).ready(function() {     $('.abouta').click(function(){         $('html, body').animate({scrollTop:308}, 'slow');         return false;     });     $('.portfolioa').click(function(){         $('html, body').animate({scrollTop:708}, 'slow');         return false;     });     $('.contacta').click(function(){         $('html, body').animate({scrollTop:1108}, 'slow');         return false;     }); }); 

When you click a link e.g. 'abouta' it scrolls to that specific section of the page. I'd rather do a scrollTo and then the id of a div so that I don't have to keep changing the scrollTo position if I change padding etc. Is there any way?

like image 761
Jake Avatar asked Dec 30 '10 18:12

Jake


2 Answers

instead of

$('html, body').animate({scrollTop:xxx}, 'slow'); 

use

$('html, body').animate({scrollTop:$('#div_id').position().top}, 'slow'); 

this will return the absolute top position of whatever element you select as #div_id

like image 77
jondavidjohn Avatar answered Sep 25 '22 14:09

jondavidjohn


My solution was the following:

document.getElementById("agent_details").scrollIntoView(); 
like image 29
Alexis Cabrera Mondeja Avatar answered Sep 23 '22 14:09

Alexis Cabrera Mondeja