Malik Daud Ahmad Khokhar
Malik Daud Ahmad Khokhar

Reputation: 13730

In jQuery is there way for slideDown() method to scroll the page down too?

The slideDown() applied to a div element does the slide down, but doesn't scroll the page down and the div slided down div remains hidden from view.

Is there a way to scroll the page down as well so the user can view the div?

Upvotes: 12

Views: 17875

Answers (2)

James
James

Reputation: 111970

$.extend($.expr[':'],{
    inView: function(a) {
        var st = (document.documentElement.scrollTop || document.body.scrollTop),
            ot = $(a).offset().top,
            wh = (window.innerHeight && window.innerHeight < $(window).height()) ? window.innerHeight : $(window).height();
        return ot > st && ($(a).height() + ot) < (st + wh);
    }
});

if ($('#whatever').is(':not(:inView)')) {
    $('html,body').animate({ 
         scrollTop: $('#whatever').offset().top 
    }, 3000);
}

Upvotes: 8

redsquare
redsquare

Reputation: 78677

Quick demo here

Basically all you need is

 $('html, body').animate({ 
      scrollTop: $('#yourDiv').offset().top 
  }, 3000); 

Upvotes: 24

Related Questions