Tell me why the animation is not smooth?
window.addEventListener('DOMContentLoaded', () => { let btn = document.getElementsByTagName('button')[0]; function myAnimation() { let box = document.querySelector('.box'), boxWidth = parseFloat(getComputedStyle(box).width), boxHeight = parseFloat(getComputedStyle(box).height), elem = document.querySelector('.square'), elemWidth = parseFloat(getComputedStyle(elem).width), elemHeight = parseFloat(getComputedStyle(elem).height), pos_top = 0, pos_left = 0, id = setInterval(frame, 100); function frame() { if(pos_top == 0 && pos_left == 0) { for(let i = 0; i < (boxWidth - elemWidth); i++) { pos_left++; elem.style.left = pos_left + 'px'; } } else if(pos_top == 0 && pos_left == 300) { for(let i = 0; i < (boxHeight - elemHeight); i++) { pos_top++; elem.style.top = pos_top + 'px'; } } else if(pos_top == 300 && pos_left == 300) { for(let i = 0; i < (boxWidth - elemWidth); i++) { pos_left--; elem.style.left = pos_left + 'px'; } } else if(pos_top == 300 && pos_left == 0) { for(let i = 0; i < (boxHeight - elemHeight); i++) { pos_top--; elem.style.top = pos_top + 'px'; } } } } btn.addEventListener('click', myAnimation); });