为什么对scrollTop使用"html,body"而不是"html" [英] why to use 'html, body' for scrollTop instead of just 'html'

查看:73
本文介绍了为什么对scrollTop使用"html,body"而不是"html"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从stackoverflow得到了一个解决方案,可以像这样滚动到页面顶部

I got a solution from stackoverflow to scroll to the top of the page like

$('html, body').animate({scrollTop: $("#page").offset().top}, 2000);

但是我不确定为什么要对'scrollTop'使用'html,body'而不是'html'吗?

But I am not sure why to use 'html, body' for scrollTop instead of just 'html' ?

推荐答案

某些浏览器将整体"滚动应用于document.documentElement(<html>元素),而其他浏览器则将其应用于document.body(<body>元素).为了与两者兼容,您必须将滚动应用于两者.

Some browsers apply the "overall" scroll to document.documentElement (the <html> element) and others to document.body (the <body> element). For compatibility with both, you have to apply the scrolling to both.

这篇关于为什么对scrollTop使用"html,body"而不是"html"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