为什么无法旋转(自己写
class Transform {
constructor(selector) {
this.el = document.querySelector(selector);
this._queue = [];
this._transform = {
rotate: "",
translate: "",
scale: "",
};
// 装_queue时间分支
this.actTime = [];
this.defaultTime = Transform.config.defaultTime;
this.el.style.transition = `all ${0.3}s`;
}
translate(value, time) {
return this._add("translate", value, time);
}
scale(value, time) {
return this._add("scale", value, time);
}
rotate(value, time) {
return this._add("rotate", value, time);
}
_add(type, value, time = this.defaultTime) {
this._queue.push({
type,
value,
time,
});
return this;
}
//添加完成,可以开始
done() {
this._start();
}
_start() {
let length = this._queue.length;
for (let index = 0; index < length; index++) {
let queueArg = this._queue.shift();
this.actTime.push(queueArg.time);
this._getTransform(queueArg);
}
console.log(this.actTime);
console.log(this._transform);
if (!this._queue.length)
return this.getResult();
}
// 用于修改css
_getTransform({ time, value, type }) {
switch (type) {
case "translate":
this._transform.translate = `translate(${value})`;
break;
case "scale":
this._transform.scale = `scale(${value})`;
break;
case "rotate":
this._transform.rotate = `rotate(${value}deg)`;
break;
}
}
getResult() {
let values = this._transform;
let arr = [];
//把对象转成数组
for (let i in values) {
arr.push(values[i]);
}
console.log(arr);
let vLength = arr.length;
for (let index = 0; index < vLength; index++) {
setTimeout(() => {
this.el.style.transition = `all ${this.actTime[index] / 1000}s`;
// if(arr[index] == 'rotate(90deg)') this.el.style.transform = arr[index];
this.el.style.transform = arr[index];
console.log(index);
}, this.actTime[index]);
}
}
}
Transform.config = {
defaultTime: 800,
};
const tf = new Transform("#ball");
console.log(tf);
class MultiTransform extends Transform {}
tf.translate("190px,300px")
.scale(1.2)
.rotate(90, 2000)
.done();
html
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]> <html class="no-js"> <!--<![endif]-->
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
<style>
#ball {
width: 200px;
height: 200px;
background:linear-gradient(#e66465 50%, #9198e5 50%);
border-radius: 50%;
}
</style>
</head>
<body>
<div id="ball"></div>
<script src="6.js"></script>
</body>
</html>
正在回答 回答被采纳积分+1
同学你好,代码问题如下:
1、初始定义了_transform数据顺序

然后getResult方法中按照定义的顺序赋值给了arr,那么动画的顺序就是_transform中的顺序,而不是调用动画的顺序。可以输入arr看看,和_queue队列顺序不同


那么动画和时间顺序就会对应不上,800对应rotate,800对应translate,2000对应scale 。而前面两个动画时间相同,transform就会进行覆盖,后面覆盖前面的,所以看不到rotate动画。如果将translate动画添加不同的时间就可以看到效果。例如:

2、建议按照课程中修改,不添加getResult方法,如下

可以再测试下,祝学习愉快!
- 参与学习 人
- 提交作业 239 份
- 解答问题 10739 个
本阶段带你深入前端开发的肌理,通过ES6基础知识和前端主流高级框架的学习,助你快速构建企业级移动webAPP应用,进入职场的终极battle
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星