• JavaScript基础(四)


    十六、client、offset、scroll系列

    1、client系列

    代码如下:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style type="text/css">
                .box{
                    width: 200px;
                    height: 200px;
                    position: absolute;
                    border: 10px solid red;
                    /*margin: 10px 0px 0px 0px;*/
                    padding: 80px;
                }
            </style>
        </head>
        <body>
            <div class="box">
                哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
            </div>
        </body>
        <script type="text/javascript">
            /*
             *   clientTop 内容区域到边框顶部的距离 ,说白了,就是边框的高度
             *      clientLeft 内容区域到边框左部的距离,说白了就是边框的乱度
             *      clientWidth 内容区域+左右padding   可视宽度
             *      clientHeight 内容区域+ 上下padding   可视高度
             * */
            
            var oBox = document.getElementsByClassName('box')[0];
            console.log(oBox.clientTop);
            console.log(oBox.clientLeft);
            console.log(oBox.clientWidth);
            console.log(oBox.clientHeight);   
            
        </script>
        
    </html>
    client

    2.屏幕的可视区域

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
        </body>
        <script type="text/javascript">
            
            // 屏幕的可视区域
            window.onload = function(){
                
                // document.documentElement 获取的是html标签
                console.log(document.documentElement.clientWidth);
                console.log(document.documentElement.clientHeight);
                // 窗口大小发生变化时,会调用此方法
                window.onresize = function(){    
                    console.log(document.documentElement.clientWidth);
                    console.log(document.documentElement.clientHeight);
                }
            }
        </script>
    </html>
    View Code

    3.offset系列

    代码如下,注释都挺清楚的

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style type="text/css">
                *{
                    padding: 0;
                    margin: 0;
                }
            </style>
            
        </head>
        <body style="height: 2000px">
            <div>
                <div class="wrap" style="  300px;height: 300px;background-color: green">
                    <div id="box" style=" 200px;height: 200px;border: 5px solid red;position: absolute;top:50px;left: 30px;">            
                    </div>
                </div>
            </div>
        </body>
        <script type="text/javascript">
            window.onload = function(){
                var box = document.getElementById('box')
                /*
                 offsetWidth占位宽  内容+padding+border
                 offsetHeight占位高 
                 * offsetTop: 如果盒子没有设置定位 到body的顶部的距离,如果盒子设置定位,那么是以父辈为基准的top值
                 * offsetLeft: 如果盒子没有设置定位 到body的左部的距离,如果盒子设置定位,那么是以父辈为基准的left值
                 
                 * */
                console.log(box.offsetTop)
                console.log(box.offsetLeft)
                console.log(box.offsetWidth)
                console.log(box.offsetHeight)
                
            }
            
        </script>
    </html>
    View Code

    4.scroll系列

    代码如下:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="UTF-8">
      <title></title>
      <style type="text/css">
        *{padding: 0;margin: 0;}
      </style>
    </head>
    <body style=" 2000px;height: 2000px;">
      <div style="height: 200px;background-color: red;"></div>
      <div style="height: 200px;background-color: green;"></div>
      <div style="height: 200px;background-color: yellow;"></div>
      <div style="height: 200px;background-color: blue;"></div>
      <div style="height: 200px;background-color: gray;"></div>
      <div id = 'scroll' style=" 200px;height: 200px;border: 1px solid red;overflow: auto;padding: 10px;margin: 5px 0px 0px 0px;">
        <p>哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
          哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
          哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
          哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
        </p>    
      </div>
    </body>
    <script type="text/javascript">
      window.onload = function(){
      //实施监听滚动事件
      window.onscroll = function(){
    //                console.log(1111)
    //                console.log('上'+document.documentElement.scrollTop)
    //                console.log('左'+document.documentElement.scrollLeft)
    //                console.log('宽'+document.documentElement.scrollWidth)
    //                console.log('高'+document.documentElement.scrollHeight)
      }
                
      var s = document.getElementById('scroll');
      s.onscroll = function(){
    //            scrollHeight : 内容的高度+padding  不包含边框
      console.log(''+s.scrollTop)
      console.log(''+s.scrollLeft)
      console.log(''+s.scrollWidth)
      console.log(''+s.scrollHeight)
        }
      }
      </script>
    </html>                    
  • 相关阅读:
    四招打造复合型人才
    Eclipse多国语言包的安装
    怎样和人配合(转)
    看海外如何解困建筑信息化(转)
    SSRS重装后不能在SSMS和IIS中打开,报Unauthorized错误的解决办法
    小心调用Replicator, While 和 CAG子活动
    自动备份 SourceSafe
    SQLServer2005新增序列功能的几个函数:ROW_NUMBER, RANK, DENSE_RANK, and NTILE
    分析物语
    定位.Net程序集文件
  • 原文地址:https://www.cnblogs.com/shanae/p/9719415.html
Copyright © 2020-2023  润新知