Cameron
Cameron

Reputation: 28783

jQuery show DIV and scroll to it without hashtag in url

I have the following simple jQuery:

            $('#features').hide();

            $('#more').click(function(e)
            {
                e.preventDefault();
                $('#more').hide();
                $('#features').show();
            });

This shows a DIV when a user clicks the more link and using the preventDefault method the #features hash isn't added to the url. However I still want to scroll down to that DIV in the same way as when a hash is passed to the url just not show it in the address bar. How do I do this? Thanks

Note: I'm not looking for any fancy effects etc so don't want to use plugins like scrollTo etc

Upvotes: 7

Views: 6331

Answers (2)

Niklas
Niklas

Reputation: 30002

Just use scrollTop

$('html, body').scrollTop($("div#features").offset().top);

http://jsfiddle.net/niklasvh/4XEVc/

Upvotes: 1

lonesomeday
lonesomeday

Reputation: 237847

You'll need to use $(window).scrollTop():

$('#more').click(function (e) {
    e.preventDefault();
    $('#more').hide();
    $('#features').show();
    $(window).scrollTop($('#features').offset().top);
});

Upvotes: 5

Related Questions