第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

匹配圖片 javascript 基本益智游戲

匹配圖片 javascript 基本益智游戲

慕田峪9158850 2023-07-29 16:50:54
我正在制作一個(gè)簡(jiǎn)單的 JavaScript 游戲,我是一個(gè)初學(xué)者,但現(xiàn)在我陷入了困境。我必須創(chuàng)建 5 個(gè)數(shù)組來(lái)存儲(chǔ) src 圖像。這是我的代碼:var imagesPart1 = [  "funny-cat1_part1x1.jpg",  "monkey_part1x1.jpg",  "panda_swap_part1x1.jpg"];var imagesPart2 = [  "funny-cat1_part2x1.jpg",  "monkey_part2x1.jpg",  "panda_swap_part2x1.jpg"];var imagesPart3 = [  "funny-cat1_part3x1.jpg",  "monkey_part3x1.jpg",  "panda_swap_part3x1.jpg"];var imagesPart4 = [  "funny-cat1_part4x1.jpg",  "monkey_part4x1.jpg",  "panda_swap_part4x1.jpg"];var imagesPart5 = [  "funny-cat1_part5x1.jpg",  "monkey_part5x1.jpg",  "panda_swap_part5x1.jpg"];function imageSwitch(id, arr) {  var element = document.getElementById(id);  var counter = Math.floor((Math.random() * 5));  function nextPic() {    counter += 1;    if (counter > arr.length - 1) {      counter = 0;    }    element.src = "./img/" + arr[counter];  }  element.addEventListener('click', nextPic);  counter -= 1;  nextPic();}imageSwitch("one", imagesPart1);imageSwitch("two", imagesPart2);imageSwitch("three", imagesPart3);imageSwitch("four", imagesPart4);imageSwitch("five", imagesPart5);div {  width: 800px;  height: 100%;}img {  width: 100%;  height: 160px;  max-width: 100%;}<div>  <div>    <img id="one" src="./img/funny-cat1_part1x1.jpg" alt="">    <img id="two" src="./img/funny-cat1_part2x1.jpg" alt="">    <img id="three" src="./img/funny-cat1_part3x1.jpg" alt="">    <img id="four" src="./img/funny-cat1_part4x1.jpg" alt="">    <img id="five" src="./img/funny-cat1_part5x1.jpg" alt="">  </div></div>現(xiàn)在我想要如果 5 個(gè)部分組成一個(gè)完整的圖像,它會(huì)發(fā)出警報(bào)或給出這樣的框陰影,但我不知道如何獲得理想的效果。這是一個(gè)工作小提琴。提前致謝!
查看完整描述

2 回答

?
白豬掌柜的

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊

您需要稍微重構(gòu)一下代碼:


// switch this to a 2d array

const imageParts = [

    [

        "https://i.ibb.co/4FrYJR3/funny-cat1-part1x1.jpg",

        "https://i.ibb.co/D9TLmt1/monkey-part1x1.jpg",

        "https://i.ibb.co/V2BXPVH/panda-swap-part1x1.jpg",

    ],

    [

        "https://i.ibb.co/CQyGWQq/funny-cat1-part2x1.jpg",

        "https://i.ibb.co/BgT0mNN/monkey-part2x1.jpg",

        "https://i.ibb.co/P6CGThy/panda-swap-part2x1.jpg",

    ],

    [

        "https://i.ibb.co/k07vFCb/funny-cat1-part3x1.jpg",

        "https://i.ibb.co/dbx5zcc/monkey-part3x1.jpg",

        "https://i.ibb.co/FK3h9Zd/panda-swap-part3x1.jpg",

    ],

    [

        "https://i.ibb.co/Vm5ZJ0g/funny-cat1-part4x1.jpg",

        "https://i.ibb.co/10hDp8c/monkey-part4x1.jpg",

        "https://i.ibb.co/HXWrzjs/panda-swap-part4x1.jpg",

    ],

    [

        "https://i.ibb.co/x8zQPWW/funny-cat1-part5x1.jpg",

        "https://i.ibb.co/2KKwwzG/monkey-part5x1.jpg",

        "https://i.ibb.co/N9vgw6y/panda-swap-part5x1.jpg",

    ],

];


// store ids in an array

const ids = ["one", "two", "three", "four", "five"];


// store counters in an array

const counters = new Array(ids.length).fill(0);


// helper function to check if every element in an array is equal

function checkAllSame(arr) {

  if (arr.length <= 1) {

    return true;

  }

  const first = arr[0];

  for (let i = 1; i < arr.length; i ++) {

    if (arr[i] !== first) {

      return false;

    }

  }

  return true;


// loop through the element ids

for (let i = 0; i < ids.length; i ++) {

  // get the current element

  const ele = document.getElementById(ids[i]);

  // get the current image parts

  const parts = imageParts[i];

  // initialize the counter to a random value

  let counter = Math.floor(Math.random() * parts.length);

  // update the counters array

  counters[i] = counter;

  // update the element

  ele.src = parts[counter];

  ele.addEventListener("click", function() {

    // on click increment the counter and use % to wrap around

    counter = (counter + 1) % parts.length;

    // update the element

    ele.src = parts[counter];

    // update the counters array

    counters[i] = counter;

    // if all the counters are the same, then it's a match

    if (checkAllSame(counters)) {

      // or whatever else you want to do

      // setTimeout of 0 lets the image load before the alert pops up

      setTimeout(function() {alert("you did it!")}, 0);

    }

  });

}

工作小提琴。



查看完整回答
反對(duì) 回復(fù) 2023-07-29
?
守候你守候我

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊

我會(huì)創(chuàng)建一個(gè)新函數(shù)setImage(arr, counter),其中包含您的 line element.src = "./img/" + arr[counter];,但也執(zhí)行其他操作:

  • 它應(yīng)該將索引存儲(chǔ)在一個(gè)全局變量中,該變量包含所有圖像的索引。

  • 它應(yīng)該檢查這些是否都相同

  • 它應(yīng)該根據(jù)檢查執(zhí)行您想要看到的任何視覺(jué)反饋。

您有一個(gè)障礙,那就是您傳遞給的數(shù)組imageSwitch沒(méi)有任何類型的標(biāo)識(shí)符...您可能希望將所有 imagePart 放入一個(gè)數(shù)組中(啊,我看到另一個(gè)答案也表明了這一點(diǎn))并傳遞一個(gè)指數(shù)?;蛘呖赡苁且粋€(gè)以你的 id 為鍵的對(duì)象,例如:

{
  one: [...],
  two: [...],
  ...
}


查看完整回答
反對(duì) 回復(fù) 2023-07-29
  • 2 回答
  • 0 關(guān)注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)