body.scrollTop is deprecated in strict mode. Please use 'documentElement.scrollTop' if in strict mode and 'body.scrollTop' only if in quirks mode.

StackOverflow https://stackoverflow.com/questions/21268450

  •  30-09-2022
  •  | 
  •  

سؤال

I'm receiving the error:

body.scrollTop is deprecated in strict mode. Please use 'documentElement.scrollTop' if in strict mode and 'body.scrollTop' only if in quirks mode.

My code is:

$(document).ready(function(){

    //Animates Scrolling to anchor
    function scrollToAnchor(aid){
        var divTag = $("div[name='"+ aid +"']");
        $('html,body').animate({scrollTop: divTag.offset().top},'slow');
    }

    //If Checking out as guest, scroll to Shipping Information
    $("#ReadDescription").click(function() {
        scrollToAnchor('longdescreadmore');
    });

});

How can I edit my code to use this documentElement.ScrollTop?

هل كانت مفيدة؟

المحلول

Dagg Nabbit gave the solution. Change

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

to

$('html').animate({scrollTop: divTag.offset().top},'slow');

if you want to avoid the deprecation warning in Chrome. (Why is body.scrollTop deprecated?)

It works because documentElement is the html node:

$('html')[0] === document.documentElement //-> true
$('body')[0] === document.body            //-> true

But your code is working now (albeit with a warning) and it will keep working when Chrome removes the "quirky" behavior. You shouldn't change your code if you want to continue supporting browsers that use body.scrollTop to represent the scrolling viewport in standards mode (older Chrome and Safari, I think).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top