scroll2
Lightweight, cross-browser and highly customizable animated scrolling
install
yarn add scroll2
usage
scroll2(scrollY = 0, duration = 1000, callback)
param | type | description |
---|---|---|
scrollY | Number | scroll end position |
duration | Number | scroll duration, default 1000 |
callback | Function | callback when scroll ended |
const scroll2 = ;
使用 javascript 获取视口/窗口大小(宽度和高度)
使用 jQuery 方法获取窗口或文档的大小
height // returns height of browser viewportheight // returns height of HTML document (same as pageHeight in screenshot)width // returns width of browser viewportwidth // returns width of HTML document (same as pageWidth in screenshot)
使用原生 js 方法获取窗口的大小
长版本
短版本
返回对象
{ var e = window a = 'inner' if !'innerWidth' in window a = 'client' e = documentdocumentElement || documentbody return width: ea + 'Width' height: ea + 'Height' }
返回变量
var w = window d = document e = ddocumentElement g = d0 x = winnerWidth || eclientWidth || gclientWidth y = winnerHeight || eclientHeight || gclientHeightconsole
使用原生 js 方法获取文档的大小
const body = documentbodyconst html = documentdocumentElement let height = Math
获取页面可滚动区域高度
{ let body = documentbody let html = documentdocumentElement let height = Math let viewportHeight = windowinnerHeight || documentdocumentElementclientHeight || document0clientHeight return height > viewportHeight ? height - viewportHeight : 0}
获取网页元素的绝对位置
{ var actualLeft = elementoffsetLeft; var current = elementoffsetParent; while current !== null actualLeft += currentoffsetLeft; current = currentoffsetParent; return actualLeft;} { var actualTop = elementoffsetTop; var current = elementoffsetParent; while current !== null actualTop += currentoffsetTop; current = currentoffsetParent; return actualTop;}
阅读链接
- stackoverflow: get-the-size-of-the-screen-current-web-page-and-browser-window
- Get viewport/window size (width and height) with javascript
- How to get height of entire document with JavaScript?
- 用Javascript获取页面元素的位置