Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Scroll to bottom of div on slideDown

Im trying to scroll to the bottom of the div when the user clicks on a link to slide the div down.

I've tried using the scrollTo and animate

$('html, body').animate({
scrollTop: $("#elementID").offset().top
}, 2000);

but nothing happens

heres the fiddle

http://jsfiddle.net/CLzfW/29/

like image 491
user1502223 Avatar asked Sep 03 '13 20:09

user1502223


People also ask

How do you scroll automatically to the bottom of the div using jQuery?

In jQuery, we can automatically use the scrollTop() and height() methods to scroll a page from top to bottom. For example, JQuery scroll to the bottom of div set the scrollTop property of a div's JavaScript to the value of scroll height property to scroll to the bottom of div.

How do I automatically scroll to the bottom of a div?

how to scroll down to the bottom of a div using javascript. scrollToBottom(theElement); // The specified node scrolls to the bottom.

How does jQuery slideDown work?

The slideDown() Method in jQuery is used to check the visibility of selected elements or to show the hidden elements. It works on two types of hidden elements: Elements hidden using jQuery methods. Elements hidden using display: none in CSS.

How can check scroll bottom in jQuery?

If you want to check whether the user has scrolled to the bottom of the page, you can use the scroll() jQuery event. The given code piece takes the top scroll of the window, so how much the page is scrolled down, it adds the height of the visible window and checks if it is equivalent to the height of the document.


1 Answers

Demo: http://jsfiddle.net/CLzfW/4/

$('.button').click(function(e){
  e.preventDefault();
  $('.expander').slideToggle();
    $('html, body').animate({
        scrollTop: 1000
    }, 2000);
});

Just use your .expander height.
If you need this to be variable you can do this: http://jsfiddle.net/CLzfW/26/

var scroll_to = $('.expander').offset().top + $('.expander').height();
$('.button').click(function(e){
  e.preventDefault();
  $('.expander').slideToggle();
    $('html, body').animate({
        scrollTop: scroll_to
    }, 2000);
});
like image 157
mtt Avatar answered Sep 19 '22 03:09

mtt