My Block


  • Startseite

  • Archiv

CSS进阶

Veröffentlicht am 2018-05-11

1. 父亲儿子的上下margin合并

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<head>
<style>
.child{
height: 100px;
border: 1px solid red;
margin-top: 100px;
}
.parent{
width: 100%;
background: green;
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>

这样儿子的margin-top会给到父亲,把父亲挤下来,但只要在父亲里加:

  • border: 0.1px solid green;
  • padding: 0.1px
  • display: table;
  • display: flex;(不加width的话会把width变成0)
  • display: inline-block(不加width的话会把width变成0)

则上下margin不会合并,父亲还是会把儿子包起来

2. 对齐中文

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<head>
<style>
span{
display: inline-block;
text-align: justify;
width: 5em;
overflow: hidden;
height: 20px;
border: 1px solid red;
}
span::after{
content: '';
display: inline-block;
width: 100%;
border: 1px solid blue;
}
</style>
</head>
<body>
<span>姓名</span> <br>
<span>联系方式</span>
</body>

3. 文字溢出变省略

  1. 显示一行

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <style>
    div{
    border: 1px solid red;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    }
    </style>
    <body>
    <div>
    只显示一行,超出一行的用省略号替代....
    </div>
    </body>
  2. 显示任意多少行(不兼容iE)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <style>
    div{
    border: 1px solid red;
    display: -webkit-box; //-webkit不兼容IE
    -webkit-line-clamp: 1; //只显示1行
    -webkit-box-orient: vertical;
    overflow: hidden;
    }
    </style>
    <body>
    <div>
    文字文字文字....
    </div>
    </body>

4. 文字绝对居中

不要给div加height,直接用padding当作高度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<style>
div{
border: 1px solid red;
/* 写line-height,不要写height */
line-height: 24px; /* div高为line-height+2*padding */
padding: 8px 0; /* 垂直居中 */
text-align: center; /* 水平居中 */
}
</style>
<body>
<div>
文字文字文字....
</div>
</body>

5. 文档流

1. div高度
综1和4所述,div高度是由div内部文档流元素高度总和决定的,文档流中内联元素从左到右依次排列若空间不够则换行,文档流中块级元素从上到下依次排列

  1. 脱离文档流
  • float
  • position: absolute
  • position: fixed
  1. 相对定位(relative)
    position:relative不会脱离文档流,给相对定位的元素设置top和left,会相对它之前在的位置变化,但它之前占的位置会一直占在那,不会脱离文档流

6. div绝对居中

  1. parent没写height时

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <head>
    <style>
    .child{
    border: 1px solid red;
    margin: 0 auto; /* 水平居中 */
    width: 100px;
    }
    .parent{
    border: 1px solid green;
    padding: 100px 0; /* 垂直居中 */
    }
    </style>
    </head>
    <body>
    <div class="parent">
    <div class="child">
    asd asd
    </div>
    </div>
    </body>
  2. parent写了height(不兼容IE)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <head>
    <style>
    .child{
    border: 1px solid red;
    }
    .parent{
    border: 1px solid green;
    height: 100px;
    display: flex; /* 这三行搞定 */
    justify-content: center;
    align-items: center;
    }
    </style>
    </head>
    <body>
    <div class="parent">
    <div class="child">
    asd asd
    </div>
    </div>
    </body>

7. 内联元素的宽高

padding和margin只会影响内联元素的宽度,不影响高度。
内联元素的宽度由文字个数、padding、margin、border决定。
高度只由line-height:5;和font-size决定。
块级元素高度由内部文档流决定

8. 生成一个1:1的div(不写高度)

1
2
3
4
5
6
7
8
9
10
11
<head>
<style>
div{
border: 1px solid green;
padding-top: 100%; /* 这是高度,会随宽度变化自动变 */
}
</style>
</head>
<body>
<div class="parent"> </div>
</body>

9. 层叠关系

堆叠顺序

10. 堆叠上下文

  • 根元素(HTML)
  • z-index值不为auto的 绝对/相对定位的元素就是堆叠上下文
  • position: fixed也是堆叠上下文
  • opacity: 0.5也是
  1. 堆叠上下文越后写的级别越高,级别高的堆叠上下文里的所有东西都可以压过级别底的里的所有东西。
  2. 在父亲是堆叠上下文中的儿子元素即使是z-index: -1;,在层叠关系里也会在父亲的border和background之上,不会沉下去

11. icon

  1. ps文件
    ps

    ps

    ps

    ps

    ps

    ps

    ps点OK变成100*100,再以png导出,在网页里用img引用即可

  2. png文件
    选择魔法棒选择魔法棒
    用魔法棒选中自己要的图标,按住shift同时选中这两个,然后反选反选然后按DEL键把背景删除,然后trim裁剪trim然后剪切成你想要的大小在网页里用img引用即可

  3. 用div的background引用图片

    1
    2
    3
    4
    5
    6
    7
    8
    <style>
    .icon{
    display: inline-block;
    width: 100px;
    height: 100px;
    background: transparent url(./qq.png) no-repeat 0 0;
    }
    </style>
  4. css sprites(雪碧图)

  5. iconfont方法最先进

12. 响应式(基本没人用了)

  1. 媒体查询

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <style>
    /* 屏幕宽度为300px-325px时的css样式 */
    @media(min-width: 300px) and (max-width: 325px){
    body{
    background: red;
    }
    }

    /* 屏幕宽度小于450px时的css样式 */
    @media(max-width: 450px){
    body{
    background: black;
    }
    }
    /* 这样前面那个就没用了,被覆盖了 */
    </style>
  2. 一般不用第一个方法,直接引用一个手机版的css即可

    1
    <link rel="stylesheet" media="(max-width:768px)" href="mobile.css">

    在屏幕宽度小于768px时,就会渲染这个css,要把这个引用写在main.css之后,把main.css覆盖。
    并加上meta:vp

    1
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">

13. flex布局

flex布局

  1. container的属性

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    flex-direction: row;// (默认)所有内容排列在一行
    flex-direction: row-reverse; //反向排列在一行
    flex-direction: column; //所有内容排列在一列
    flex-direction: column-reverse; //反向

    flex-wrap: nowrap; //不换行(默认)
    flex-wrap: wrap; //自动换行

    flex-flow: row nowrap; //上面两个属性的简写

    //justify控制主轴
    justify-content: space-between; //这两个都是子元素在一行均匀分布
    justify-content: space-around;
    justify-content: flex-start; //子元素都往起点靠
    justify-content: flex-end; //子元素都往终点靠
    justify-content: center; //子元素居中

    //align控制子元素的侧轴
    align-items: stretch; //(默认)把同一行里的子元素的高度都伸展到同一行里最高那个元素
    align-items: center; //垂直居中
    align-items: flex-start; //往侧轴的起点靠
  2. item的属性

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    //flex-grow,当一行空间没占满的时候
    .child:nth-child(1){
    flex-grow: 2;
    }
    .child:nth-child(2){
    flex-grow: 1;
    }
    //在同一行内把这两个元素按2比1的比例占满这一行

    //这三个元素的简写,后两个很少用
    flex: flex-grow flex-shrink flex-basis;

    //order(实现双飞翼)
    .child:nth-child(1){
    order: 2; //改变排列顺序,这个放在第二个
    }
    .child:nth-child(2){
    order: 1; //这个放在第一个
    }

    //align-self
    .child:nth-child(1){
    align-self: center; //第一个儿子在垂直方向居中
    }

14. 小技巧

对display:none到display:block的过程transition: all 1s;没用,可以改成visibility: hidden;和visibility: visible

15. bootstrap

  1. 安装
    下载css、js文件,js需要用jquery,所以要先引用jquery
  2. 栅格系统

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <div class=container>  //自动居中
    <div class=row> //一行分成12份
    <div class=col-md-1>1</div> //这两个分别占1份和11份
    <div class=col-md-11>11</div>
    </div>
    <div class=row> //第二行
    <div class="col-md-9 col-md-offset-3">9</div> //这样这个div会往右边对齐,因为第二个class代表往右偏3格
    </div>
    <div class=row> //第三行
    <div class="col-xs-6 col-md-1 col-sm-3">6</div>
    //在屏幕宽度很小的时候(手机)占6份
    //中等(电脑)的时候占1份
    //小(ipad)的时候占3份
    //超大屏(col-lg-12)
    </div>
    </div>
  3. 秘诀
    直接复制官网的html代码,复杂css组件,js组件,定制bootstrap可以自己改样式、颜色

  4. 小技巧
    靠右:不写col-md-...,直接写pull-right

  5. 主题
    下载的文件里有bootstrap.theme.css

16. IFC

  • font-size是刻字的时候的那个模版的大小
  • line-height是字所占的行高,但不同字体排在一起行高可能会变了,因为每个字体基线位置不同。而在一排要用基线对齐
  • vertical-top是元素实际占的高度的顶部对齐,所以img不写vertical-top的时候是以基线对齐的,下面会空出一点东西
  • 不要用inline-block布局了,用flex或float

HTTP面试

Veröffentlicht am 2018-05-05

1. HTTP状态码

1开头的

用于指定客户端应响应的某些动作

200 通常的成功 OK

204 成功处理请求,但不会刷新页面

表示服务器接收到的请求已经处理完毕,但浏览器页面不会刷新

301 永久重定向 Moved Permanently

请求的网页已永久移动到新位置,浏览器会转到另一个网址,永久性重定向与临时重定向不同的是,浏览器有可能会缓存,记住结果

302 Found

HTTP/1.0就有的,临时性重定向,POST方法的重定向在未询问用户的情况下就会变成GET

303 和 307

这两个是HTTP/1.1新加的,都是临时重定向,303和302一样,POST重定向为GET。
307不同的是,把POST转为GET的。

304 Not Modified

表示资源未被修改过,还是返回和上次一样的东西

400 Bad Request

客户端中存在语法错误。

401 Unauthorized

用户未授权,需要用户验证。

403 Forbidden

服务器已经理解请求,但拒绝执行

404 Not Found

服务器找不到请求的网页。

500 Internal Server Error

服务器遇到错误,无法完成请求。

503 Service Unavailable

由于临时的服务器维护或者过载,暂时无法处理请求

2. HTTP缓存(Cache-Control)

在服务器里设置

1
response.setHeader('Cache-Control', 'max-age=30')

每次浏览器请求完这个url后的30s内,都不会再请求这个文件。
但只要改变url,文件就会被再请求一次
所以css、js文件更新了,可以通过改变url来更新,例如

1
2
3
4
5
<script src="main.js"></script>  //第一个版本

<script src="main.js?v=2"></script>//第二个版本

<script src="main.js?随机数"></script>//以后的版本

3. Cache-Control和Etag的区别

  1. ETag
    设置响应头Etag
    1
    2
    3
    let string = fs.readFileSync('./main.js', 'utf8')
    let fileMd5 = md5(string);
    response.setHeader('ETag', fileMd5)

这样响应头的ETag中就会有该文件的md5值,浏览器会在请求头里设置if-None-Match,并附上这个md5值。
于是在请求的时候,如果这个md5值和你文件的md5值一样,说明文件没有更新,不需要下载

1
2
3
if(request.header['if-None-Match'] === fileMd5){
response.statusCode = 304;
}

  1. 和缓存的区别
    缓存根本就不会发请求,而ETag会发请求,所以缓存更快

4. Cookie和Session

cookie seesion

  • Cookie
    1. HTTP响应通过Set-Cookie设置Cookie
    2. 浏览器访问指定域名必须带上Cookie作为Request Header
      1. Cookie一般用来记录用户信息
  • Session
    1. session是服务器端的数据
    2. session一般通过在cookie里记录sessionID实现
    3. sessionID一般是随机数

5. LocalStorage和Cookie区别

LocalStorage

  1. cookie会随请求被发送到服务器上,而localStorage不会
  2. cookie大小一般在4k以下,localStorage在5Mb左右
  3. localStorage一般被用来记录有没有提示过用户等信息,不能用来记录密码之类的

6. GET和POST的区别

  1. 参数,GET的参数放在url的查询参数里,POST的参数(数据)放在请求消息体里
  2. 安全,http协议下,POST比GET更安全
  3. GET的参数有长度限制,一般是1024个字符,POST的参数一般没有长度限制(4-10Mb)
  4. 包,GET请求只需要发一个包,POST要发两个以上包(因为有消息体)
  5. GET用来读取数据,POST用来写数据,POST不幂等(幂等就是不管发多少请求,结果都一样,不幂等就是发一个请求,就多一条信息,数据库会越来越大)

7. 跨域

  1. JSONP
  2. CORS
  3. postMessage

JS面试

Veröffentlicht am 2018-05-05

1. 数据类型

  • number
  • string
  • boolean
  • symbol
  • null
  • undefine
  • object

2. promise怎么用

  • 生成promise

    1
    2
    3
    4
    5
    6
    7
    8
    9
    function(){
    return new Promise(function(resolve,reject){
    if(成功){
    resolve()
    }else{
    reject()
    }
    })
    }
  • then

    1
    $.ajax(...).then(成功函数,失败函数)
  • then是链式的

    1
    2
    $.ajax(...).then(成功函数1,失败函数1).then(成功函数2,失败函数2)
    //ajax返回404,失败函数1执行,成功函数2执行

3. 手写ajax

1
2
3
4
5
6
7
8
9
let request = new XMLHttpRequest();
request.open('POST','/xxx');
request.onreadystatechange = ()=>{
if(request.readyState === 4 && request.status === 200){
var string = request.responseText;
var value = JSON.parse(string);
}
}
request.send('a=1&b=2');

4. 手写闭包

1
2
3
4
5
6
7
8
9
10
11
function createAdder(){
let n=1;
return function(){
n+=1;
}
}

let adder = createAdder();
adder() //n===2
adder() //n===3
console.log(n) //n is nor defined

5. this是什么

  1. fn()里面的this正常模式下是Window
  2. fn()里面的this严格模式下是undefined
  3. a.b.c.fn()里面的this就是a.b.c
  4. new F()里面的this就是F()对象
  5. () => {console.log(this)}里面的this就是外面的this

6. 立即执行函数

!function(){}()
作用是创造一个函数作用域,防止污染全局变量
es6新语法可以代替立即执行函数

1
2
3
{
let name = 'p' //用let声明变量即可
}

7. async/await语法

举例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function returnPromise(){
return new Promise(function(resolve,reject){
setTimeout(()=>{
resolve('参数1')
},1000)
})
}

returnPromise().then((result)=>{
//这个result === '参数1'
})

let result = await returnPromise();
//这个result === '参数1'

await可以把异步代码变成同步代码,若Promise运行结果是reject,可用try()catch捕捉到。
加个await就可以等当前异步代码执行完再执行下面的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function 煮面(){
水=加水()
if(面===false){
面 = 去超市买面() //异步操作
}
阳春面 = 开火(水,面)
吃面(阳春面)
}
//这样的结果你只能喝白开水
//但是加上await 就不同了

async function 煮面(){
水=加水()
if(面===false){
面 = await 去超市买面() //异步操作
}
阳春面 = 开火(水,面)
吃面(阳春面)
}
//用了await 就可以等面回来以后再执行以下语句

8. 深拷贝

  1. 用JSON深拷贝
    1
    2
    let a = {...}
    let b = JSON.parse(JSON.stringify(a))

但这个方法不支持函数、引用、undefined、正则…

  1. 递归拷贝
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    var china = {
    nation : '中国',
    birthplaces:['北京','上海','广州'],
    skincolr :'yellow',
    friends:['sk','ls']
    }
    //深复制,要想达到深复制就需要用递归
    function deepCopy(o,c){
    var c = c || {}
    for(var i in o){
    if(typeof o[i] === 'object'){ //要考虑深复制问题了
    if(o[i].constructor === Array){
    //这是数组
    c[i] =[]
    }else{
    //这是对象
    c[i] = {}
    }
    deepCopy(o[i],c[i])
    }else{
    c[i] = o[i]
    }
    }
    return c
    }
    var result = {name:'result'}
    result = deepCopy(china,result)
    console.dir(result)

9. 数组去重

  1. ES5

    • 方法一(计数排序的逻辑):

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      Array.prototype.distinct = function (){
      var arr = this,
      i,
      obj = {},
      result = [],
      for(i = 0; i< arr.length; i++){
      if(!obj[arr[i]]){ //如果能查找到,证明数组元素重复了
      obj[arr[i]] = 1;
      result.push(arr[i]);
      }
      }
      return result;
      };
      var a = [1,2,3,4,5,6,5,3,2,4,56,4,1,2,1,1,1,1,1,1,];
      var b = a.distinct();
      console.log(b.toString()); //1,2,3,4,5,6,56
    • 方法二:(递归)

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      Array.prototype.distinct = function (){
      var arr = this,
      len = arr.length;
      arr.sort(function(a,b){ //对数组进行排序才能方便比较
      return a - b;
      })
      function loop(index){
      if(index >= 1){
      if(arr[index] === arr[index-1]){
      arr.splice(index,1);
      }
      loop(index - 1); //递归
      }
      }
      loop(len-1); //调用loop函数
      return arr;
      };
      var a = [1,2,3,4,5,6,5,3,2,4,56,4,1,2,1,1,1,1,1,1,56,45,56];
      var b = a.distinct();
      console.log(b.toString()); //1,2,3,4,5,6,45,56

方法一比较好

  1. ES6
    set数据结构
    1
    2
    3
    4
    function dedupe(array){
    return Array.from(new Set(array));
    }
    dedupe([1,1,2,3]) //[1,2,3]

10. 用正则实现string.trim()

1
2
3
4
function trim(string){
return string.replace(/^\s+|\s+$/g,'')
//^\s+表示开头有一个或多个空格,|表示或,\s+$表示结尾
}

11. 什么是原型

举例:

1
2
3
4
//一个数组
let a = [1,2,3]
//a只有4个key:0,1,2,length
a.push(4) //[1,2,3,4]

push方法哪来的?原型里的,数组是一个对象,每个对象都有一个隐藏的属性__proto__,而数组的隐藏属性指向数组的原型Array.prototype,push方法就存在这里面,可直接引用(还有pop,slice,splice)

12. class(类)

  1. 类声明不会提升
  2. 举例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    class Rectangle {
    // constructor
    constructor(height, width) {
    this.height = height;
    this.width = width;
    }
    // Getter
    get area() {
    return this.calcArea()
    }
    // Method
    calcArea() {
    return this.height * this.width;
    }
    }
    const square = new Rectangle(10, 10);

    console.log(square.area);
    // 100
  3. extends(继承)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    class Animal { 
    constructor(name) {
    this.name = name;
    }

    speak() {
    console.log(this.name + ' makes a noise.');
    }
    }

    class Dog extends Animal {
    speak() {
    console.log(this.name + ' barks.');
    }
    }

    var d = new Dog('Mitzie');
    d.speak(); // 'Mitzie barks.'

13. JS如何实现继承

  1. extends
    见上面的extends
  2. 原型链
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    function Animal(){
    this.body = '肉体' //自身属性
    }
    Animal.prototype.move = function(){}

    function Human(name){
    Animal.apply(this,arguments) //继承Animal的自身属性
    this.name = name
    }

    //下面这三句继承Animal的原型属性
    let f = function(){}
    f.prototype = Animal.prototype
    Human.prototype = new f()

    Human.prototype.useTools = function(){}
    //Human自己的原型属性

DOM面试

Veröffentlicht am 2018-05-05

1. DOM事件模型

  1. 冒泡
  2. 捕获
  3. 如果这个元素是被点击元素,那么执行顺序由监听顺序决定

2. 移动端的触摸事件

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    //说明触摸开始
    document.body.ontouchstart !== undefined
    //触摸开始移动
    canvas.ontouchmove = function (ev) {
    var x = ev.touches[0].clientX; //触摸的x坐标
    var y = ev.touches[0].clientY;
    }
    ontouchend //触摸结束
    ontouchcancel //触摸取消,因为有的浏览器不会触发touchend事件
  2. 模拟swipe事件(滑动)
    记录两次touchmove的位置差,若后一次在前一次的右边,说明向右滑了

3. 事件委托

  1. 监听父元素,看被触发的是哪个儿子,就是事件委托,委托父亲监听儿子
  2. 优点是可以监听动态生成的儿子,减少了事件处理程序的数量,整个页面占用的内存空间会更少。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    function listen(element,eventType,selector,fn){
    element.addEventListener(eventType,(e)=>{
    let el = e.target
    while(!el.matches(selector)){ //可能点击的是li里的span
    if(element === el){ //如果点击的是父元素,则跳出监听
    el =null
    break
    }
    el = el.parentNode
    }
    el&&fn.call(el,e,el)
    })
    return element
    }

    //<ul id="list">
    // <li></li>
    // ...
    //</ul>
    //let list=document.querySelector('#list')
    //listen(list,'click','li',()=>{})

HTML、CSS面试

Veröffentlicht am 2018-05-05

1. 盒模型

包括:实际内容(content)、填充(padding)、边框(border)

  • padding就是内容到border的距离
  • margin是border到父元素border的距离
  1. border-boxborder-box.pngborder-box: 实际宽度 == width == 100 == 内容区 + padding + border

  2. content-boxcontent-box.pngcontent-box: 实际宽度 == width + padding + border

2. 如何理解HTML语义化

  1. 举例
    比如说,标题就用h1~h6,段落就用p,边栏用aside,主要内容用main
  2. 用处
    在没有CSS的情况下,页面也能呈现出很好地内容、代码结构

    3. meta:viewport

    背下来
    1
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">

控制页面在移动端不会缩小显示

4. 复习canvas项目

自己再做一遍canvas

5. css Reset和normalize.css区别

  • css reset的想法是清除所有默认样式
  • 后来normalize.css取代了css reset,normalize自己制定相应的默认样式,用了normalize.css之后,所有浏览器上默认样式都基本一致

6. css居中

1. 垂直居中

  1. 若父元素没有写height,则直接在父元素写

    1
    padding: 10px 0;

    子元素就可以居中,所以尽量避免父亲高度确定

  2. 让一个元素在父级元素中绝对居中
    方法一:
    给父级元素加:

    1
    2
    position:relative;   //若父级元素是body可以不用
    加

    再给自己加:

    1
    2
    3
    4
    5
    6
    7
    8
    div{
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    }

    方法二:(若不兼容IE,工作中只要用这一种方法即可,最简单,Chrome,移动端都可以用)
    给父元素加:

    1
    2
    3
    display: flex;               //让它变成一个弹性盒
    justify-content: center; //水平居中
    align-items: center; //垂直居中
  3. table自带居中(兼容IE)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <html>
    <style>
    .parent{
    border: 1px solid red;
    height: 600px;
    }
    .child{
    border: 1px solid green;
    }
    </style>
    <body>
    <table class="parent">
    <tr>
    <td class="child">
    文字
    </td>
    </tr>
    </table>
    </body>
    </html>

    文字会居中

  4. 用div假扮table(兼容IE)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    <html>
    <style>
    div.table{
    display: table;
    border: 1px solid red;
    height: 600px;
    }

    div.tr{
    display: table-row;
    border: 1px solid green;
    }

    div.td{
    display: table-cell;
    border: 1px solid blue;
    vertical-align: middle;
    }

    </style>
    <body>
    <div class="table">
    <div class="tr">
    <div class="td">
    文字
    </div>
    </div>
    </div>
    </body>
    </html>
  5. 用100%高度的before和after

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    .parent{
    border: 3px solid red;
    height: 600px;
    text-align: center;
    }

    .child{
    border: 3px solid black;
    display: inline-block;
    width: 300px;
    vertical-align: middle;
    }

    .parent:before{
    content:'';
    display: inline-block;
    height: 100%;
    vertical-align: middle;
    }
    .parent:after{
    content:'';
    display: inline-block;
    height: 100%;
    vertical-align: middle;
    }
  6. 绝对定位加上margin-top: -自身height的50%

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    <html>
    <style>
    .parent{
    height: 600px;
    border: 1px solid red;
    position: relative;
    }
    .child{
    border: 1px solid green;
    width: 300px;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-left: -150px;
    height: 20px;
    margin-top: -10px;
    text-align: center;
    }
    </style>
    <body>
    <div class="parent">
    <div class="child">
    文字
    </div>
    </div>
    </body>
    </html>
  7. 让一个背景图居中,并且让它自适应屏幕

    1
    2
    background: url("wallhaven-w-min.jpg") no-repeat center center ;
    background-size: cover;

2. 水平居中

  1. 块级元素水平居中

    1
    2
    margin-left:auto;
    margin-right:auto;
  2. 内联元素水平居中,给它们的父元素加上

    1
    text-align:center;

    若不是内联元素想让它居中,可加display:inline-block,加了之后一般还要加下面这句,不然可能会有bug(下面可能会空出一行)

    1
    vertical-align: top;
  3. 让导航栏横过来,并在同一行里均匀分布
    给ul加css

    1
    2
    3
    4
    ul{
    display:flex;
    justyfy-content:space-between;
    }

    去掉li的float:left
    去掉ul的clearfix

7. 选择器的优先级

  1. id比class优先级高
  2. 同样优先级,写在后面的会覆盖前面
  3. a:hover和a的优先级一样,所以加hover样式的时候,hover要写在下面。不然会被覆盖
  4. color: red !important 优先级最高

8. BFC

块格式化上下文(Block Formatting Context),面试的时候举例说明

1. 功能一(bfc用来包住儿子元素)

  1. 父亲若是以下七种情况,则是bfc(可以包住它的儿子)
    • 浮动元素(float:left或right)
    • 绝对定位
    • 内联块(display: inline-block)
    • 表格(display: table-cell 或 display: table-caption 或 )
    • overflow的值不是visible(如overflow:hidden)
    • display: flow-root(css新规则,专为bfc而生)

举例1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<style>
.father{
width: 100px;
height: 600px;
border: 4px solid red;
overflow: hidden
}
.son{
border: 4px solid green;
float: left;
}
</style>
<body>
<div class="father">
<div class="son">2</div>
</div>
</body>

答1:bfc的overflow: hidden可清除浮动(但平常清除浮动用clearfix即可)
答2:父亲的overflow: hidden取消父亲儿子的margin-top合并,若父亲没加overflow:hidden,儿子的margin-top同时也会给父亲(平常若不想用bfc,给父亲加一个padding-top: 0.1px,也可以达到这个效果)

  1. 父亲里的儿子没有float的话,儿子之间的上下margin会合并
  2. 父亲会包住儿子,但如果儿子里面有孙子且儿子是bfc的话,父亲是不会管孙子的

2. 功能二(让浮动的兄弟之间划清界限)

float+div做左右自适应布局,如果不加bfc,兄弟会重合(因为有float)

举例2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<style>
.brother1{
width: 100px;
height: 600px;
border: 4px solid red;
float: left;
margin-right: 20px;
}
.brother2{
height: 600px;
border: 4px solid green;
display: flow-root
}
</style>
<body>
<div class="brother1">1</div>
<div class="brother2">2</div>
</body>

这个功能可以用flex布局实现,bfc被淘汰了!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<style>
.brother1{
width: 100px;
height: 600px;
border: 4px solid red;
margin-right: 20px;
}
.brother2{
height: 600px;
border: 4px solid green;
flex: 1;
}
body{
display: flex;
}
</style>
<body>
<div class="brother1">1</div>
<div class="brother2">2</div>
</body>

9. 如何清除浮动

  1. bfc的overflow: hidden
  2. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    .clearfix::after{
    content: '';
    clear: both;
    display: block;
    }

    //若要兼容IE
    .clearfix{
    zoom: 1;
    }

10. 父亲儿子margin合并

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<head>
<style>
.child{
height: 100px;
border: 1px solid red;
margin-top: 100px;
}
.parent{
width: 100%;
background: green;
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>

这样儿子的margin-top也会给到父亲,把父亲挤下来,但只要在父亲里加:

  • border-top: 0.1px solid green;
  • display: table;
  • display: flex;(不加width的话会把width变成0)
  • display: inline-block(不加width的话会把width变成0)

canvas入门

Veröffentlicht am 2018-04-28
  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    //获取canvas标签
    var canvas = document.querySelector("#canvas");

    //初始化
    var context = canvas.getContext('2d');

    //画线
    function drawLine(x1,y1,x2,y2) {
    context.beginPath();
    context.moveTo(x1-0,y1+29); //起点
    context.lineWidth = 5; //线的粗细
    context.lineTo(x2-0,y2+29); //终点
    context.stroke(); //将各个点练成线
    }

    //画三角形
    function drawLine(x1,y1,x2,y2) {
    context.beginPath();
    context.moveTo(x1,y1); //起点
    context.lineTo(x2,y2); //第二个点
    context.lineTo(x3,y3); //第三个点
    context.stroke(); //将各个点练成线
    context.closePath(); //自动将第一个点和最后一个点连起来
    }
    //画圆圈
    function drawTriangle(x,y,r) {
    context.beginPath();
    context.arc(x-0,y+29,r,0,Math.PI*2); //left,top,半径, 起点(以原点为圆心从第一象限的x轴开始往y轴负方向画圈),终点
    context.fill(); //将圆圈填色
    }

    context.strokeStyle = 'black'; //线的颜色
    context.fillStyle = 'black'; //圆圈填充的颜色
    context.clearRect(x,y,30,30); //橡皮擦功能

arc的参数自己试

  1. 1
    2
    3
    4
    5
    6
    7
    8
    function setCanvasSize() {
    //获取用户界面的宽高
    var pageWidth = document.documentElement.clientWidth;
    var pageHeight = document.documentElement.clientHeight;
    //赋给画板
    canvas.width = pageWidth;
    canvas.height = pageHeight;
    }
  2. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    //触屏设备
    canvas.ontouchstart = function(ev){
    var x = ev.touches[0].clientX; //获取点击的位置
    var y = ev.touches[0].clientY;
    } //监听手指点击事件
    canvas.ontouchmove = function(){} //监听手指移动事件

    //非触屏设备
    canvas.onmousedown = function(ev){}
    canvas.onmousemove = function (ev) {}
    canvas.onmouseup = function () {}
    canvas.onmouseout = function () {}

异步与回调

Veröffentlicht am 2018-04-27

1. 同步

等自己这行代码任务执行完了得到结果,再执行下一行,叫做同步

1
2
3
console.log('1')  
console.log('2')
console.log('3') //按顺序执行

2. 异步

不等自己这行代码运行完,直接运行下一行代码,就是异步

1
2
3
4
setTimeout(function(){
alert('1')
},5000)
alert('2') //会先打2,再打1

3. 回调

  • 将函数f1作为参数传给另一个函数f2
  • f2在需要f1的时候直接在f2函数里调用。
  • f1就是回调函数,f2调用f1叫做调用回调函数。

webpack入门

Veröffentlicht am 2018-04-23

1. 初体验

很乱,数不清的配置

2. 安装

  1. 创建目录

    1
    2
    3
    mkdir webpack-demo
    cd webpack-demo
    npm init //创建一个package.json
  2. copy Github上webpack官网的文档

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    //安装webpack
    npm install --save-dev webpack

    //配置
    touch webpack.config.js
    vi webpack.config.js
    //在里面写以下内容
    /*
    const path = require('path');
    module.exports = {
    entry: './src/index.js',
    output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
    }
    };*/

    //创建文件
    touch src/index.js

    //运行webpack
    npx webpack //这时会多出dist目录,里面有bundle.js文件

3. 使用

  1. 在index.js里写

    1
    2
    3
    4
    5
    6
    console.log(1)

    //再运行webpack:
    npx webpack

    //再看bundle.js,这时会多出来一行console.log(1)
  2. 安装babel-loader自动转换es6,我用的是 7.x branch for docs with Babel v6

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    //安装v6,命令行
    npm install babel-loader babel-core babel-preset-env webpack

    //将这个复制到webpack的配置文件webpack.config.js里,加在output的下面
    module: {
    rules: [
    {
    test: /\.js$/,
    exclude: /(node_modules|bower_components)/,
    use: {
    loader: 'babel-loader',
    options: {
    presets: ['env']
    }
    }
    }
    ]
    }

    //加在output的下面,复制完后成这样
    const path = require('path');

    module.exports = {
    entry: './src/index.js',
    output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
    },
    module: {
    rules: [
    {
    test: /\.js$/,
    exclude: /(node_modules|bower_components)/,
    use: {
    loader: 'babel-loader',
    options: {
    presets: ['env']
    }
    }
    }
    ]
    }
    };

运行npx webpack
若出现can't find '...'或can't resolve '...'的报错,则安装省略号里的东西npm i '省略号'
注意:若是Couldn't find preset "env",不要安装env,而是npm i babel-preset-env

  1. 使用babel
    1
    2
    3
    //当你在写index.js里写
    let a=1
    //它就会帮你自动转换成es5了

3. 总结

太乱了太多配置了,希望parcel赶快升级替换掉webpack

ETag入门

Veröffentlicht am 2018-04-23

1. MD5

摘要算法,任何相同文件的MD5值都一样,只要改变了这个文件里的内容,它的MD5值就会变

2. ETag

在服务器里设置响应头

1
2
3
let string = fs.readFileSync('./main.js', 'utf8')
let fileMd5 = md5(string);
response.setHeader('ETag', fileMd5)

这样响应头的ETag中就会有该文件的md5值,浏览器会在请求头里设置if-None-Match,并附上这个md5值。
于是在请求的时候,如果这个md5值和你文件的md5值一样,说明文件没有更新,不需要下载

1
2
3
if(request.header['if-None-Match'] === fileMd5){
response.statusCode = 304;
}

3. 和缓存的区别

缓存根本就不会发请求,而ETag会发请求,并比较md5值是否一样。
所以还是缓存好

Cache-Control(缓存)入门

Veröffentlicht am 2018-04-23

1. 性能优化

想让哪个文件被缓存(如js文件、css文件等),就在服务器里的路由里写上

1
response.setHeader('Cache-Control', 'max-age=30')

这样每次浏览器请求完这个url后的30s内,都不会再请求这个文件,提升了速度。
但只要改变url,文件就会被再请求一次

2. html不要设置Cache-Control

因为html若设置了缓存,浏览器就不会向服务器发送任何请求,这样用户无法获取到任何更新后的版本。
若是css、js这类文件更新了,可以通过改变url来使用户获取到更新,例如

1
2
3
4
5
6
7
<script src="main.js"></script>  //第一个版本

<script src="main.js?v=2"></script> //第二个版本,改变url,加了个查询参数,于是浏览器就会重新请求文件了

<script src="main.js?v=3"></script> //第三个版本

<script src="main.js?随机数"></script> //以后的版本

3. 旧版本的缓存(Expires)

1
response.setHeader('Expirse', 'Feb 2018 14:04:04.....')

区别:Expires设置的是日期,而且是根据本地时间判断的,若是用户的本地时间有误,就会有bug,所以现在都用Cache-Control,若两个都设置了会优先使用Cache-Control,因为这是新版的

12…5

Pengyize

50 Artikel
© 2018 Pengyize
Erstellt mit Hexo
|
Theme — NexT.Muse v5.1.4