Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Scroll To Section of Page

I'm trying to build a new trendy one page website where I scroll to a section on the page. I want to bring the "fixed" header to exactly where the content is. I am using the Zurb Foundation. This is what I have so far (some of the code found here):

  <li><%= link_to "Recent Work", "#", "data-scroll" => "recent" %></li>

  $(document).ready(function() {
$("a[data-scroll]").click(function() {
   var id = $(this).data("scroll")
    $('html,body').animate({
            scrollTop: $("#"+id).offset().top},
        'slow');
});
 });

  <div id="recent">some content</div>

Problem I have is stated above. I want to bring my fixed header to exactly where this div is placed on the page. Please advise.

like image 492
Brian Avatar asked Apr 13 '13 18:04

Brian


People also ask

How do I scroll to a specific div?

The scrollTo method: The scrollTo() is used to scroll to the specified element in the browser.

How do I smooth a div scroll?

click(function(event) { event. preventDefault(); $. scrollTo($('#myDiv'), 1000); });

How do you navigate to another page with a smooth scroll on a specific ID?

Modern Browsers detect the hash in the url and then automatically open that part. So, if you want to scroll smoothly to that part instead, you first need to reset the scroll position to 0 and then add smooth scrolling. // direct browser to top right away if (window. location.


1 Answers

There's a plugin for that.

Or just roll your own.

If you need to offset the "roll your own" solution then try the following:

$("#button").click(function() {
    var offset = 20; //Offset of 20px

    $('html, body').animate({
        scrollTop: $("#elementtoScrollToID").offset().top + offset
    }, 2000);
});
like image 110
cereallarceny Avatar answered Oct 11 '22 08:10

cereallarceny