纯 JavaScript 中的以下 jQuery 动画的等价物是什么?
function animate(element, position, speed) {
  $(element).animate({
    "top": position
  }, speed);
}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>纯 JavaScript 中的以下 jQuery 动画的等价物是什么?
function animate(element, position, speed) {
  $(element).animate({
    "top": position
  }, speed);
}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>setTimeout您可以使用和setInterval方法使用纯 javascript 实现复杂的动画。
请在此处查看。
这是移动元素的关键部分:
function move(elem) {
    var left = 0
    function frame() {
        left++  // update parameters
        elem.style.left = left + 'px' // show frame
        if (left == 100)  // check finish condition
            clearInterval(id)
    }
    var id = setInterval(frame, 10) // draw every 10ms
}
这个版本使用 vanilla javascript .animate() 函数,它比 requestAnimation 框架更好或更高性能。& 它也是 JQuerys .animate() 的合适替代品。
您可以使用迭代、计时功能和填充方法以及将其与其他动画进行菊花链
document.getElementById("Elem");
         Elem.style.position = "absolute";
         Elem.animate({
              top: ['8px', '280px']
                 },{ duration: 1760,        // number in ms [this would be equiv of your speed].
                     easing: 'ease-in-out',
                     iterations: 1,         // infinity or a number.
                  // fill: ''
        });   
我相信 setTimeout 和 setInterval 函数都使用 unsafe eval() 函数,但不是 100% 确定这一点,只要记住阅读一篇关于它的文章......
不要引用我的话!只是研究一下,
但是我写的代码已经过测试可以正常工作..希望这对某人有所帮助...
Element.animate() 函数似乎非常简单和有用。但是目前存在很多兼容性问题。你可以阅读它:
https://developer.mozilla.org/en-US/docs/Web/API/Element/animate
我建议习惯于 requestAnmationFrame。它与所有浏览器兼容,并且非常强大:
setInterval() 方法对于浏览器来说太重了,所以最好用 requestAnimationFrame() 来做动画。以下代码是使用此方法的示例。
let _btns = document.querySelectorAll('.btn'),
        _start = null;
let _loop = function (timestamp, duration, position, wrap) {
        if (!_start) _start = timestamp;
        let progress = (timestamp - _start) / duration;
        wrap.style.left = progress * position + 'px'
        if ( progress < 1 ) {
            window.requestAnimationFrame( function (timestamp){
                _loop(timestamp,duration,position,wrap);
            } );
        } else {
            _start = null;
        }
    },
    _animation = function () {
        const wrap = document.querySelector('.logo-2'),
            position = 300, // 300 pixels
            duration = 500; // 500 milliseconds
        _loop(0,duration,position,wrap);
    },
    _addEvents = function () {
        [].forEach.call(_btns,function(el){
            el.addEventListener('click', function (e) {
                _animation();
            })
        });
    },
    _init = function() {
        _addEvents();
    };
_init();