js获取网页滚动条高度的语法
时间: 2023-10-15 12:08:12 浏览: 43
要获取网页滚动条的高度,可以使用JavaScript代码中的`document.documentElement.scrollTop`或`document.body.scrollTop`属性,具体取决于网页的具体结构。以下是两种方法的示例:
1. `document.documentElement.scrollTop`:
```
var scrollTop = document.documentElement.scrollTop;
```
这将获取当前网页文档的滚动条高度,不包括浏览器工具栏和菜单栏的高度。
2. `document.body.scrollTop`:
```
var scrollTop = document.body.scrollTop;
```
这将获取当前网页文档的滚动条高度,包括浏览器工具栏和菜单栏的高度。
注意,不同浏览器可能有不同的实现方式,因此建议在具体项目中根据实际情况选择使用哪种方法。
相关问题
获取iframe滚动条高度
要获取iframe中滚动条的高度,可以使用以下代码:
```javascript
var iframe = document.getElementById("myIframe");
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
var scrollHeight = innerDoc.documentElement.scrollHeight || innerDoc.body.scrollHeight;
var clientHeight = innerDoc.documentElement.clientHeight || innerDoc.body.clientHeight;
var scrollbarHeight = scrollHeight - clientHeight;
```
其中,`myIframe` 是iframe的id,`scrollHeight`是iframe内部网页的总高度,`clientHeight`是iframe可见部分的高度,`scrollbarHeight`是滚动条的高度。
vue 获取当前滚动条高度
你可以通过以下代码获取 Vue 中当前滚动条的高度:
```
computed: {
scrollTop() {
return document.documentElement.scrollTop || document.body.scrollTop;
}
}
```
其中 `document.documentElement.scrollTop` 和 `document.body.scrollTop` 分别用于兼容不同浏览器的滚动条高度获取方式。