BOM介绍及应用

01-BOM顶级对象

<!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>BOM顶级对象</title>
</head>

<body>
    <script>
        // window.document.querySelector();
        // var num = 10;
        // console.log(num); //10
        // console.log(window.num); //10
        function fn() {
            console.log(11);
        }
        //fn();
        //window.fn();
        // alert(11);
        // window.alert(11);
        // console.dir(window);
        //var name = 10;
        console.log(window.name); //特殊属性 一般不用name作为变量名
    </script>
</body>

</html>

02-window常见事件onload

<!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>window常见事件onload</title>
</head>

<body>
    <script>
        // window.onload = function() {
        //     var btn = document.querySelector('button');
        //     btn.addEventListener('click', function() {
        //         alert('点击我');
        //     })
        // }
        // window.onload = function() { //以上事件和这个事件只执行这个当前的事件,覆盖原有的事件处理程序
        //     alert(22);
        // }
        window.addEventListener('load', function() {
            var btn = document.querySelector('button');
            btn.addEventListener('click', function() {
                alert('点击我');
            })
        })
        window.addEventListener('load', function() {
            alert(23);
        })
        document.addEventListener('DOMContentLoaded', function() {
                alert(33);
            })
            // 两者区别
            // load 等页面内容全部加载完毕,包含页面dom元素 图片 flash  css 等等
            // DOMContentLoaded 是DOM 加载完毕,不包含图片 falsh css 等就可以执行 加载速度比 load更快一些
    </script>
    <button>点击</button>

</body>

</html>

03-调整窗口大小事件

<!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>调整窗口大小事件</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) { //如果屏幕尺寸小于等于800
                    div.style.display = 'none'; //div这个盒子隐藏
                } else {
                    div.style.display = 'block';
                }
            })
        })
    </script>
    <div></div>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值