简体   繁体   English

非线性 DOM 动画

[英]Non-Linear DOM Animations

Question: WHAT THE "myMove()" METHOD DOES?问题:“myMove()”方法有什么作用? Can you explain it?你能解释一下吗?

#myContainer { width: 400px; #myContainer { 宽度:400px; height: 400px; 高度:400px; position: relative; 位置:相对; background: yellow; 背景:黄色; } #myAnimation { width: 50px; } #myAnimation { 宽度:50px; height: 50px; 高度:50px; position: absolute; 位置:绝对; background-color: red; 背景颜色:红色; } }

Click Me点击我

function myMove() { var elem = document.getElementById("myAnimation"); function myMove() { var elem = document.getElementById("myAnimation"); var pos = 0; 变量位置 = 0; var id = setInterval(frame, 10); var id = setInterval(frame, 10); function frame() { if (pos == 350) { clearInterval(id); function frame() { if (pos == 350) { clearInterval(id); } else { pos++; } else { pos++; elem.style.top = pos + 'px'; elem.style.top = pos + 'px'; elem.style.left = pos + 'px'; elem.style.left = pos + 'px'; } } } } } }

Quick note: In this answer, I'll be assuming the final question reads:快速说明:在这个答案中,我假设最后一个问题是:

What does the `myMove()` method do?

Let's first take a look at the variables:我们先来看看变量:

var elem = document.getElementById("myAnimation");  

var pos = 0;

var id = setInterval(frame, 10);

The variable elem refers to the red box which is being moved across the canvas (a div element).变量elem指的是在画布上移动的红色框(一个 div 元素)。 We set the pos tick variable to 0, which will control how many times we will move the box across the screen.我们将pos tick 变量设置为 0,这将控制我们将在屏幕上移动框的次数。 Finally, id refers to a setInterval function which calls the defined frame function every 10 milliseconds.最后, id指的是setInterval函数,它每 10 毫秒调用一次定义的frame函数。

Now let's look at the function frame :现在让我们看看函数frame

function frame() {
  if (pos == 350) {
    clearInterval(id);
  } else {
    pos++;
    elem.style.top = pos + 'px';
    elem.style.left = pos + 'px';
  }
}

First, it runs a quick check to see if the ticks are done, or in other words, checks to see if the position is 350. Then, if it is, it stops the loop.首先,它运行快速检查以查看刻度是否完成,或者换句话说,检查位置是否为 350。然后,如果是,则停止循环。 If it is not, it makes the box move down 1 pixel by setting the position from the top to the pos variable.如果不是,它通过设置从顶部到pos变量的位置使框向下移动 1 个像素。

All of this code is contained within the myMove() function.所有这些代码都包含在myMove()函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM