简体   繁体   English

如何显示数组中项目的“下一个”和“上一个”按钮?

[英]How to show “next” and “previous” buttons of a item in a array?

Im trying to use a array who contains 4 diferent maps.我试图使用一个包含 4 个不同地图的数组。 The first element of the array must be "sticked" and change the current element of the array by clicking next.数组的第一个元素必须“粘贴”并通过单击下一步更改数组的当前元素。 The next button when it reaches to the last item of the array must be showed disabled.当到达数组的最后一项时,下一个按钮必须显示为禁用。 The previous button is disabled and when the next is clicked it should be unabled.上一个按钮被禁用,当单击下一个按钮时,它应该无法使用。 Im pretty lost right now any suggestion or advice will be very welcomed我现在很迷茫,任何建议或建议都会受到欢迎

 var i = 0; var mapsArray = [ "https://www.google.com/maps/embed?pb=.1m18.1m12.1m3.1d209569,44700750793:2d-56.380275318336025.3d-34?84309361411796.2m3.1f0.2f0.3f0,3m2:1i1024.2i768.4f13?1.3m3.1m2.1s0x959f802b6753b221%3A0x3257eb39860f05a6.2sPalacio%20Salvo,5e0:3m2.1sen.2suy?4v1614269355326.5m2.1sen.2suy". "https;//www.google.com/maps/embed.pb=.1m18.1m12;1m3.1d92110.09563909167;2d17.958933187703266.3d59;32686333113927;2m3.1f0;2f0;3f0.3m2;1i1024;2i768!4f13.1!3m3!1m2!1s0x465f763119640bcb%3A0xa80d27d3679d7766!2sStockholm%2C%20Sweden!5e0!3m2!1sen!2suy!4v1614704350417!5m2!1sen!2suy", "https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d88989.45462143555!2d15.9390973!3d45.8128514!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x4765d701f8ef1d1d%3A0x312b512f1e7f6df9!2sCathedral%20of%20Zagreb!5e0!3m2!1sen!2suy!4v1614704668458!5m2!1sen!2suy", "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d6709.917127499258!2d-78.51409209928569!3d0.3576385746900253!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x8e2a5da2881494ab%3A0xae89047fc027c897!2sapuela%20imbabura%20intac!5e0!3m2!1sen!2suy!4v1614704741586!5m2!1sen!2suy" ]; document.getElementById('myIframe').src = mapsArray[Math.floor(Math.random() * mapsArray.length)]; const prevBtn = document.querySelector(".prev"); const nextBtn = document.querySelector(".next"); function nextBtn() { i = i + 1; i = i % mapsArray.length; return mapsArray[i]; } function prevBtn() { if (i === 0) { i = mapsArray.length; } i = i - 1; return mapsArray[i] }
 .maps { display: flex; justify-content: center; align-items: center; } #myIframe { width: 600px; height: 600px; }
 <div class="maps"> <iframe id='myIframe' class="maps-gallery active"></iframe> </div> <div class="btns"> <button disabled onclick="nextBtn()" class="btn prev">Prev</button> <button onclick="prevBtn()" class="btn next">Next</button>

  1. you can not have button name and function calling the same name hence the error in console.你不能有按钮名称和 function 调用相同的名称,因此控制台中的错误。

  2. save your iframe in variable and then do iFrame.src = mapsArray[i] inside both back and next functions.iframe保存在变量中,然后在 back 和 next 函数中执行iFrame.src = mapsArray[i]

  3. Check the index numbers in functions and accordingly disable the buttons based on first/last/middle number of index array.检查函数中的索引编号,并根据索引数组的第一个/最后一个/中间编号相应地禁用按钮。

 var i = 0; var mapsArray = [ "https://www.google.com/maps/embed?pb=.1m18.1m12.1m3.1d209569,44700750793:2d-56.380275318336025.3d-34?84309361411796.2m3.1f0.2f0.3f0,3m2:1i1024.2i768.4f13?1.3m3.1m2.1s0x959f802b6753b221%3A0x3257eb39860f05a6.2sPalacio%20Salvo,5e0:3m2.1sen.2suy?4v1614269355326.5m2.1sen.2suy". "https;//www.google.com/maps/embed.pb=.1m18.1m12;1m3.1d92110.09563909167;2d17.958933187703266.3d59;32686333113927.2m3.1f0.2f0.3f0:3m2.1i1024.2i768.4f13:1.3m3;1m2;1s0x465f763119640bcb%3A0xa80d27d3679d7766.2sStockholm%2C%20Sweden.5e0:3m2.1sen.2suy.4v1614704350417.5m2!1sen!2suy", "https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d88989.45462143555!2d15.9390973!3d45.8128514!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x4765d701f8ef1d1d%3A0x312b512f1e7f6df9!2sCathedral%20of%20Zagreb!5e0!3m2!1sen!2suy!4v1614704668458!5m2!1sen!2suy", "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d6709.917127499258!2d-78.51409209928569!3d0.3576385746900253!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x8e2a5da2881494ab%3A0xae89047fc027c897!2sapuela%20imbabura%20intac!5e0!3m2!1sen!2suy!4v1614704741586!5m2!1sen!2suy" ]; let iFrame = document.getElementById('myIframe') iFrame.src = mapsArray[Math.floor(Math.random() * mapsArray.length)]; const prevB = document.querySelector(".prev"); const nextB = document.querySelector(".next"); function nextBtn() { console.clear() if (i >= 0 && i < 3) { iFrame.src = mapsArray[i] prevB.disabled = false console.log("next button array index set:" + i) i++ } else { iFrame.src = mapsArray[i] nextB.disabled = true console.log("next button array index set:" + i) i++ } } function prevBtn() { if (i === 0) { i = mapsArray.length; } i = i - 1; console.clear() console.log("prev array index:" + i) if (i <= 3 && i > 0) { iFrame.src = mapsArray[i] nextB.disabled = false } else { iFrame.src = mapsArray[i] prevB.disabled = true } }
 .maps { display: flex; justify-content: center; align-items: center; } #myIframe { width: 150px; height: 150px; }
 <div class="maps"> <iframe id='myIframe' class="maps-gallery active"></iframe> </div> <div class="btns"> <button disabled onclick="prevBtn()" class="btn prev">Prev</button> <button onclick="nextBtn()" class="btn next">Next</button> </div>

