中间自适应布局的5种解法

前言 在做页面时,我们往往会碰到页面布局相关的内容,面试时也经常会被问到,那么今天我就来总结一下关于页面布局的内容。问题:如何实现三栏布局(高度固定,左中右的结构) 假设高度已知,请写出三栏布局,其中左右宽度均为300px,中间自适应。 看了上面的题目,有经验的人也许会觉得很简单,仔细想想,如果我们来写,能写出几种方案呢?一般都会想到两种吧,float和position定位,其实除了这两种外,还有3种可以写,下面我就来一一介绍一下:方案一(float浮动)<section class=‘layout float’> <style> .layout.float .left-right-center{ height: 100px; } .layout.float .left{ float: left; width: 300px; background-color: red; } .layout.float .right{ float: right; width: 300px; background-color: blue; } .layout.float .center{ background-color: yellow; } </style> <article class=“left-right-center”> <div class=“left”></div> <div class=“right”></div> <div class=“center”>我是中间的自适应元素–浮动</div> </article> </section>原理:左右两个div由于浮动脱离了文档流,center就会上移,造成三栏布局的效果(前提是高度相同)优点:兼容性高缺点:需要清除浮动来防止影响其他元素如果高度不固定,中间的内容会被撑开,左右两边不会一起撑开方案二(绝对定位)<section class=“layout absolute”> <style> .layout.absolute .left-center-right div{ position: absolute; height: 100px; } .layout.absolute .left{ left: 0; width: 300px; background-color: red; } .layout.absolute .center{ left: 300px; right: 300px; background-color: yellow; } .layout.absolute .right{ right: 0; width: 300px; background-color: blue; } </style> <article class=“left-center-right”> <div class=“left”></div> <div class=“center”> 我是中间的自适应元素–绝对定位 </div> <div class=“right”></div> </article> </section>原理:利用绝对定位以及宽度,将左右两边的div固定住,中间div的宽度就会有自适应的效果优点:快捷缺点:如果父元素脱离了文档流,子元素一定会脱离文档流,运用的场景不多如果中间元素的高度增加,两边元素的高度不会增加,所以只有中间的div会撑开方案三(flex布局)<section class=“layout flex”> <style> .layout.flex .left-center-right{ display: flex; height: 100px; } .layout.flex .left{ width: 300px; background-color: red; } .layout.flex .center{ flex: 1; background-color: yellow; } .layout.flex .right{ width: 300px; background-color: blue; } </style> <article class=“left-center-right”> <div class=“left”></div> <div class=“center”> 我是中间的自适应元素–flex布局 </div> <div class=“right”></div> </article> </section>原理:将父元素设置为flex布局,然后中间元素设置flex为1,达到自适应的效果优点:在实际开发中常用缺点:IE8及以下的浏览器不支持如果高度不固定,中间内容的高度撑开后,两边也会随之撑开方案四(table布局) <section class=“layout table”> <style> .layout.table .left-center-right{ display: table; height: 100px; width: 100%; } .layout.table .left{ display: table-cell; width: 300px; background-color: red; } .layout.table .center{ display: table-cell; background-color: yellow; } .layout.table .right{ display: table-cell; width: 300px; background-color: blue; } </style> <article class=“left-center-right”> <div class=“left”></div> <div class=“center”> 我是中间的自适应元素–table </div> <div class=“right”></div> </article> </section>原理:将父元素设置为table布局,然后每个子元素都是teble-cell,给左右两个格子设置固定的宽度,中间的格子就可以达到自适应的效果优点:兼容性好,可做flex布局在ie8以下的代替缺点:局限性如果高度不固定,中间被撑开时,左右两边也会被撑开,和flex类似方案五(网格布局)<section class=“layout grid”> <style> .layout.grid .left-center-right{ display: grid; width: 100%; grid-template-rows: 100px;/每一格都是100px高/ grid-template-columns: 300px auto 300px;/左右两格都是300px,中间是自适应/ } .layout.grid .left{ background-color: red; } .layout.grid .center{ background-color: yellow; } .layout.grid .right{ background-color: blue; } </style> <article class=“left-center-right”> <div class=“left”></div> <div class=“center”> 我是中间的自适应元素–grid布局 </div> <div class=“right”></div> </article> </section> 原理:将父元素设置为网格布局,然后规定每格的高度以及每格的宽度,只用分别给每格单独设置颜色即可优点:技术比较新,方便缺点:兼容性不是很好如果高度不固定,中间元素添加文本,也不会撑开 ...

January 20, 2019 · 2 min · jiezi

网页标准文档流中垂直方向的margin塌陷现象

在标准文档流中(即没有浮动,如果设置了浮动那就不会出现这中情况了),body中有两个div元素,这两个div元素的宽、高都是200px,第一个div设置了margin-bottom:t0px;第二个div设置了margin-top:20px,那么实际上两个div元素之间的距离是50px,这就是margin塌陷现象,并且margin塌陷还是以大的一方为准。如图片:<style> body,div{ margin: 0; padding: 0; } .box{ width: 200px; height: 100px; margin-left: 100px; color: #fff; font-weight: 600; } .box1{ background-color: #f10; margin-bottom: 50px; } .box2{ background-color: #f60; margin-top: 20px; }</style><body> <div class=“box box1”> margin-bottom: 50px; </div> <div class=“box box2”> margin-top: 20px; </div></body>

January 4, 2019 · 1 min · jiezi

解决vue多个路由共用一个页面的问题

本次为大家分享一篇解决vue多个路由共用一个页面的问题,写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学习下。如有不足之处,欢迎批评指正。在日常的vue开发中我们可能会遇见多个路由需要共用一个页面的需求,特别是当路由是通过动态添加的,不同的路由展示的东西只是数据不同其他没有变化。例如:let routes = [ {path:"/zhanshan", components:Person, }, {path:"/lisi", components:Person, }, {path:"/wangwu", components:Person, }]//欢迎加入前端全栈开发交流圈一起学习交流:864305860这种情况的时候,我们发现,其实我们的页面在第一次加载成功后就不会再加载了。所以页面一直显示第一次加载的数据,给人的赶脚好像路由没有生效,而我们通过观察浏览器地址栏中的变化可以确定的是这和路由没关系,这对刚刚开始使用的vue的同学可能会产生一点点困扰,其实这和页面的声明周期是相关的,这种情况出现的原因是因为页面在加载后他的大多数钩子函数(mounted,computed…)就不会再次出发了,所以导致页面感觉没有跳转。一道这种业务需求其实也比较好处理,其实我们不需要页面切换,我们只需要页面中的数据发生改变就好了,我们可以在页面中监听路由地址的变化,当地址变化的时候,我们就重新加载数据。watch:{ “$route”:function(to,from){ //from 对象中包含当前地址 //to 对象中包含目标地址 //其实还有一个next参数的//这个参数是控制路由是否跳转的//如果没写,可以不用写next()来代表允许路由跳转,如果写了就必须写next(),否则路由是不会生效的。 }//欢迎加入前端全栈开发交流圈一起学习交流:864305860}每当路由发生变化的时候上面的函数都会被触发,我们可以在这个函数中对页面的数据进行重新加载的操作。如果页面结构变化很大,还是建议单独新建一个不同的页面。上面的问题可能对新入门vue的朋友有用,对vue比较了解了的朋友请绕过。结语感谢您的观看,如有不足之处,欢迎批评指正。

December 18, 2018 · 1 min · jiezi