题目:假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px,中间自适应javascript
方法一:display:flex
#container{
display: flex;
height: 100vh;
}
.left{
width: 300px;
background: red;
}
.content{
flex: 1;
background: darkcyan;
}
.right{
width: 300px;
background: red;
}
<article id="container">
<div class="left"></div>
<div class="content"></div>
<div class="right"></div>
</article>
方法二:浮动
#container{
min-height: 100px;
}
.left{
float: left;
width: 300px;
background: red;
}
.content{
background: darkcyan;
}
.right{
float: right;
width: 300px;
background: red;
}
<article id="container">
<div class="left"></div>
<div class="right"></div>
<div class="content"></div>
</article>
方法三:定位
*{
margin: 0;
padding: 0;
}
.left{
position: absolute;
top: 0;
left: 0;
width: 300px;
height: 100vh;
background: red;
}
.content{
margin: 0 300px;
height: 100vh;
background: darkcyan;
}
.right{
position: absolute;
top: 0;
right: 0;
width: 300px;
height: 100vh;
background: red;
}
<article id="container">
<div class="left"></div>
<div class="right"></div>
<div class="content"></div>
</article>
方法四:表格布局
*{
margin: 0;
padding: 0;
}
#contenter{
display: table;
width: 100%;
min-height: 100px;
}
.left,.center,.right{
display: table-cell;
}
.left{
width: 300px;
background: red;
}
.right{
width: 300px;
background: cornflowerblue;
}
<article id="contenter">
<div class="left"></div>
<div class="center">表格布局</div>
<div class="right"></div>
</article>
方法五:网格布局
*{
margin: 0;
padding: 0;
}
#contenter{
display: grid;
width: 100%;
grid-template-rows: 100px;
grid-template-columns: 300px auto 300px;
}
.left{
background: red;
}
.center{
background: rebeccapurple;
}
.right{
background: cornflowerblue;
}
<article id="contenter">
<div class="left"></div>
<div class="center">表格布局</div>
<div class="right"></div>
</article>
复制代码
flex布局:是比较完美的php
表格布局:兼容性好css
网格布局:新技术,代码量少html
绝对定位解决方案:前端
浮动java
浮动后是脱离文档流的,若是处理很差会带来不少问题web
优势是兼容性比较好面试
CSS盒模型算法
题目:谈谈你对CSS盒模型的认识segmentfault
BFC垂直方法边距重叠
<style> article{ background: blue; overflow: auto; } p{ margin: 5px 0 25px; background: red; } div{ overflow: auto; } </style>
<section>
<article>
<p>1</p>
<div>
<p>2</p>
</div>
<p>3</p>
</article>
</section>
BFC不与float重叠
<style> article{ background: blue; } .left{ width: 100px; height: 100px; float: left; background: red; } .right{ height: 110px; background: #ccc; overflow: hidden; } </style>
<section>
<article>
<div class="left"></div>
<div class="right"></div>
</article>
</section>
BFC子元素即便是float,也会参与高度计算
<style> section{ /*float: left;*/ overflow: auto; background: red; } .float{ float: left; } </style>
<section>
<div class="float">我是浮动元素</div>
</section>
复制代码
DOM事件类
DOM0 element.onclick=function(){}
DOM2 element.addEventListener('click',function(){},false)
DOM3 element.addEventListener('keyup',function(){},false)
DOM3增长了一些事件类型
复制代码
冒泡:从目标元素往上
捕获:从上到下的过程往目标元素
复制代码
浏览器在为当前页面与用户作交互的过程当中,点击鼠标左键,这个左键是如何传到页面上。
一个完整的事件流分三个阶段:
1. 捕获:
2. 目标阶段:事件经过捕获到达目标元素
3. 冒泡阶段:从目标元素再上传到window对象
复制代码
1.window
2.document
3.html:获取html节点:docoment.documentElement
4.body
……
目标元素
复制代码
Event对象的常见应用
event.preventDefault():阻止默认事件
event.stopPropagation():阻止事件冒泡
event.stopImmediatePropagation():阻止事件执行(事件响应优先级)
event.currentTarget:当前所绑定的事件对象
event.target:当前被点击的元素
复制代码
Event('事件名')
<div id="ev">目标元素</div>
<script> var eve=new Event('custome'); ev.addEventListener('custome',function () { console.log('custome'); }); ev.dispatchEvent(eve); </script>
CustomEvent('事件名',obj):能够增长一个obj参数对象
detail:提供有关事件的自定义信息的子对象。
<div id="ev">目标元素</div>
<script> var ev=document.getElementById('ev'); var eve=new CustomEvent('test',{detail:{a:1, b:2}}); ev.addEventListener('test',function (e) { console.log(e.detail.a) }); ev.dispatchEvent(eve); </script>
复制代码
HTTP协议类
原型链类
//对象字面量
var o1={name:'wbq'};
console.log(o1.name);
//new Object
var o2=new Object({name:'wbq2'});
console.log(o2.name);
//构造函数方法
var o3=function (name) {
this.name=name;
}
var result=new o3('wbq3');
console.log(result.name);
//Object.create
var o4=Object.create({name:'wbq4'});
console.log(o4.name);
复制代码
var M=function (name) {
this.name=name;
}
var new2=function (func) {
var o=Object.create(func.prototype);
var k=func.call(o);
if(typeof k==='object'){
return k
}else {
return o
}
}
复制代码
面向对象类
/*类的声明:构造函数方法*/
function Animal(name){
this.name=name;
}
/*ES6中的class的声明*/
class Animal2{
constructor(){
this.name=name;
}
}
复制代码
/*借助构造函数实现继承:只实现部分继承,没法继承父类原型对象的方法*/
function Parent1(){
this.name='parent1';
}
function Child1() {
Parent1.call(this);//继承
this.type='child1';
}
//call,apply改变函数运行上下文,改变this指向
console.log(new Child1());
/*借助原型链实现继承:缺点改第一个对象的属性,第二个对象的属性也跟着改变了*/
function Parent1(){
this.name='parent1';
this.play=[1,2,3];
}
function Child1() {
this.type='child1';
}
Child1.prototype=new Parent1();
console.log(new Child1().play);
var s1=new Child1();
var s2=new Child1();
console.log(s1.play,s2.play);//[1,2,3]f
s2.play.push(4);
console.log(s1.play,s2.play);//[1, 2, 3, 4]
/*借助组合方式实现继承:*/
function Parent1(){
this.name='parent1';
this.play=[1,2,3]
}
function Child1() {
Parent1.call(this);
this.type='child1';
}
Child1.prototype=new Parent1();
var s1=new Child1();
var s2=new Child1();
s2.play.push(4);
console.log(s1.play,s2.play);//[1,2,3],[1, 2, 3, 4]
/*借助组合继承优化方式1:*/
function Parent1(){
this.name='parent1';
this.play=[1,2,3]
}
function Child1() {
Parent1.call(this);
this.type='child1';
}
Child1.prototype=Parent1.prototype;//这样不用两次调用new Parent1()
var s1=new Child1();
var s2=new Child1();
s2.play.push(4);
console.log(s1.play,s2.play);//[1,2,3],[1, 2, 3, 4]
console.log(s1.instanceOf Child1,s1.instanceOf Parent1);//true,true
console.log(s1.constructor);//Parent1,缺点是构造函数指向的是一个,没法区分实例是由父类建立仍是由子类建立的
/*借助组合继承优化方式2:*/
function Parent1(){
this.name='parent1';
this.play=[1,2,3]
}
function Child1() {
Parent1.call(this);
this.type='child1';
}
Child1.prototype=Object.create(Parent1.prototype);//Object.create建立对象的原理:
Child1.prototype.constructor=Child1;
var s1=new Child1();
console.log(s1 instanceof Child1,s1 instanceof Parent1);//true true
console.log(s1.constructor);//Child1
复制代码
通讯类
同源策略:端口、域名、协议相同
同源策略限制从一个源加载的文档或脚本如何与来自另外一个源的资源进行交互。
这是用于隔离潜在恶意文件的关键的安全机制
1. Cookie,LocalStorage和indexDB没法读取
2. DOM没法得到
3. Ajax请求不能发送
复制代码
1.Ajax:同源下的通讯方式
2.WebSocket:不受同源策略限制
3.CORS:支持跨域通讯也支持同源通讯
复制代码
1.XMLHttpRequest对象的工做流程
2.兼容性处理
3.事件的触发条件
4.事件的触发顺序
复制代码
1.JSONP
原理是什么:利用script能够实现异步加载实现的
怎么实现的:script的src连接里要加上一个callback名称。在全局建立一个callback函数
2.Hash:浏览器连接#后面的值
window.localtion.hash
window.onhashchange
利用hash,场景是当前页面A经过iframe或frame嵌入了跨域的页面B
//在A中的代码
var B=document.getElementsByTagName('iframe');
B.src=B.src+'#'+'data';
//在B中的代码以下:
window.onhashchange=function(){
var data=window.location.hash;
};
复制代码
3.postMessage
窗口A向(http:A.com)向跨域的窗口B(http:B.com)发送信息
//在A窗口发送数据:
window.postMessage('data','http:B.com')
//在B窗口中监听message事件:
window.addEventListener('message',function(event){
console.log(event.origin);//http:A.com
console.log(event.source);//Bwindow
console.log(event.data);//data
},false);
复制代码
4.WebSocket
var ws=new WebSocket('wss://echo.websocket.org');
ws.onopen=function(evt){
console.log('Connection open...');
ws.send('Hello WebSocktes!');
ws.onmessage=function(evt){
console.log('Received Message'+evt.data);
ws.close();
}
ws.onclose=function(evt){
console.log('connection closed')
}
}
复制代码
5.CORS
fetch('/some/url',{
method:'get'
}).then(function(response){
}).catch(function(err){
//出错,等价于then的第二个参数,但这样更好用直观
})
http://www.ruanyifeng.com/blog/2016/04/cors.html
复制代码
前端安全类
前端算法类
模拟二面
<meta http-equiv="x-dns-prefetch-control" content="on">
<link rel="dns-prefetch" href="//www.imooc.com">
复制代码
<script crossorigin src="http://www.lmj.com/demo/crossoriginAttribute/error.js"></script>
res.setHeader("Access-Control-Allow-Origin","*");复制代码