JSFiddle - React, Tailwind, and code Playground
HTML
<body>
<div id="test">
<div class="s">
</div>
</div>
</body>
CSS
#test{
margin-top: 66px;
background-color: #ffff4f;
width: 500px;
height: 50px;
}
JavaScript
/**
* Класс создающий анимацию
* @param node
* @param styles
* @param options
* @constructor
*/
function Animation(node, styles, options) {
this.node = node;
this.styles = styles;
this.options = options;
this.initValue = this.getInitValue();
this.animatedValue = this.getAnimatedValue();
this.timeout = 10;
}
/**
* Главный метод запускающий анимацию
*/
Animation.prototype.startAnimate = function() {
if (!Object.keys(this.animatedValue).length) {
throw 'No animation';
}
this.start = new Date().getTime();
this.end = this.start + this.options.duratrion;
this.animationLoop = setInterval(function() {
var time = new Date().getTime();
var progressPercent = 100 - Math.floor(100 * (this.end - time) / this.options.duratrion);
if (progressPercent >= 100) {
this.stopAnimation();
return;
}
var currentAnimated = this.getValueByStep(progressPercent);
this.setAnimatedValue(currentAnimated);
if ((typeof this.options.onTick) == 'function') {
this.options.onTick(progressPercent);
}
}.bind(this), this.timeout);
};
/**
* Установить новые стили анимируемого объекта
* @param animatedValue объект с новыми значениями стилей
*/
Animation.prototype.setAnimatedValue = function(animatedValue) {
for (var prop in animatedValue) {
if (animatedValue.hasOwnProperty(prop)) {
this.node.style[prop] = this.getStringValue(this.initValue[prop] + animatedValue[prop]);
}
}
};
Animation.prototype.stopAnimation = function() {
clearInterval(this.animationLoop);
if ((typeof this.options.onEnd) == 'function') {
this.options.onEnd();
}
};
/**
* Возвращает значения с пикселями
* @param digitalValue число
* @returns {string}
*/
Animation.prototype.getStringValue = function(digitalValue) {
return digitalValue + "px";
};
/**
* Получить исходные стили элемента
* @returns {{}}
...