Question

So i got the jQuery scroll working just fine on my page. But I want to change one thing. I don't want to show the anchor text in the url. ie. #products or #contactinfo

HTML CODE:

<div>
<a href="#products">Products</a>
<a href="#contactinfo">Contact info</a>
</div>

<div id="products"></div>

<div id="contactinfo"></div>

jQuery code:

    $(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
        }, 900, 'swing', function () {
            window.location.hash = target;
        });
    });
});
Was it helpful?

Solution

Try this:

HTML:

<div>
    <span class="link" rel="#products">Products</span>
    <span class="link" rel="#contactinfo">Contact info</span>
</div>

<div style="height:800px;"></div>
<div id="products">products</div>
<div style="height:800px;"></div>
<div id="contactinfo">contact</div>
<div style="height:800px;"></div>

Script:

$(document).ready(function(){
    $('.link').on('click',function (e) {
        $('html, body').stop().animate({
            'scrollTop': $($(this).attr('rel')).offset().top
        }, 900, 'swing', function () {});
    });
});

Fiddle

OTHER TIPS

just add a return false; at the end of the click function. Edit: and probalby remove this line, I mean.. it does exactly what you not want? Appearently?

window.location.hash = target;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top