Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

javascript - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...