简体   繁体   中英

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

here is my html code:

<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:

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.

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.

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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