$.scrollTo to div doesn't work with my current JS

127 views Asked by At

I have tried to add a scrollTo function to my actual JS

$(function() {
    $('.click').click(function() {
        $('#me').stop().slideToggle(500);
        return false;
    });
});

It just basically display the hidden div under the actual text

What I would like to do is it should jump right away to div


Js fiddle http://jsfiddle.net/3zahL1qv/


What I tried to do is adding $.scrollTo($('#me'), 500);, but for some reason it does nothing but just break my current JS

Thanks in advance

1

There are 1 answers

3
Gaurav Kalyan On

Just add $(window).scrollTop(500); in your code. Here is a working example-http://jsfiddle.net/3zahL1qv/1/

$(function() {
    $('.click').click(function() {
        $('#me').stop().slideToggle(500);
        $(window).scrollTop(500);
        return false;
    });
});

Better way to do it as follow:

$(function() {
$('.click').click(function() {
    $('#me').stop().slideToggle(500);
    $('html, body').animate({scrollTop:$("#me").position()["top"]}, 'slow');
    return false;
});

});

Working example: http://jsfiddle.net/3zahL1qv/13/