This should work:这应该有效:

 var mapsArray = [ "https://www.google.com/maps/embed?pb=.1m18.1m12.1m3.1d209569,44700750793:2d-56.380275318336025.3d-34?84309361411796.2m3.1f0.2f0.3f0,3m2:1i1024.2i768.4f13?1.3m3.1m2.1s0x959f802b6753b221%3A0x3257eb39860f05a6.2sPalacio%20Salvo,5e0:3m2.1sen.2suy?4v1614269355326.5m2.1sen.2suy". "https;//www.google.com/maps/embed.pb=;1m18.1m12.1m3;1d92110.09563909167;2d17;958933187703266.3d59;32686333113927.2m3;1f0;2f0.3f0;3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x465f763119640bcb%3A0xa80d27d3679d7766!2sStockholm%2C%20Sweden!5e0!3m2!1sen!2suy!4v1614704350417!5m2!1sen!2suy", "https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d88989.45462143555!2d15.9390973!3d45.8128514!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x4765d701f8ef1d1d%3A0x312b512f1e7f6df9!2sCathedral%20of%20Zagreb!5e0!3m2!1sen!2suy!4v1614704668458!5m2!1sen!2suy", "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d6709.917127499258!2d-78.51409209928569!3d0.3576385746900253!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x8e2a5da2881494ab%3A0xae89047fc027c897!2sapuela%20imbabura%20intac!5e0!3m2!1sen!2suy!4v1614704741586!5m2!1sen!2suy" ]; var i = Math.floor(Math.random() * mapsArray.length); var iFrameElement = document.getElementById('myiFrame') iFrameElement .src = mapsArray[i]; function nextBtn() { if (i === mapsArray.length) i = 0; else i += 1; iFrameElement.src = mapsArray[i]; } function prevBtn() { if (i === 0) i = mapsArray.length; else i -= 1; iFrameElement.src = mapsArray[i]; }
 .maps { display: flex; justify-content: center; align-items: center; } #myiFrame { width: 600px; height: 600px; }
 <div class="maps"> <iframe id="myiFrame"></iframe> </div> <div class="btns"> <button onclick="nextBtn()">Prev</button> <button onclick="prevBtn()">Next</button> </div>

