最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Scroll to id with fixed top bar - Stack Overflow

programmeradmin6浏览0评论

My page has a fixed top navigation bar an several id-elements on page. if i press a link one of these id-elements, it scrolls this id to top, but its hidden under the top navigation bar. In the wild the page is very long and has diffent "jumppoints".

I have a simplified fiddle to show the problem with the following html

<div id="fixedbar">This is the fixed top bar</div>
<div id="toplinks"><a href="#first">First</a> <a href="#second">Second</a></div>
<p id="first"><strong>First</strong>
    Lorem ipsum dolor sit amet[...]
</p>
<p id="second"><strong>Second</strong>
    Duis autem vel eum iriure dolor[...]
</p>
<div id="bottomlinks"><a href="#first">First</a> <a href="#second">Second</a></div>

and this css

body{padding:0;margin:0}
#toplinks{padding-top:2em}
#fixedbar{
    position:fixed;
    background-color:#ccc;
    height:2em;
    width:100%
}

I want that a click on a link scrolls to the right element but plements the fixed navigation bar. A possible solution should at best only include css, but can include javascript or jquery (1.9 is used production).

Thank you for any help!

My page has a fixed top navigation bar an several id-elements on page. if i press a link one of these id-elements, it scrolls this id to top, but its hidden under the top navigation bar. In the wild the page is very long and has diffent "jumppoints".

I have a simplified fiddle to show the problem with the following html

<div id="fixedbar">This is the fixed top bar</div>
<div id="toplinks"><a href="#first">First</a> <a href="#second">Second</a></div>
<p id="first"><strong>First</strong>
    Lorem ipsum dolor sit amet[...]
</p>
<p id="second"><strong>Second</strong>
    Duis autem vel eum iriure dolor[...]
</p>
<div id="bottomlinks"><a href="#first">First</a> <a href="#second">Second</a></div>

and this css

body{padding:0;margin:0}
#toplinks{padding-top:2em}
#fixedbar{
    position:fixed;
    background-color:#ccc;
    height:2em;
    width:100%
}

I want that a click on a link scrolls to the right element but plements the fixed navigation bar. A possible solution should at best only include css, but can include javascript or jquery (1.9 is used production).

Thank you for any help!

Share Improve this question asked Feb 28, 2013 at 15:41 Bastian RangBastian Rang 2,1871 gold badge19 silver badges25 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 10

Here is the JavaScript work around for this problem. Bind a click event to the <a> in toplinks and bottomlinks, on the click event find target <p> offset and scroll to it by using javascript.

$("#toplinks, #bottomlinks").on('click','a', function(event){ 
    event.preventDefault();
    var o =  $( $(this).attr("href") ).offset();   
    var sT = o.top - $("#fixedbar").outerHeight(true); // get the fixedbar height
    // pute the correct offset and scroll to it.
    window.scrollTo(0,sT);
});

Fiddle : http://jsfiddle/AnmJY/1/

Use scroll-padding-top instead. Check this post: https://dev.to/einlinuus/fixed-navigations-and-sections-here-is-scroll-padding-25nb

Modified JS:

 $("#toplinks a").click(function() {
    var id =  $(this).attr('href');
     $('html, body').animate({         
         scrollTop: $(id).offset().top-40
     }, 1000);
 });

Modified CSS:

body{padding:0;margin:0}
#toplinks{padding-top:2em}
#fixedbar{
    position:fixed;
    background-color:#ccc;
    height:40px;
    width:100%
}

Fiddle: http://jsfiddle/xSdKr/

40(px) is the menu height, 1000 is time in miliseconds to perform an animation.

JS solution is by far more elegant than pure CSS mostly because it keeps elements in place without any artificial paddings that might interfere with your website styles. It also includes animation - people usually finds smooth transitions much more acceptable than insta-flips with pure CSS/HTML as they make it easier to follow the content of the page and where you exactly are. Downside is that if someone uses #first in URL - he will see the menu overlapping with headers.

You can cheat a little bit.

http://jsfiddle/vyPkA/

body{padding:0;margin:0}
#toplinks{padding-top:2em; margin-bottom: -40px;}
#fixedbar{
    position:fixed;
    background-color:#ccc;
    height:2em;
    width:100%;
}
#first, #second{
    padding-top: 40px;
}
发布评论

评论列表(0)

  1. 暂无评论