简体   繁体   English

如何在两个图标之间交换?

[英]how can i swap between two icons?

I'm new to JavaScript and i'm making a weather app it works fine but the issue is in the icons when i type another country to see the weather, the new country icon goes on top of the previous country icon and i want to hide the previous icon and show the new one i tried the if statement and switch case method but it didn't work我是 JavaScript 的新手,我正在制作一个天气应用程序,它运行良好,但问题出在图标上,当我输入另一个国家/地区查看天气时,新的国家/地区图标位于前一个国家/地区图标之上,我想隐藏上一个图标并显示新图标 我尝试了 if 语句和 switch case 方法,但它不起作用

here is my html code:这是我的 html 代码:

<form>
  <input class="input" type="text" placeholder="type your country">
</form>

<button class="button">SUBMIT</button>

<div class="location">
  <h1 class="timezone">TIMEZONE</h1>
  <div id="icons" class="">
    <img id="cloud" class="hide" src="./gifs/cloud.png">
    <img id="clouds" class="hide" src="./gifs/clouds.png">
    <img id="cloudy" class="hide" src="./gifs/cloudy.png">
    <img id="rain" class="hide" src="./gifs/rain.png">
    <img id="snowflake" class="hide" src="./gifs/snowflake.png">
    <img id="storm" class="hide" src="./gifs/storm.png">
    <img id="sun" class="hide" src="./gifs/sun.png">
    <img id="wind" class="hide" src="./gifs/wind.png">
  </div>
</div>

and here is my javascript code:这是我的 javascript 代码:

const temp = data.main.temp - 273.15 ;
const celsius = temp.toPrecision(3);
const descripiton = data.weather[0].description;
const name = data.name;
tempDeg.textContent = celsius;
tempDes.textContent = descripiton;
tmZn.textContent = name;
switch(descripiton){
  case "mist" :
    document.getElementById("wind").classList.remove("hide");
    break;
  case "clear sky" : 
    document.getElementById("sun").classList.remove("hide");
    break;
  case "broken clouds" : 
    document.getElementById("clouds").classList.remove("hide");
    break;
  case "shower rain" : 
    document.getElementById("rain").classList.remove("hide");
    break;
  case "thunderstorm" : 
    document.getElementById("storm").classList.remove("hide");
    break;
  case "snow" :
    document.getElementById("snow").classList.remove("hide");
    break;
  case "few clouds" : 
    document.getElementById("cloudy").classList.remove("hide");
    break;
  case "scattered clouds" : 
    document.getElementById("cloud").classList.remove("hide");
    break;
}

You need to hide everything using something like a for loop.您需要使用for循环之类的东西隐藏所有内容。

var icons = document.querySelector("#icons").children;
for (var i=0; i<icons.length; i++) {
  icons[i].classList.add("hide");
}

Then remove the hide class from the one you wish to show as you have done in your code.然后从您希望显示的代码中删除隐藏 class,就像您在代码中所做的那样。

If you are showing only a single icon there is another approach than hiding all icons如果您只显示一个图标,还有另一种方法,而不是隐藏所有图标

 var iconName = 'default'
 switch(descripiton) {
                    case "mist" :
                    iconName = 'wind';
                    break;
                    case "clear sky" : 
                    iconName = 'sun';
                    break;
                    case "broken clouds" : 
                    iconName =  'clouds'
                    break;
                    // add rest of items here
                }

document.getElementById("icons").innerHTML = '<img src="./gifs/' + iconName + '.png">';

If you want to keep your current approach first hide all icons and then make only the required icon visible如果您想保留当前方法,请先隐藏所有图标,然后仅显示所需的图标

var images = document.getElementById('icons').querySelectorAll('img');
    for (i = 0; i < images.length; i++){
        images[i].classList.add('hide');      
    }

In your switch make the relevant item visible like this在您的开关中,使相关项目像这样可见

document.getElementById("wind").classList.remove("hide");

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

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