每日学习总结系列(二)
一、页面loading效果
下午做功能,做了2个loading
一个是条形的loading
一个是转圈的loading
组件内代码
<!--
* @Descripttion: loading集合
* @Author: wangxi
* @Date: 2020-05-08 15:05:54
* @LastEditors: wangxi
* @LastEditTime: 2020-05-08 18:23:50
-->
<template>
<div class="progressbar-loading-wrapper" :class="{active: loading}" :style="{background: bgColor}">
<div v-if="type === 'primary'" class="progressbar"></div>
<div v-else-if="type === 'logo'">
<img class="logo" :src="webLogo" alt height="75" />
<svg viewBox="25 25 50 50" class="circular">
<circle
cx="50"
cy="50"
r="20"
fill="none"
stroke-width="2"
stroke-miterlimit="10"
class="path"
/>
</svg>
</div>
</div>
</template>
<script>
import weblogo from "@/assets/logo-loading.png";
export default {
props: {
type: {
type: String,
default: "primary"
},
loading: {
type: Boolean,
default: false
},
bgColor: {
type: String,
default: "#111113"
}
},
data() {
return {
webLogo: weblogo
};
}
};
</script>
<style lang="scss" scoped>
.progressbar-loading-wrapper {
background-color: $trade-block-color;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 9;
opacity: 0;
filter: alpha(opacity=0);
pointer-events: none;
transition: opacity 0.2s;
&.active {
pointer-events: all;
opacity: 1;
filter: none;
.progressbar {
transform: scale(1);
opacity: 1;
filter: none;
}
.circular {
opacity: 1;
}
}
.progressbar {
position: absolute;
top: 50%;
left: 50%;
margin: -5px 0 0 -75px;
height: 10px;
width: 150px;
transform: scale(0);
opacity: 0;
filter: alpha(opacity=0);
animation-timing-function: linear;
animation-name: wait-bg;
animation-iteration-count: infinite;
animation-duration: 0.45s;
background-repeat: repeat;
background-position: 0 0;
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAKQWlDQ1BJQ0MgUHJvZmlsZQAASA2dlndUU9kWh8+9N73QEiIgJfQaegkg0jtIFQRRiUmAUAKGhCZ2RAVGFBEpVmRUwAFHhyJjRRQLg4Ji1wnyEFDGwVFEReXdjGsJ7601896a/c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ltZW5zaW9uPgogICAgICAgICA8ZXhpZjpDb2xvclNwYWNlPjE8L2V4aWY6Q29sb3JTcGFjZT4KICAgICAgICAgPGV4aWY6UGl4ZWxZRGltZW5zaW9uPjUwPC9leGlmOlBpeGVsWURpbWVuc2lvbj4KICAgICAgICAgPGRjOnN1YmplY3Q+CiAgICAgICAgICAgIDxyZGY6QmFnLz4KICAgICAgICAgPC9kYzpzdWJqZWN0PgogICAgICAgICA8eG1wOk1vZGlmeURhdGU+MjAxNC0xMS0wNlQwMDoxMToyNTwveG1wOk1vZGlmeURhdGU+CiAgICAgICAgIDx4bXA6Q3JlYXRvclRvb2w+UGl4ZWxtYXRvciAzLjIuMTwveG1wOkNyZWF0b3JUb29sPgogICAgICA8L3JkZjpEZXNjcmlwdGlvbj4KICAgPC9yZGY6UkRGPgo8L3g6eG1wbWV0YT4KpNNNcQAAAPdJREFUaAXd2EsOw0AIA9BMT9X7r3qraZCqSFF+88FglwVb9GRWLrXW9wKeUsoHfGJ5oQ9EICwMKCQKYWHAIJEIGCQaAYFkINwhWQhXSCbCDZKNcIEwIKYhLIgpCBNiGMKGGIIwIrohrIguCDOiGcKOaIIoIB4hKohbiBLiEqKGOIUoIg4QVcQOoozYIOoIgxRb6AkpAf8CsTaZ0ERCkvjVsTBIJMK+CgKJRkAgGQh3SBbCFZKJcINkI1wgDIhpCAtiCsKEGIawIYYgjIhuCCuiC8KMaIawI5ogCohHiAriFqKEuISoIU4hiogDRBWxgygjNog6wiBf8vfHgJj+rHIAAAAASUVORK5CYII=);
border-radius: 10px;
background-color: $primary-color;
transition-delay: 0.2s;
transition: transform 0.3s cubic-bezier(0.18, 0.89, 0.32, 1.28),
opacity 0.3s;
box-shadow: 0 2px 15px rgba(0, 0, 0, 0.2);
pointer-events: none;
}
.logo {
position: absolute;
height: 75px;
width: 75px;
top: 50%;
left: 50%;
margin: -37.5px 0 0 -37.5px;
}
.circular {
animation: rotate 2s linear infinite;
height: 150px;
width: 150px;
top: 50%;
left: 50%;
margin: -75px 0 0 -75px;
transform-origin: center center;
position: absolute;
opacity: 0;
.path {
stroke-dasharray: 1, 200;
stroke-dashoffset: 0;
animation: dash 1.5s ease-in-out infinite;
stroke-linecap: round;
stroke: $primary-color;
}
}
@keyframes rotate {
100% {
transform: rotate(360deg);
}
}
@keyframes dash {
0% {
stroke-dasharray: 1, 200;
stroke-dashoffset: 0;
}
50% {
stroke-dasharray: 89, 200;
stroke-dashoffset: -35px;
}
100% {
stroke-dasharray: 89, 200;
stroke-dashoffset: -124px;
}
}
@keyframes wait-bg {
0% {
background-position: 0 0;
}
to {
background-position: 50px 0;
}
}
}
</style>
使用
注意:需要在使用组件的外层添加
position:relative
的class
//默认开启,ajax回调后设置为false,如果觉得时间太短,可以稍作延迟
holdingloading: true
setTimeout(() => {
this.holdingloading = false
}, 500)
二、实现Promise
昨天看到的那篇实现Promise
的更全一些,明天看那篇,今天跟着神三元的文章实现了一个基础版的,算是热身了。
小试牛刀
初步实现Promise:
1.实现三种状态:pending
, fulfilled
, rejected
2.能够实现then
方法两种回调函数的处理
//promise.js
class Promise{
//传一个异步函数进来
constructor(excutorCallBack){
this.status = 'pending';
this.value = undefined;
this.fulfillAry = [];
this.rejectedAry = [];
//=>执行Excutor
let resolveFn = result => {
if(this.status !== 'pending') return;
let timer = setTimeout(() => {
this.status = 'fulfilled';
this.value = result;
this.fulfillAry.forEach(item => item(this.value));
}, 0);
};
let rejectFn = reason => {
if(this.status !== 'pending')return;
let timer = setTimeout(() => {
this.status = 'rejected';
this.value = reason;
this.rejectedAry.forEach(item => item(this.value))
})
};
try{
//执行这个异步函数
excutorCallBack(resolveFn, rejectFn);
} catch(err) {
//=>有异常信息按照rejected状态处理
rejectFn(err);
}
}
then(fulfilledCallBack, rejectedCallBack) {
//resolve和reject函数其实一个作为微任务
//因此他们不是立即执行,而是等then调用完成后执行
this.fulfillAry.push(fulfilledCallBack);
this.rejectedAry.push(rejectedCallBack);
//一顿push过后他们被执行
}
}
module.exports = Promise;
测试如下:
let Promise = require('./promise');
//new Promise传入一个方法,参数是resolveFn和rejectFn
//然后执行then,将传入的方法丢到fulfillAry和rejectedAry里
//最后,等待resolve或reject时,执行resolveFn或rejectFn方法,改变状态,循环执行数组里的方法,并返回value
let p1 = new Promise((resolve, reject) => {
setTimeout(() => {
Math.random()<0.5?resolve(100):reject(-100);
}, 1000)
}).then(res => {
console.log(res);
}, err => {
console.log(err);
})
完成链式效果
最大的难点在于链式调用的实现,具体来说就是then
方法的实现。
//then传进两个函数
then(fulfilledCallBack, rejectedCallBack) {
//保证两者为函数
typeof fulfilledCallBack !== 'function' ? fulfilledCallBack = result => result:null;
typeof rejectedCallBack !== 'function' ? rejectedCallBack = reason => {
throw new Error(reason instanceof Error? reason.message:reason);
} : null
//返回新的Promise对象,后面称它为“新Promise”
return new Promise((resolve, reject) => {
//注意这个this指向目前的Promise对象,而不是新的Promise
//再强调一遍,很重要:
//目前的Promise(不是这里return的新Promise)的resolve和reject函数其实一个作为微任务
//因此他们不是立即执行,而是等then调用完成后执行
this.fulfillAry.push(() => {
try {
//把then里面的方法拿过来执行
//执行的目的已经达到
let x = fulfilledCallBack(this.value);
//下面执行之后的下一步,也就是记录执行的状态,决定新Promise如何表现
//如果返回值x是一个Promise对象,就执行then操作
//如果不是Promise,直接调用新Promise的resolve函数,
//新Promise的fulfilAry现在为空,在新Promise的then操作后.新Promise的resolve执行
x instanceof Promise ? x.then(resolve, reject):resolve(x);
}catch(err){
reject(err)
}
});
//以下同理
this.rejectedAry.push(() => {
try {
let x = this.rejectedCallBack(this.value);
x instanceof Promise ? x.then(resolve, reject):resolve(x);
}catch(err){
reject(err)
}
})
}) ;
}
测试用例:
let p1 = new Promise((resolve, reject) => {
setTimeout(() => {
Math.random()<0.5?resolve(100):reject(-100);
}, 1000)
})
let p2 = p1.then(result => {
//执行then返回的是一个新的Promise
return result + 100;
})
let p3 = p2.then(result => {
console.log(result);
}, reason => {
console.log(reason)
})
链式调用的内部流程:
Promise.race(iterable)
race
函数返回一个Promise
,它将与第一个传递的promise
相同的完成方式被完成。它可以是完成(resolves
),也可以是失败(rejects
),这要取决于第一个完成的方式是两个中的哪个。
大白话说就是返回第一个promise的返回结果。
三、js大数字
今天发现一个问题,明天找下原因,使用bigInt
也不行
好记性不如烂笔头,看到自己觉得应该记录的知识点,结合自己的理解进行记录,用于以后回顾。