CodeLab
Show Output
 
xxxxxxxxxx
29
 
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="utf-8">
5
<title>JavaScript Window Resize Event</title>
6
</head>
7
<body>
8
    <div id="result"></div>
9
 
10
    <script>        
11
    // Defining event listener function
12
    function displayWindowSize(){
13
        // Get width and height of the window excluding scrollbars
14
        var w = document.documentElement.clientWidth;
15
        var h = document.documentElement.clientHeight;
16
        
17
        // Display result inside a div element
18
        document.getElementById("result").innerHTML = "Width: " + w + ", " + "Height: " + h;
19
    }
20
     
21
    // Attaching the event listener function to window's resize event
22
    window.addEventListener("resize", displayWindowSize);
23
    
24
    // Calling the function for the first time
25
    displayWindowSize();
26
    </script>
27
    <p><strong>Note:</strong> Please resize the browser window to see how it works.</p>
28
</body>
29
</html> 
 

在微博、微信、公众号、小程序分享此示例。 如果您觉得有帮助,请给我们一个赞。