Run ScrollTop with offset of element by ID

前端 未结 2 803
一个人的身影
一个人的身影 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;
    
    0 讨论(0)
  • 2020-12-07 15:25

    No magic involved, just subtract from the offset top of the element

    $('html, body').animate({scrollTop: $('#contact').offset().top -100 }, 'slow');
    
    0 讨论(0)
提交回复
热议问题