变量为 true,但图像未转换为 Gifs - 类 / ElementID

Variable is true but images are not turning to Gifs - Class / ElementID

提问人:Yosuf 提问时间:4/18/2023 最后编辑:Scott MarcusYosuf 更新时间:4/18/2023 访问量:39

问:

我正在尝试将图像转换为 gif,而不是当 GifsActive 为 true 时,当有人勾选该框时。

我试图在类内和类外创建函数并调用它们,但这似乎不起作用,我创建了this.gif = gifs并尝试调用一个函数将其从false设置为true,但它仍然不起作用。

class Queen {

   constructor(name, acting, comedy, dance, design, improv, runway, lipsync, image = "noimage", custom = false, guestjudge = false, country = "none", gifs = false) {
      this.trackRecord = [];
      this.friends = [];
      this.enemies = [];
      this.sisters = [];
      this.miniEpisode = [];
      this.tCaptain = [];
      this.voteHerstory = [];
      this.immuneEp = [];
      this.runwayScore = 0;
      this.lipsyncScore = 0;
      this.performanceScore = 0;
      this.finaleScore = 0;
      this.favoritism = 0;
      this.unfavoritism = 0;
      this.ppe = 0;
      this.stars = 0;
      this.episodesOn = 0;
      this.votes = 0;
      this.rankP = 0;
      this.retEp = 0;
      this.blocked = false;
      this.QueenDisqOrDept = false;
      this.customqueen = false;
      this.immune = false;
      this.chocolate = false;
      this.maxiT = false;
      this._name = name;
      this._actingStat = acting;
      this._comedyStat = comedy;
      this._danceStat = dance;
      this._designStat = design;
      this._improvStat = improv;
      this._runwayStat = runway;
      this._lipsyncStat = lipsync;

      if (image == "noimage")
         this.image = "image/queens/noimage.jpg";
      else if (custom == true)
         this.image = image;
      else if (guestjudge == true)
         this.image = "image/judges/" + image + ".webp";
      else if (gifsActive == true)
         this.image = "image/judges/" + image + ".gif";
      else
         this.image = "image/queens/" + image + ".webp";

      if (country == "none")
         this.country = "image/country/noimage.jpg";
         
      else if (country == "UK")
         this.country = "image/country/UK.png";

      else if (country == "USA")
         this.country = "image/country/USA.png";

      else if (country == "SWEDEN")
         this.country = "image/country/SWEDEN.png";

      else if (country == "CAN")
         this.country = "image/country/CAN.png";

      else if (country == "THAI")
         this.country = "image/country/THAI.png";

      else if (country == "BELGIUM")
         this.country = "image/country/BELGIUM.png";

      else if (country == "DU")
         this.country = "image/country/DU.png";

      else if (country == "SPAIN")
         this.country = "image/country/SPAIN.png";

      else if (country == "FRANCE")
         this.country = "image/country/FRANCE.png";

         else if (country == "DEN")
         this.country = "image/country/DEN.png";

      else if (country == "AUS")
         this.country = "image/country/AUS.png";

      else if (country == "INDIA")
         this.country = "image/country/INDIA.png";

      else if (country == "BRAZIL")
         this.country = "image/country/BRAZIL.png";

      else if (country == "ISRAEL")
         this.country = "image/country/ISRAEL.png";

      else if (country == "PHIL")
         this.country = "image/country/PHIL.png";

      else if (country == "CHINA")
         this.country = "image/country/CHINA.png";

      else if (country == "HOL")
         this.country = "image/country/HOL.png";

      else if (country == "ITALY")
         this.country = "image/country/ITALY.png";

      else
         this.country = "image/queens/" + country + ".png";
   }

