jQuery animate函数在setInterval内的使用

huangapple go评论57阅读模式
英文:

Usage of jQuery animate function inside a set Interval

问题

在任务2中,当我在setInterval内使用animate函数时,出现了一个奇怪的行为,我不太理解。以下是您的代码:

// 任务2
let interv;
var direction = 1;
$('input:eq(1)').click(function () {
  let input = $(this);
  var image = $('img');
  if (input.val() === 'Animate') {
    input.val('Stop');

    interv = setInterval(function () {
      if (image.position().left >= 398) {
        direction = -1;
      } else if (image.position().left <= 0) {
        direction = 1;
      }
      image.animate({ left: '+=' + 10 * direction }, 20);
    }, 100);
  } else if (input.val() === 'Stop') {
    input.val('Animate');
    clearInterval(interv);
    image.stop();
  }
});

如果将动画的时间改为1毫秒,setInterval的时间改为3毫秒,代码会崩溃。这很奇怪,因为逻辑似乎并没有改变。是否有人可以解释这种行为,或者告诉我应该在哪里寻找解决方法。

英文:

there is a weird behavior that I don't understand when I use the animate function inside the setInterval.

here is the code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

//task2
let interv;
var direction = 1;
$(&#39;input:eq(1)&#39;).click(function () {
  let input = $(this);
  var image = $(&#39;img&#39;);
  if (input.val() === &#39;Animate&#39;) {
    input.val(&#39;Stop&#39;);

    interv = setInterval(function () {
      if (image.position().left &gt;= 398) {
        direction = -1;
      } else if (image.position().left &lt;= 0) {
        direction = 1;
      }
      image.animate({ left: &#39;+=&#39; + 10 * direction }, 20);
    }, 100);
  } else if (input.val() === &#39;Stop&#39;) {
    input.val(&#39;Animate&#39;);
    clearInterval(interv);
    image.stop();
  }
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
    &lt;title&gt;JQuery&lt;/title&gt;
    &lt;script src=&quot;jquery-3.6.4.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;Lab.js&quot; defer&gt;&lt;/script&gt;
    &lt;style&gt;
      .container1 {
        margin: auto;
        margin-bottom: 100px;
        width: fit-content;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
      }
      #div1 {
        border: 2px solid black;
        width: 500px;
        height: 200px;
        display: inline-block;
        margin-bottom: 20px;
      }
      #div2 {
        border: 2px solid black;
        width: 500px;
        height: 200px;
        display: inline-block;
      }
      #div3 {
        border: 2px solid black;
        width: 500px;
        height: 200px;
        display: inline-block;
        position: relative;
      }

      img {
        border: 1px solid black;
        position: absolute;
      }
    &lt;/style&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;div class=&quot;container1&quot;&gt;
      &lt;div&gt;&lt;h1&gt;Task 1&lt;/h1&gt;&lt;/div&gt;
      &lt;div id=&quot;div1&quot;&gt;
        &lt;ul&gt;
          &lt;li&gt;OS&lt;/li&gt;
          &lt;li&gt;SD&lt;/li&gt;
          &lt;li&gt;Java&lt;/li&gt;
          &lt;li&gt;Elearning&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/div&gt;

      &lt;div id=&quot;div2&quot;&gt;
        &lt;ul&gt;&lt;/ul&gt;
      &lt;/div&gt;
      &lt;input
        style=&quot;margin-left: 400px; background-color: blueviolet&quot;
        type=&quot;button&quot;
        value=&quot;&gt;&gt;&quot;
      /&gt;
    &lt;/div&gt;

    &lt;div class=&quot;container1&quot;&gt;
      &lt;div&gt;&lt;h1&gt;Task 2&lt;/h1&gt;&lt;/div&gt;
      &lt;div id=&quot;div3&quot;&gt;
        &lt;img
          style=&quot;padding-top: 50px&quot;
          src=&quot;facebook.png&quot;
          alt=&quot;&quot;
          width=&quot;100&quot;
          height=&quot;100&quot;
        /&gt;
      &lt;/div&gt;

      &lt;input
        style=&quot;margin-left: 50px; background-color: lime&quot;
        type=&quot;button&quot;
        value=&quot;Animate&quot;
      /&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

look at task 2, the animate button starts an interval, the image will move until it reaches the end of the div then move backward. the animation button (now is stop) when clicked again stops the interval. the code is working fine at this point.

if you change the time of the animation to 1 millisecond and the time of the set interval to 3 milliseconds the code crashes. which is weird because the logic didn't even change.

can anyone explain this behavior or tell me where should I search for this.

答案1

得分: 1

以下是您提供的代码的翻译部分:

function slideLeft(target) {
    target.animate({left: "-=10"}, 100, "swing", () => {
        if (target.position().left == 0) {
            slideRight(target);
        } else {
            slideLeft(target);
        }
    });
}
function slideRight(target) {
    let maxRight = target.parent().width() - target.width();
    target.animate({left: "+=10"}, 100, "swing", () => {
        if (target.position().left >= maxRight) {
            slideLeft(target);
        } else {
            slideRight(target);
        }
    });
}

let interv;
var direction = 1;
$('input:eq(1)').click(function () {
    let input = $(this);
    var image = $('img');
    if (input.val() === 'Animate') {
        input.val('Stop');
        slideRight(image);
    } else if (input.val() === 'Stop') {
        input.val('Animate');
        image.stop();
    }
});

这是您提供的JavaScript部分的翻译。

英文:

The interval timing is getting screwed up. In my answer I replaced the Interval by using the animation's complete function. Inside that function I check the current position to determine if the slide function should get ran again or switched so it slides back.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function slideLeft(target) {
    target.animate({left: &quot;-=10&quot;}, 100, &quot;swing&quot;, () =&gt; {
      if(target.position().left == 0){slideRight(target)}
      else{slideLeft(target)}
    });
}
function slideRight(target) {
    let maxRight = target.parent().width() - target.width();
    target.animate({left: &quot;+=10&quot;}, 100, &quot;swing&quot;, () =&gt; {
      if(target.position().left &gt;= maxRight){slideLeft(target)}
      else{slideRight(target)}
    });
}


let interv;
var direction = 1;
$(&#39;input:eq(1)&#39;).click(function () {
  let input = $(this);
  var image = $(&#39;img&#39;);
  if (input.val() === &#39;Animate&#39;) {
    input.val(&#39;Stop&#39;);
    slideRight(image);
  } else if (input.val() === &#39;Stop&#39;) {
    input.val(&#39;Animate&#39;);
    image.stop();
  }
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
    &lt;title&gt;JQuery&lt;/title&gt;
    &lt;script src=&quot;jquery-3.6.4.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;Lab.js&quot; defer&gt;&lt;/script&gt;
    &lt;style&gt;
      .container1 {
        margin: auto;
        margin-bottom: 100px;
        width: fit-content;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
      }
      #div1 {
        border: 2px solid black;
        width: 500px;
        height: 200px;
        display: inline-block;
        margin-bottom: 20px;
      }
      #div2 {
        border: 2px solid black;
        width: 500px;
        height: 200px;
        display: inline-block;
      }
      #div3 {
        border: 2px solid black;
        width: 500px;
        height: 200px;
        display: inline-block;
        position: relative;
      }

      img {
        border: 1px solid black;
        position: absolute;
      }
    &lt;/style&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;div class=&quot;container1&quot;&gt;
      &lt;div&gt;&lt;h1&gt;Task 1&lt;/h1&gt;&lt;/div&gt;
      &lt;div id=&quot;div1&quot;&gt;
        &lt;ul&gt;
          &lt;li&gt;OS&lt;/li&gt;
          &lt;li&gt;SD&lt;/li&gt;
          &lt;li&gt;Java&lt;/li&gt;
          &lt;li&gt;Elearning&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/div&gt;

      &lt;div id=&quot;div2&quot;&gt;
        &lt;ul&gt;&lt;/ul&gt;
      &lt;/div&gt;
      &lt;input
        style=&quot;margin-left: 400px; background-color: blueviolet&quot;
        type=&quot;button&quot;
        value=&quot;&gt;&gt;&quot;
      /&gt;
    &lt;/div&gt;

    &lt;div class=&quot;container1&quot;&gt;
      &lt;div&gt;&lt;h1&gt;Task 2&lt;/h1&gt;&lt;/div&gt;
      &lt;div id=&quot;div3&quot;&gt;
        &lt;img
          style=&quot;padding-top: 50px&quot;
          src=&quot;facebook.png&quot;
          alt=&quot;&quot;
          width=&quot;100&quot;
          height=&quot;100&quot;
        /&gt;
      &lt;/div&gt;

      &lt;input
        style=&quot;margin-left: 50px; background-color: lime&quot;
        type=&quot;button&quot;
        value=&quot;Animate&quot;
      /&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月31日 22:43:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定