How to scrollTop when html, body is set to height: 100%?

后端 未结 6 1249
别跟我提以往
别跟我提以往 2021-01-04 03:26

The following won\'t work unless I remove height: 100% from body and html. However, i need that style as I am using it for other elements on the page.

<

6条回答
  •  萌比男神i
    2021-01-04 03:49

    I had the same problem with body { height: 100%; } and scrollTop(0).

    Since I was not able to change html/css for different reasons, workaround was to remove height before scrollTop, and then revert to initial state.

    // Change height:100% into auto
    $('body').css('height', 'auto');
    // Successfully scroll back to top
    $('body').scrollTop(0);
    // Remove javascript added styles
    $('body').css('height', '');
    

提交回复
热议问题