   _calculateScores(min, max, stat = 0) {
      let score = randomNumber(min, max);
      return score - stat;
   }
   getName() {
      return this._name;
   }
   getLipSyncStat() {
      return this._lipsyncStat;
   }
   getActing() {
      this.performanceScore = this._calculateScores(15, 35, this._actingStat);
   }
   getComedy() {
      this.performanceScore = this._calculateScores(15, 35, this._comedyStat);
   }
   getMarketing() {
      this.performanceScore = this._calculateScores(25, 45, this._comedyStat + this._actingStat);
   }
   getDance() {
      this.performanceScore = this._calculateScores(15, 35, this._danceStat);
   }
   getDesign() {
      this.performanceScore = this._calculateScores(15, 35, this._designStat);
   }
   getRunwayChallenge() {
      this.performanceScore = this._calculateScores(15, 35, this._runwayStat);
   }
   getImprov() {
      this.performanceScore = this._calculateScores(15, 35, this._improvStat);
   }
   //special 'gets':
   getSnatch() {
      this.performanceScore = this._calculateScores(25, 45, this._improvStat + this._comedyStat);
   }
   getRusical() {
      this.performanceScore = this._calculateScores(25, 45, this._danceStat + this._lipsyncStat);
   }
   getBall() {
      this.performanceScore = this._calculateScores(25, 45, this._designStat + this._runwayStat);
   }
   getRumix() {
      this.performanceScore = this._calculateScores(25, 45, this._danceStat + this._improvStat);
   }
   getTalentShow() {
      this.performanceScore = this._calculateScores(15, 35, randomNumber(1, 35));
   }
   getFinale() {
      this.finaleScore = this.favoritism - this.unfavoritism;
   }
   getRunway() {
      this.runwayScore = this._calculateScores(12, 35, this._runwayStat);
   }
   getLipsync() {
      this.lipsyncScore = this._calculateScores(0, this._lipsyncStat, this.unfavoritism) + this.favoritism;
   }
   getASLipsync() {
      this.lipsyncScore = this._calculateScores(0, this._lipsyncStat);
   }
   addToTrackRecord(placement) {
      this.trackRecord.push(placement);
   }
   editTrackRecord(added) {
      this.trackRecord[this.trackRecord.length - 1] += added;
   } 
}

用于将变量变为 true 的按钮

let gifsActive = false;

if (document.getElementById("gifMode").checked == true) {
      gifsActive = true;
   }

gifMode 在 HTML 中作为复选框,并且拼写正确。

这是我试图更改为 gif 的人

let sashaColby = new Queen("Sasha Colby", 12, 9, 13, 10, 8, 14, 14, "SashaColby", false, false,"USA", false);

第二个字符串“”是作为WEBP的图像,但我在同一个文件夹中还有一个GIF格式的同名,以确保当它切换时,它应该只改变格式。

HTML ---

<input class="optleft" type="checkbox" id="gifMode"> Enable Gifs

任何帮助都是值得赞赏的,我已经尝试了几个小时,但无论我做什么,它都行不通。

JavaScript 变量 构造函数 GetElementById

评论

1赞 Scott Marcus 4/18/2023
"我正在尝试将图像转换为gif,而不是当GifsActive为真时,当有人勾选框时。<<--- ??什么?
0赞 Yosuf 4/18/2023
@mykaf我添加了 HTML - 该复选框在网站上被选中,其余复选框工作正常。
0赞 Yosuf 4/18/2023
@ScottMarcus我的文件夹中有 2 张图片,1 张是 WEBP,1 张是 GIF。我想将格式从 WEBP 更改为 GIF,以便在勾选 gif 复选框时选择 GIF。
0赞 Scott Marcus 4/18/2023
我试图了解您发布的代码与您提出的问题有什么关系。如果您有多种可用的文件格式,并且只是想在复选框选择时显示一种格式,只需使用 CSS 来显示/隐藏适当的文件类型。
1赞 Scott Marcus 4/18/2023
此外,尽管在 it/else 语句的分支代码周围省略大括号是合法的,但这仅适用于在该分支中要执行一个语句的情况。省略大括号会为下面的错误打开大门,如果你添加第二个语句但忘记添加大括号。因此,作为最佳实践,应始终将分支代码放在大括号内。

答:

1赞 Yosuf 4/18/2023 #1

我更改了图像 src dom 元素以显示 gif 而不是带有 .replaceALL 的 webp,它现在可以工作了!

谢谢你们的帮助!