jQuery animate function equivalent in pure JavaScript

后端 未结 3 1151
别那么骄傲
别那么骄傲 2020-12-29 05:35

What is the equivalent of the following jQuery animate in pure JavaScript?

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 06:02

    You can acheive complex animations with pure javascript by using setTimeout and setInterval methods.

    Please check here.

    Here is the key part of moving an element:

    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
    }
    

提交回复
热议问题