BOM、body的位置属性

BOM、body的位置属性

一、BOM

  • BOM,browser object model 浏览器对象模型。用来实现js与浏览器的交互
  • BOM的核心对象是window,是Global对象(全局对象,顶级对象)。
  • 全局变量、函数、对象都会自动成为window对象的属性、方法。document对象也是window对象的属性
  • 全局变量自动成为window对象的属性
  •       var a=10;//全局变量会被追加为window对象的属性
          console.log(a);
          console.log(window.a);//a变为window对象一个属性
    
  • 函数自动成为window对象的方法
  •       var a=10;//全局变量会被追加为window对象的属性
          console.log(a);
          console.log(window.a);//a变为window对象一个属性
    
1.window对象的方法
  • alert()           警告框
  • prompt()       输入框
    确定按钮      返回输入框内容
    取消按钮      返回null
  • confirm()      确认框
    确定      返回true
    取消      返回false
  • open() 打开窗口
    参数:
    第一个参数:地址
    第二个参数:打开模式 _blank(空白页)_self (当前页)
    第三个参数:打开的窗口的样式
open('http://www.baidu.com','_blank',"width=300px,height=300px,left=300px,top=200px");
  • close() 关闭窗口
        btn.onclick=function(){
            close();
        }
2.location对象

该对象中包含了与URL相关的信息

  • href                   url地址
  • protocol            协议
  • host/hostname 主机名
        console.log(location.protocol);
        //协议 http:
        console.log(location.host);
        //主机名 localhost:52332
        console.log(location.hostname);
        //主机名 localhost
        console.log(location.href);
        //url地址 http://localhost:52332/2.window.html
  • port                   端口号
  • search              查询字符串
3.history历史记录对象
  • back()
  •         <button>后退</button>
            document.querySelector('button').onclick=function(){
              // history.back();//回到历史列表中的上一个页面
              history.go(-1);//回到历史列表中的上一个页面
          }
          history.back()与history.go(-1)等价
    
  • forward()
  •     <button>前进</button>
          document.querySelector('#btn3').onclick=function(){
              // history.forward();//回到历史列表中的下一个页面
              history.go(1);//回到历史列表中的下一个页面
          };
          history.forward()与history.go(1)等价
    
  • go()
    参数数字 1或-1,前进和后退
4、Navigator

该对象中保存了与浏览器相关的信息

        console.log(window.navigator.appName);
        //返回浏览器的名称
        console.log(window.navigator.appVersion);
        //返回浏览器的平台和版本信息
        console.log(navigator.cookieEnabled);
        //返回指明浏览器中是否启用 cookie 的布尔值
        console.log(window.navigator.platform);
        //返回运行浏览器的操作系统平台

二.body的位置属性

1.client系列
  • 元素可视宽高
    clientWidth      width+padding
    clientHeight     height+padding
  • 屏幕可视区域的宽高
    document.documentElement.clientWidth
    document.documentElement.clientHeight
  • 上边框和左边框的大小:
    clientTop
    clientLeft
    <style>
        div {
            width: 100px;
            height: 100px;
            border: 10px solid gray;
            background: greenyellow;
            padding: 5px;
            margin-left: 10px;
        }
    </style>
    <script>
        var o=document.querySelector('div');
        console.log(o.clientWidth);
        //可视宽度  110
        console.log(o.clientHeight);
        //可视高度  110
        
        console.log(document.body.clientHeight);
        //130  被内容撑起来的高度,不包括外边距
        console.log(document.body.clientWidth);
        //1350  被内容撑起来的宽度,不包括外边距
        
        //可视区域的宽高
        console.log(document.documentElement.clientHeight);
        //可视屏幕的高度
        console.log(document.documentElement.clientWidth);  
        //1366 可视屏幕的宽度
        console.log(o.clientLeft);//10 上边框的大小
        console.log(o.clientTop);//10 左边框的大小
    </script>
2.offset系列
  • 元素占位的大小
    offsetWidth     width+padding+border
    offsetHeight    height+padding+border
  • 元素在页面中的位置
    offsetLeft:元素到定位的父元素的left值,若没有定位,则到body的left
    offsetTop:元素到定位的父元素的top值,若没有定位,则到body的top值
    <style>
        div {
            width: 100px;
            height: 100px;
            border: 10px solid gray;
            background: greenyellow;
            padding: 5px;
            position: absolute;
            left: 100px;
            top: 50px;
            
        }
    </style>
    <script>
        console.log(o.offsetWidth);//130
        console.log(o.offsetHeight);//130
        console.log(o.offsetLeft);//100
        console.log(o.offsetTop);//50
    </script>
3.scroll系列
  • 滚动事件
    window.onscroll
  • 属性
    scrollTop        被卷去的高
    scrollLeft        被卷去的宽
    scrollWidth     滚动区域的宽度(实际内容宽)
    scrollHeight    滚动区域的高度(实际内容高)
  • 页面滚动的高度
    document.documentElement.scrollTop
    document.body.scrollTop
  • 4 回到顶部案例
    <style>
        body{
            height: 2000px;
            background-image:linear-gradient(red,yellow,teal);
        }
        button{
            position:fixed;
            right:0;
            bottom:0;
            display:none;
        }
    </style>
