screen对象包含有关客户端显示屏幕的信息screen.availWidth // 屏幕的宽度screen.availHeight // 屏幕的高度screen.height // 屏幕分辨率的高screen.width // 屏幕分辨率的宽
// 获取屏幕的宽度var screenWidth = window.screen.availWidth;console.log(screenWidth);// 获取屏幕的可视区var viewWidth = document.body.clientWidth;console.log(viewWidth);/*获取屏幕可视区域高度*/var vh = document.documentElement.clientHeight;console.log(vh);
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *{margin: 0;padding: 0;} body{height: 2000px;} div{width: 300px;height: 1700px;background-color: lightcoral;} </style></head><body> <div></div> <script> var bodyH = document.body.clientHeight; var vh = document.documentElement.clientHeight; console.log(bodyH); console.log(vh); /** * 滚动条滚动距离 + vh = bodyH */ window.onscroll = function(){ var top = window.scrollY; console.log(top); } /*top + vh == bodyH 判断滚动条到达底部*/ </script></body></html>