Run ScrollTop with offset of element by ID

前端 未结 2 812
一个人的身影
一个人的身影 2020-12-07 14:30

Trying to make the browser scroll to a specific ID with an added offset -

$(\'html, body\').animate({scrollTop: $(\'#contact\').offset().top}, \'slow\');
         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 15:13

    var top = ($(".apps_intro_wrapper_inner").offset() || { "top": NaN }).top;   
    if (!isNaN(top)) {
    $("#app_scroler").click(function () {   
    $('html, body').animate({
                scrollTop: top
            }, 100);
        });
    }
    

    if you want to scroll a little above or below from specific div that add value to the top like this.....like I add 800

    var top = ($(".apps_intro_wrapper_inner").offset() || { "top": NaN }).top + 800;
    

提交回复
热议问题