0

I'd like to use smooth scrolling on a onepager. On clicking a link, the depending section should scroll to the nav's bottom. Problem: the nav's position is fixed.

Fiddle: http://jsfiddle.net/LNwmt/

HTML:

<nav>
    <ul>
        <li><a href="#section1">Section 1</a></li>
        <li><a href="#section2">Section 2</a></li>
        <li><a href="#section3">Section 3</a></li>
        <li><a href="#section4">Section 4</a></li>
        <li><a href="#section5">Section 5</a></li>
        <li><a href="#section6">Section 6</a></li>
    </ul>
</nav>
<section id="section1">
    <h2>Section 1</h2>
    <p>[...] Text [...]</p>
</section>
<section id="section2">
    <h2>Section 2</h2>
    <p>[...] Text [...]</p>
</section>
<section id="section3">
    <h2>Section 3</h2>
    <p>[...] Text [...]</p>
</section>
<section id="section4">
    <h2>Section 4</h2>
    <p>[...] Text [...]</p>
</section>
<section id="section5">
    <h2>Section 5</h2>
    <p>[...] Text [...]</p>
</section>
<section id="section6">
    <h2>Section 6</h2>
    <p>[...] Text [...]</p>
</section>
<footer>
    Footer
</footer>

CSS:

nav { 
    position: fixed;
    top: 0;
    z-index: 3;
    width: 100%;
    height: 200px;
    background: gray; 
}

section {
    border: 1px solid red;
    margin: 10px 0 10px 0;
}

section:first-of-type {
    margin-top: 200px;
}

footer {
    height: 100px;
    background: gray;
}

JS:

$(document).ready(function(){
$('a[href^="#"]').on('click',function (e) {
    e.preventDefault();

    var target = this.hash,
    $target = $(target);

    $('html, body').stop().animate({
        'scrollTop': $target.offset().top-200 // - 200px (nav-height)
    }, 900, 'swing', function () {
        window.location.hash = target;
    });
});
});

Does anyone know, how to make this code working fine? Thanks in advance!

Mr. B.
  • 8,041
  • 14
  • 67
  • 117
  • This appears to work find in Chrome - items 4-6 won't scroll all the way up as the page length won't allow it. What behavior would you like to see? – dc5 Sep 05 '13 at 16:47
  • @dc5 You're right, the first items are working in Chrome, but not in Firefox. I'd like to have it working like in Chrome (most important!), browser-independent. The scrolling of item 4-6 should work as well. Maybe there's an additional offset needed. – Mr. B. Sep 05 '13 at 16:54
  • It's the hash that is triggering the additional scroll. Perhaps this post will help: [Modifying document.location.hash without page scrolling](http://stackoverflow.com/questions/1489624/modifying-document-location-hash-without-page-scrolling)? – dc5 Sep 05 '13 at 17:03

1 Answers1

3

For Firefox, one fix for the jumping scrolling behavior is to change the location hash to one that doesn't exist in the document.

Changing your code to:

$(document).ready(function () {
    $('a[href^="#"]').on('click', function (e) {
        e.preventDefault();

        var target = this.hash,
            $target = $(target);

        $('html, body').stop().animate({
            'scrollTop': $target.offset().top - 200 // - 200px (nav-height)
        }, 900, 'swing', function () {
            // Replace this with something that can be easily parsed and used by your code
            window.location.hash = '1' + target;
        });
    });
});

Seems to fix the problem: Demo jsFiddle

dc5
  • 12,341
  • 2
  • 35
  • 47