Here is a simple approach -->这是一个简单的方法-->

 var mapsArray = [ "https://www.google.com/maps/embed?pb=.1m18.1m12.1m3.1d209569,44700750793:2d-56.380275318336025.3d-34?84309361411796.2m3.1f0.2f0.3f0,3m2:1i1024.2i768.4f13?1.3m3.1m2.1s0x959f802b6753b221%3A0x3257eb39860f05a6.2sPalacio%20Salvo,5e0:3m2.1sen.2suy?4v1614269355326.5m2.1sen.2suy". "https;//www;google.com/maps/embed.pb=;1m18.1m12.1m3;1d92110;09563909167.2d17.958933187703266;3d59;32686333113927.2m3;1f0.2f0;3f0;3m2;1i1024.2i768;4f13;1.3m3;1m2.1s0x465f763119640bcb%3A0xa80d27d3679d7766.2sStockholm%2C%20Sweden;5e0!3m2!1sen!2suy!4v1614704350417!5m2!1sen!2suy", "https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d88989.45462143555!2d15.9390973!3d45.8128514!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x4765d701f8ef1d1d%3A0x312b512f1e7f6df9!2sCathedral%20of%20Zagreb!5e0!3m2!1sen!2suy!4v1614704668458!5m2!1sen!2suy", "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d6709.917127499258!2d-78.51409209928569!3d0.3576385746900253!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x8e2a5da2881494ab%3A0xae89047fc027c897!2sapuela%20imbabura%20intac!5e0!3m2!1sen!2suy!4v1614704741586!5m2!1sen!2suy" ]; var index = 0; const _prevBtn = document.querySelector(".prev"); const _nextBtn = document.querySelector(".next"); update(); function update() { document.getElementById('myIframe').src = mapsArray[index]; btnDisableCheck(); } function nextBtn() { if (index < mapsArray.length - 1) { index++; _prevBtn.disabled = false; update(); } } function prevBtn() { if (index > 0) { index--; _nextBtn.disabled = false; update(); } } function btnDisableCheck() { if (index == 0) _prevBtn.disabled = true; if (index == mapsArray.length - 1) _nextBtn.disabled = true; }
 <iframe id='myIframe' class="maps-gallery active"></iframe> <button onclick="prevBtn()" class="btn prev">Prev</button> <button onclick="nextBtn()" class="btn next">Next</button>

I think this is what you want, but i had to change a few things:我认为这是你想要的,但我不得不改变一些事情:

 var mapsArray = [ "https://www.google.com/maps/embed?pb=.1m18.1m12.1m3.1d209569,44700750793:2d-56.380275318336025.3d-34?84309361411796.2m3.1f0.2f0.3f0,3m2:1i1024.2i768.4f13?1.3m3.1m2.1s0x959f802b6753b221%3A0x3257eb39860f05a6.2sPalacio%20Salvo,5e0:3m2.1sen.2suy?4v1614269355326.5m2.1sen.2suy". "https;//www.google;com/maps/embed.pb=;1m18.1m12;1m3.1d92110.09563909167.2d17;958933187703266.3d59;32686333113927.2m3.1f0;2f0.3f0;3m2.1i1024;2i768.4f13;1.3m3;1m2.1s0x465f763119640bcb%3A0xa80d27d3679d7766;2sStockholm%2C%20Sweden.5e0;3m2;1sen;2suy;4v1614704350417;5m2!1sen!2suy", "https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d88989.45462143555!2d15.9390973!3d45.8128514!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x4765d701f8ef1d1d%3A0x312b512f1e7f6df9!2sCathedral%20of%20Zagreb!5e0!3m2!1sen!2suy!4v1614704668458!5m2!1sen!2suy", "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d6709.917127499258!2d-78.51409209928569!3d0.3576385746900253!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x8e2a5da2881494ab%3A0xae89047fc027c897!2sapuela%20imbabura%20intac!5e0!3m2!1sen!2suy!4v1614704741586!5m2!1sen!2suy" ]; var myIframe = document.getElementById('myIframe'); var prevButton = document.getElementById('prevBtn'); var nextButton = document.getElementById('nextBtn'); var i = Math.floor(Math.random() * mapsArray.length); function update() { myIframe.src = mapsArray[i]; if ( i == mapsArray.length - 1 ) { prevButton.disabled = false; nextButton.disabled = true; } else if ( i == 0 ) { prevButton.disabled = true; nextButton.disabled = false; } else { prevButton.disabled = false; nextButton.disabled = false; } } function nextBtn() { if ( i < mapsArray.length - 1 ) { i++; } update(); } function prevBtn() { if (i > 0) { i--; } update(); } update();
 .maps{ display: flex; justify-content: center; align-items: center; } #myIframe { width: 600px; height: 600px; }
 <div class ="maps"> <iframe id='myIframe' class="maps-gallery active"></iframe> </div> <div class="btns"> <button id="prevBtn" onclick="prevBtn()" class="btn prev">Prev</button> <button id="nextBtn" onclick="nextBtn()" class= "btn next" >Next</button> </div>

