【问题标题】:jQuery animate - duration not workingjQuery动画 - 持续时间不起作用
【发布时间】:2023-03-23 14:55:01
【问题描述】:

js:

var time, key, speed, noteTime;
function readTextFile(file) {
  var rawFile = new XMLHttpRequest();
  rawFile.open("GET", file, false);
  rawFile.onreadystatechange = function () {
    if(rawFile.readyState === 4) {
      if(rawFile.status === 200 || rawFile.status == 0) {
        var allText = rawFile.responseText;
        var occ = occurrences(allText, "\n", "false");
        var string = allText.split("\n");
        for (i = 0; i < occ; i++) {
          time = string[i].split("{time:");
          time = time[1].split(",key:");
          key = string[i].split(",key:");
          key = key[1].split(",speed:");
          speed = string[i].split(",speed:");
          speed = speed[1].split("}");
          speed = speed[0];
          noteTime = time[0] - 3000;
          if(noteTime < 0) {
            noteTime = 0;
          }
          console.log(noteTime);
          console.log(speed);
          setTimeout(function(){
            $("#test").animate({
              left: "+=100"
            }, speed);
          }, noteTime);
        }
      }
    }
  }
  rawFile.send(null);
}
readTextFile("mercury.myr");

html:

<div style="height:50px; width:50px; background-color: black; position: relative;" id="test"></div>

mercury.myr:

{time:7000,key:32,speed:3000}\n

..我认为这是正确的代码,但不是。
jQuery animate() 工作正常,但不是 3000 毫秒。
这与 500 毫秒一样快 ..!
我已多次更改 HTML、CSS 和 JS,但这不是固定的。
我该如何解决?

【问题讨论】:

  • 我想以 3000 毫秒的速度移动#test div。
  • 我想以 3000ms 的速度移动 #test div,但 #test div 以大约 500ms 的速度移动。
  • 和3000一样
  • 这是一个调试问题
  • 你要求别人调试你的代码吗?

标签: javascript jquery jquery-animate duration


【解决方案1】:

setTimeout(function(){
            $("#test").animate({
              left: "+=100"
            }, speed);
          }, noteTime);

speed 替换为 3000

【讨论】:

  • 请检查问题中写的代码。它由 readTextFile() 函数定义。
  • 对不起,我解决了这个问题。它通过使用 Number 函数将速度转换为数字来工作。
  • 这意味着,我的回答很有帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-20
  • 1970-01-01
  • 2013-03-16
相关资源
最近更新 更多