-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
397 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const arr = [ | ||
{ | ||
id: 1, | ||
child: [ | ||
{ id: 3 }, | ||
{ | ||
id: 4, | ||
child: [ | ||
{ | ||
id: 5, | ||
child: [{ id: 6 }], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 2, | ||
child: [ | ||
{ | ||
id: 7, | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
function findPathById(arr, id) { | ||
let resPath; | ||
function fn(arr, path = []) { | ||
for (const item of arr) { | ||
if (item.id === id) { | ||
path.push(id); | ||
resPath = path; | ||
return; | ||
} | ||
if (item.child) { | ||
path.push(item.id); | ||
fn(item.child, [...path]); | ||
} | ||
} | ||
} | ||
fn(arr); | ||
return resPath; | ||
} | ||
|
||
// findPathById(arr, 6); | ||
console.log("findPathById(arr, 6)", findPathById(arr, 6)); | ||
console.log("findPathById(arr, 7)", findPathById(arr, 7)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 2 为并发最大数 | ||
const limit = pLimit(2); | ||
const inputs = [ | ||
limit(() => fetchSomething(1000), "param1"), | ||
limit(() => fetchSomething(2000), "param2"), | ||
limit(() => fetchSomething(3000), "param3"), | ||
]; | ||
function fetchSomething(time) { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(); | ||
}, time); | ||
}); | ||
} | ||
const time = Date.now(); | ||
Promise.all(inputs).then((results) => { | ||
console.log(results); | ||
console.log(Date.now() - time); // 需要打印为 4s。(解释:因为并发数限制为2,第一个和第二个请求并发进行,第三个等待,第一个结束后开始第三个请求。总体时间为4s) | ||
}); | ||
|
||
// 实现 pLimit 函数? | ||
function pLimit(limit) { | ||
let count = 0; | ||
const list = []; | ||
function schedule() { | ||
if (count < limit && list.length > 0) { | ||
const { cb, resolve, reject, args } = list.shift(); | ||
count++; | ||
cb(...args) | ||
.then(resolve) | ||
.catch(reject) | ||
.finally(() => { | ||
count--; | ||
schedule(); | ||
}); | ||
} | ||
} | ||
return function (cb, ...args) { | ||
return new Promise((resolve, reject) => { | ||
list.push({ cb, args, resolve, reject }); | ||
schedule(); | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Object.prototype[Symbol.iterator] = function () { | ||
return Object.values(this)[Symbol.iterator](); | ||
}; | ||
|
||
// 面试题:如何结构? | ||
const [a, b] = { a: 1, b: 2 }; | ||
|
||
console.log(a, b); // 1,2 |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
# 移动端适配 | ||
|
||
## 1. 移动端适配方案 | ||
|
||
- 方案一:使用viewport | ||
- 方案二:使用rem | ||
- 方案三:使用flexible.js | ||
- 方案四:使用postcss-pxtorem | ||
|
||
|
||
## 2. viewport | ||
|
||
viewport是指浏览器的可视区域,它决定了网页的最终显示效果。 | ||
|
||
viewport的设置方式有两种: | ||
|
||
1. meta标签: | ||
|
||
```html | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
``` | ||
|
||
2. CSS: | ||
|
||
```css | ||
html { | ||
width: device-width; | ||
height: 100%; | ||
} | ||
``` | ||
|
||
|
||
- width: device-width:设置viewport宽度为设备宽度,这样可以保证页面的宽度适应不同设备的屏幕大小。 | ||
- initial-scale=1.0:设置初始缩放比例为1.0,这样可以保证页面的初始显示效果。 | ||
|
||
我们布局时,能使用 vw,vh 去作为单位,这样就可以完美适配不同设备的屏幕大小。 | ||
|
||
|
||
## 3. rem | ||
|
||
rem是相对于根元素html的font-size的单位,它可以实现不同设备的适配。 | ||
|
||
使用rem的步骤: | ||
|
||
1. 设置根元素html的font-size: | ||
|
||
```js | ||
const $html = document.querySelector('html'); | ||
$html.style.fontSize = $html.clientWidth / 10 + 'px'; | ||
``` | ||
|
||
|
||
2. 利用rem单位来设置元素的font-size: | ||
|
||
```css | ||
.item { | ||
font-size: 1.6rem; | ||
} | ||
``` | ||
|
||
## 4. flexible 方案 | ||
|
||
flexible 方案的原理是指定设计稿的宽度,然后根据 dpr 进行缩放,以达到适配不同屏幕的效果。 | ||
|
||
假设设计稿宽度为 750px,我们可以设置如下代码: | ||
|
||
|
||
在 HTML 的 head 标签里配置 meta 如下: `<meta name="viewport" content="width={设计稿宽度}, initial-scale={屏幕逻辑像素宽度/设计稿宽度}" > `。 | ||
|
||
```js | ||
const width = 750; | ||
const dpr = window.devicePixelRatio || 1; | ||
const scale = window.innerWidth / width; | ||
|
||
let meta = document.querySelector('meta[name="viewport"]'); | ||
const content = `width=${width}, initial-scale=${scale} user-scalable=no` | ||
if (!meta) { | ||
meta = document.createElement('meta'); | ||
meta.setAttribute('name', 'viewport') | ||
document.head.appendChild(meta); | ||
} | ||
meta.setAttribute('content', content); | ||
|
||
|
||
``` | ||
那么我们就可以在项目里面愉快的使用 px 啦,并且不需要任何的单位转换。 | ||
|
||
## 5. postcss-pxtorem | ||
|
||
postcss-pxtorem是一个PostCSS插件,它可以将px单位转换为rem单位,实现不同设备的适配。 | ||
|
||
使用postcss-pxtorem的步骤: | ||
|
||
1. 安装postcss-pxtorem: | ||
|
||
``` | ||
npm install postcss-pxtorem --save-dev | ||
``` | ||
|
||
2. 在postcss.config.js中配置postcss-pxtorem: | ||
|
||
```js | ||
module.exports = { | ||
plugins: [ | ||
require('postcss-pxtorem')({ | ||
rootValue: 16, // 1rem = 16px | ||
propList: ['*'] | ||
}) | ||
] | ||
} | ||
``` | ||
|
||
3. 在需要适配的元素的样式中使用rem单位: | ||
|
||
```css | ||
.container { | ||
font-size: 1.6rem; | ||
} | ||
``` | ||
|
||
## 6. 总结 | ||
|
||
移动端适配方案有很多,每种方案都有其优缺点,根据项目的实际情况选择适合的方案即可。 |
Oops, something went wrong.