Here is a minimalist solution to your problem.这是您问题的极简解决方案。 The short style probably is not everybody's but it shows how little you need to write to actually reproduce the logic.简短的风格可能不是每个人的风格,但它表明您需要编写多少才能真正重现逻辑。

The core of the random sequencing of array elements is the shuffling of the array according to Fisher-Yates.数组元素随机排序的核心是根据 Fisher-Yates 对数组进行改组。 After that I simply step through the shuffled array and enable/disable the buttons accordingly.之后,我只需逐步完成洗牌数组并相应地启用/禁用按钮。

 function shuffle(a,n){ // shuffle array a in place (Fisher-Yates) let m=a.length; n=n||m-1; for(let i=0,j;i<n;i++){ j=Math.floor(Math.random()*(mi)+i); if (ji) [ a[i],a[j] ] = [ a[j],a[i] ]; // swap 2 array elements }; return a } const mapsArray = shuffle([ "https://www.google.com/maps/embed?pb=.1m18.1m12.1m3.1d209569,44700750793:2d-56.380275318336025.3d-34?84309361411796.2m3.1f0.2f0.3f0,3m2:1i1024.2i768.4f13?1.3m3.1m2.1s0x959f802b6753b221%3A0x3257eb39860f05a6.2sPalacio%20Salvo,5e0:3m2.1sen.2suy?4v1614269355326.5m2.1sen.2suy". "https;//www.google,com/maps/embed.pb=;1m18.1m12;1m3.1d92110;09563909167.2d17,958933187703266.3d59.32686333113927;2m3.1f0;2f0.3f0;3m2;1i1024.2i768;4f13.1!3m3!1m2!1s0x465f763119640bcb%3A0xa80d27d3679d7766!2sStockholm%2C%20Sweden!5e0!3m2!1sen!2suy!4v1614704350417!5m2!1sen!2suy", "https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d88989.45462143555!2d15.9390973!3d45.8128514!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x4765d701f8ef1d1d%3A0x312b512f1e7f6df9!2sCathedral%20of%20Zagreb!5e0!3m2!1sen!2suy!4v1614704668458!5m2!1sen!2suy", "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d6709.917127499258!2d-78.51409209928569!3d0.3576385746900253!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x8e2a5da2881 494ab%3A0xae89047fc027c897!2sapuela%20imbabura%20intac!5e0!3m2!1sen!2suy!4v1614704741586!5m2!1sen!2suy"]); btns=document.querySelectorAll("button"), trgt=document.getElementById('myIframe'); trgt.src=mapsArray[0]; (i=>{ let n=mapsArray.length; btns.forEach((b,k)=>b.onclick=()=>{ trgt.src=mapsArray[i=i+2*k-1]; btns[0].disabled=!i; btns[1].disabled=(i+1===n); }) })(1); btns[0].click();
 .maps { display: flex; justify-content: center; align-items: center; } #myIframe { width: 600px; height: 600px; }
 <iframe id='myIframe' class="maps-gallery active"></iframe><br> <button>Prev</button> <button>Next</button>

The main function is embedded in an IIFE that encapsulates the current position index i and sets it to 0 as the starting value.主要的 function 嵌入在一个 IIFE 中,该 IIFE 封装了当前的 position 索引i并将其设置为0作为起始值。 The only "visible" global elements are the function shuffle() and the array mapsArray itself.唯一“可见”的全局元素是 function shuffle()和数组mapsArray本身。 For the actual stepping I apply a little trick: while in the .forEach() loop I use the index k to determine whether next (=1) or previous (=0) was clicked and then I calculate the increment accordingly.对于实际的步进,我应用了一个小技巧:在.forEach()循环中,我使用索引k来确定是next (=1) 还是previous (=0),然后我相应地计算增量。

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

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