Согласно совету @ Rory, я создал простой модуль для решения моей проблемы. Я поделюсь этим с кратким описанием для людей, которые имеют такую же проблему, как я, в ближайшем будущем.
Основная функция этого модуля - Неограниченное накопление . В отличие от метода .css()
в jQuery, на накопление не влияет свойство transition
CSS.
Спасибо за совет @RoryMcCrossan: -)
========= ПОСЛЕДНИЕ ОБНОВЛЕНИЯ =========
Теперь пользователь может накапливать любые виды unit
, такие как px
, cm
, rem
,
даже %
.
см. В Github
// @BUG FIXED
// @CHANGED THE CODE MORE CLEARER
// @USAGE: Operator.assemble(target, increment, property, unit);
const Operator = (function() {
function Accumulate(init, acc, name, unit) {
this.init = document.querySelector(init);
this.acc = acc;
this.name = name;
this.unit = unit;
this.convert(this.result);
}
Accumulate.prototype = {
convert: function(callback) {
let defaultDisplay = this.init.style.display;
this.init.style.display = 'none';
let bind = callback.bind(this),
getValues = window.getComputedStyle(this.init, null).getPropertyValue(this.name),
S2N = parseInt(getValues, 10);
this.init.style.display = defaultDisplay;
bind(S2N);
},
result: function(value) {
let getNewValue = value + this.acc;
this.init.style.left = getNewValue + this.unit;
}
}
return {
assemble: (init, acc, name, unit) => {
new Accumulate(init, acc, name, unit);
}
}
}());
//==============================================
setInterval(() => {
Operator.assemble('.content', 10, 'left', '%');
}, 1000);
#box{
max-width: 2560px;
background-color: gold;
}
.content {
left: 10%; /* 10px */
position: relative;
transition: 1000ms;
}
<div id="box">
<div class="content">
wowtested
</div>
</div>