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

时间:2022-08-24 23:26:03

The slideDown applied to a div does the slideDown 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?

应用于div的slideDown执行slideDown但不向下滚动页面并且div向下滑动div仍然隐藏在视图之外。有没有办法滚动页面,以便用户可以查看div?

2 个解决方案

#1


22  

Quick demo here

这里快速演示

Basically all you need is

基本上你需要的只是

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

#2


8  

$.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);
}

#1


22  

Quick demo here

这里快速演示

Basically all you need is

基本上你需要的只是

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

#2


8  

$.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);
}