Why is body.scrollTop deprecated?

后端 未结 4 1482
日久生厌
日久生厌 2020-12-01 08:50

It seems body.scrollTop (and body.scrollLeft) are deprecated in ES5 strict-mode. What is the reason for this, given that it still seems okay to use

4条回答
  •  失恋的感觉
    2020-12-01 09:36

    I noticed my code stop working on newer versions of Chrome. I fixed it by using window.scrollY

    Before:

    var scrollTop = document.body.scrollTop;
    

    Now:

    var scrollTop = window.scrollY;
    

    It works all the time now. You can find more documentation here.

    Also, I was using:

    document.body.scrollTop = 0;
    

    now I replaced it with:

    window.scrollTo(0, 0);
    

提交回复
热议问题