1. 程式人生 > 其它 >JS之調整視窗大小事件

JS之調整視窗大小事件

<!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>Document</title>
    <style>
        div 
{ width: 200px; height: 200px; background-color: pink; } </style> </head> <body> <script> window.addEventListener('load', function() { var div = document.querySelector('div'); window.addEventListener(
'resize', function() { console.log(window.innerWidth); console.log('變化了'); if (window.innerWidth <= 800) { div.style.display = 'none'; } else { div.style.display = 'block'; } }) })
</script> <div></div> </body> </html>