js获取窗口大小并监听窗口变化

demo: 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>onresize</title>
</head>
<body>
    <div>
        我是页面的内容
    </div>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script>
        window.onload = function(){
            console.log('页面加载完毕')
            watchChangeSize();

        }
        window.onresize=function(){  
            console.log('监听变化')
            watchChangeSize();
        } 

        function watchChangeSize (){
            //可视区的宽/高(DOM)
            //offsetHeight(带边框)和clientHeight(不带边框)区别参考上一篇文章     
            var offsetWid = document.documentElement.clientWidth;
            var offsetHei = document.documentElement.clientHeight;
            console.log(offsetWid);
            console.log(offsetHei);
        }
    </script>
</body>
</html>

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2021-06-07
  • 2021-11-27
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
  • 2021-09-13
  • 2022-12-23
  • 2021-11-29
相关资源
相似解决方案