</head>
<body>
    <button>回到顶部</button>
    <script>
        var btn = document.querySelector("button");
        window.onscroll = function(){
            var h = document.documentElement.scrollTop||document.body.scrollTop;
            console.log(h);
            if(h<=800){//被卷去的距离
                btn.style.display = "none";
            }else{
                btn.style.display = "block";
            }
        }

        btn.onclick = function(){//回到顶部
            var h = document.documentElement.scrollTop||document.body.scrollTop;
            var timer = setInterval(function(){
                h -= 10;
                if(h <= 0){
                    h = 0;
                    clearInterval(timer);
                }
                document.documentElement.scrollTop = h;
            },10);
        }
    </script>
</body>
  • 懒加载
    <style>
        img {
            height: 480px;
            width: 830px;
        }
    </style>
</head>

<body>
    <img _src="http://www.ujiuye.tech:3000/image_source/home_loop_img/32407805826e4adbb35d03cb82afd0e8.png" alt="">
    <img _src="http://www.ujiuye.tech:3000/image_source/home_loop_img/158217880112765841.jpg" alt="">
    <img _src="http://www.ujiuye.tech:3000/image_source/home_loop_img/158218554377624177.jpg" alt="">
    <img _src="http://www.ujiuye.tech:3000/image_source/home_loop_img/158218605663337136.jpg" alt="">
    <img _src="http://www.ujiuye.tech:3000/image_source/home_loop_img/158224870698497558.jpg" alt="">
    <img _src="http://www.ujiuye.tech:3000/image_source/home_loop_img/158224891879191249.jpg" alt="">
    <img _src="http://www.ujiuye.tech:3000/image_source/home_loop_img/158225066085716872.jpg" alt="">
    <img _src="http://www.ujiuye.tech:3000/image_source/home_loop_img/158233428702116474.jpg" alt="">

    <script>
        var imgs = document.querySelectorAll('img');//获取所有图片
        var viewHeight = document.documentElement.clientHeight || document.body.clientHeight;
        //获取视口的高度
        run();
        function run(){
            var jHeight = document.documentElement.scrollTop || document.body.scrollTop;
            //获取卷去的高度
            for(var i = 0;i < imgs.length;i++){
                var imgHeight = imgs[i].offsetTop;//获取图片到顶部的距离

                // console.log(imgHeight);
                console.log(jHeight);

                if(imgHeight <= viewHeight+jHeight){
                    imgs[i].src = imgs[i].getAttribute('_src');
                }
            }
        }

        window.onscroll = function(){
            run();
        }
    </script>
</body>
  • resize事件
        window.onresize = function () {
            console.log(1);
            //浏览器窗口发生改变时触发
        };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DOM节点的操作和属性 1. 常用鼠标与键盘事件: ```javascript // 鼠标事件 element.addEventListener('click', function(event) { // 点击事件 }); element.addEventListener('mousedown', function(event) { // 按下鼠标键事件 }); element.addEventListener('mouseup', function(event) { //开鼠标键事件 }); element.addEventListener('mousemove', function(event) { // 鼠标移动事件 }); element.addEventListener('mouseover', function(event) { // 鼠标移入事件 }); element.addEventListener('mouseout', function(event) { // 鼠标移出事件 }); // 键盘事件 element.addEventListener('keydown', function(event) { // 按下键盘事件 }); element.addEventListener('keyup', function(event) { // 松开键盘事件 }); element.addEventListener('keypress', function(event) { // 按键事件 }); ``` 2. 事件对象的绑定与使用: ```javascript element.addEventListener('click', function(event) { // 阻止默认行为 event.preventDefault(); // 阻止事件冒泡 event.stopPropagation(); // 获取事件目标 event.target; // 获取事件类型 event.type; // 获取触发事件的元素 event.currentTarget; }); ``` 3. 排他操作: ```javascript const elements = document.querySelectorAll('.item'); elements.forEach(function(element) { element.addEventListener('click', function(event) { // 移除所有元素的 active 类 elements.forEach(function(item) { item.classList.remove('active'); }); // 添加当前元素的 active 类 element.classList.add('active'); }); }); ``` 4. Bom获取窗体属性: ```javascript // 获取窗口宽度 window.innerWidth; // 获取窗口高度 window.innerHeight; // 获取文档宽度 document.documentElement.clientWidth; // 获取文档高度 document.documentElement.clientHeight; ``` 5. Bom相关属性的使用: ```javascript // 获取当前页面的url window.location.href; // 获取当前页面的域名 window.location.hostname; // 获取当前页面的路径 window.location.pathname; // 获取当前页面的协议 window.location.protocol; ``` 6. Bom相关方法的使用: ```javascript // 跳转到指定url window.location.href = 'https://www.example.com'; // 刷新页面 window.location.reload(); // 后退一页 window.history.back(); // 前进一页 window.history.forward(); ``` 7. Offset的使用: ```javascript // 获取元素距离文档顶部的距离 element.offsetTop; // 获取元素距离文档左侧的距离 element.offsetLeft; // 获取元素的宽度 element.offsetWidth; // 获取元素的高度 element.offsetHeight; ``` 8. DOM节点的操作和属性: ```javascript // 创建元素节点 const element = document.createElement('div'); // 设置元素属性 element.setAttribute('id', 'my-div'); element.setAttribute('class', 'my-class'); // 获取元素属性 element.getAttribute('id'); element.getAttribute('class'); // 添加元素到文档中 document.body.appendChild(element); // 删除元素 document.body.removeChild(element); // 获取元素的子元素 element.children; // 获取元素的父元素 element.parentNode; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值