• 一道引人深思的前端面试题


    这绝对是一道值得深思的题目,帮你明白布局的死角。

    <!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>
          * { margin: 0; padding: 0; }
          html, body, #app { margin: 0; padding: 0; height: 100%; }
          #header, #footer {
            height: 50px;
            line-height: 50px;
            text-align: center;
            background: #555;
            color: #fff;
          }
          #side {  200px; background: #eee; }
    
            /*css here*/
        </style>
      </head>
      <body>
        <div id="app">
          <header id="header">header</header>
          <aside id="side">side</aside>
          <div id="main">
            <ul>
              <li><a href="https://www.bilibili.com/1">Link1</a></li>
              <li><a href="https://www.bilibili.com/1">Link2</a></li>
              <li><a href="https://www.bilibili.com/1">Link3</a></li>
              <br>
              <li><a href="https://www.bilibili.com/1">Link4</a></li>
              <li><a href="https://www.bilibili.com/1">Link5</a></li>
            </ul>
          </div>
          <footer id="footer">footer</footer>
        </div>
        <script>
          // JS here
        </script>
      </body>
    </html>
    

    1、完成经典的上 header ,下 footer ,左边是侧边栏,右边是内容。
    2、去掉列表前面的 · ,列表项水平排列,注意里面的br标签需要换行,同时每两个li后有一条竖线。
    3、点击列表项不跳转,弹出href内的内容。

    完成第一问

    float的方式

       /* css here */
    #side{
      float: left;
      height: calc(100% - 100px);
    }
    #main{
      margin-left: 200px;
      height: calc(100% - 100px);
    }
    

    思考:(这里是精髓)我的意识当中 假如一行两个块元素,其中一个元素float:left,那么两个元素在一行,并且重叠,将内容挤出来,其实不然, 第一个元素float:left,第二个元素会与第一个元素重叠,也就是在一行,但是如果是第二个元素float,第一个元素是块元素会独占一行,第二个float元素会被挤到第二行(重重重,以前不知道),所以上面题的第一问,header为块元素独占一行, 而aside为浮动元素,main元素会挤上来,并且重叠(重叠问题可以使用bfc或者直接用margin)

    定位的方式

    我的解决方式,是解决了问题,可是都不算完美

    #app{
      position: relative;
    }
    #side{
      position: absolute;
      left: 0;
      top: 50px;
      height: calc(100% - 100px);
    }
    #main{
      margin-left: 200px;
      height: calc(100% - 100px);
    }
    

    更完美的的方式(或许不能称更完美,只是想到了一些平时布局不会去用的方式,而它却比较使用)

    /* css here */
            #app{
                position: relative;
            }
            #side{
                position: absolute;
                left: 0;
                top: 50px;
                bottom: 50px;
            }
            #main{
                position: absolute;
                left: 200px;
                top: 50px;
             	right: 0; 
                bottom: 50px;
            }
            #footer {
            /* footer 设置 绝对定位 */
                position: absolute;
                bottom: 0;
                 100%; /* 设置浮动后,补上宽度 */
            }
    

    思考:绝对定位同时给left和right或者top和bottom可以解决宽度和高度问题(宽高不定,margin正负值,定位等都能影响宽高)。绝对定位之后块元素的宽高将根据内容撑开。

    flex方式

    /*css here*/
    #app{
        display: flex;
        flex-wrap: wrap;
    }
    #header, #footer {
         100%;
    }
    #side{
        height: calc(100% - 100px);
    }
    #main{
        height: calc(100% - 100px);
        flex: 1;
    }
    

    grid方式

    暂无

    完成第二问

    #main li{
      list-style: none;
      float: left;
    }
    #main li:nth-of-type(2n){
      border-right: 1px solid #000;
    }
    

    思考:nth-of-type() 和 nth-child()的区别,为什么使用nth-child()不行。

    完成第三问

    document.querySelectorAll('ul')[0].addEventListener('click',event => {            
      if(event.target.tagName === 'A') { 
        event.preventDefault(); // 阻止默认行为 alert(event.target.getAttribute('href'));
      }
    });
    

    思考:事件委托

  • 相关阅读:
    Docker容器彻底删除所有容器、删除所有镜像、删除所有卷、删除所有网络
    Fabric区块链浏览器启动报错Error : [ 'Explorer is closing due to channel name [%s] is already exist in DB'...]
    查看docker里面的Postgres数据库里面的信息
    将本地镜像推送到指定docker服务器
    linux 下 配置C++ 开发环境
    Go 发送邮件
    Ubuntu下使用nginx发布vue项目
    C++多线程之条件变量
    C/C++ 递归
    STL容器概述
  • 原文地址:https://www.cnblogs.com/chenlei987/p/10541961.html
Copyright © 2020-2023  润新知