1、获取视窗的宽高、文档的宽高、文档滚动距离的大小,分别写原生JS和JQ中的获取方法。
时间: 2024-05-06 19:20:44 浏览: 62
JS获取各种宽度、高度的简单介绍
原生JS获取方法:
获取视窗宽高:
```javascript
var viewportWidth = window.innerWidth || document.documentElement.clientWidth;
var viewportHeight = window.innerHeight || document.documentElement.clientHeight;
```
获取文档宽高:
```javascript
var documentWidth = Math.max(document.documentElement.clientWidth, document.body.scrollWidth, document.documentElement.scrollWidth, document.body.offsetWidth, document.documentElement.offsetWidth);
var documentHeight = Math.max(document.documentElement.clientHeight, document.body.scrollHeight, document.documentElement.scrollHeight, document.body.offsetHeight, document.documentElement.offsetHeight);
```
获取文档滚动距离:
```javascript
var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;
var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
```
JQ获取方法:
获取视窗宽高:
```javascript
var viewportWidth = $(window).width();
var viewportHeight = $(window).height();
```
获取文档宽高:
```javascript
var documentWidth = $(document).width();
var documentHeight = $(document).height();
```
获取文档滚动距离:
```javascript
var scrollLeft = $(document).scrollLeft();
var scrollTop = $(document).scrollTop();
```
阅读全文