【问题标题】:How to method to change HTML background according to API result value?如何根据 API 结果值更改 HTML 背景?
【发布时间】:2020-09-26 02:42:07
【问题描述】:

我使用带有 openweathermap API 的 HTML、CSS、JS(不是 jQuery)创建 Web 应用程序。 我想根据天气改变背景。

但我已经尝试了很多,但没有成功。 js文件是一个main.js文件。

这是JS代码。

function displayResults(weather) {
  var city = document.querySelector(".city");
  city.innerHTML = weather.name + ", " + weather.sys.country;

  let now = new Date();
  let date = document.querySelector(".location .date");
  date.innerHTML = dateBuilder(now);

  let temp = document.querySelector(".current .temp");
  temp.innerHTML = Math.round(weather.main.temp - ChangeTemp).toFixed(0) + "°C";

  var weather_el = document.querySelector(".current .weather");
  weather_el.innerHTML = weather.weather[0].main;
  var weatherinfo = weather.weather[0].main;

  let hilow = document.querySelector(".hi-low");
  hilow.innerText =
    "Low and High Temp: " +
    Math.round(weather.main.temp_min - ChangeTemp) +
    " °C / " +
    Math.round(weather.main.temp_max - ChangeTemp) +
    " °C";
}

function backgroundChange(weather) {
  var imgs = document.getElementById("allbody");

  imgs.style.backgroundImage = "url(background.gif)";

  if (weatherinfo == Rain) {
    imgs.style.backgroundImage = "url(rain3.gif)";
  } else if (weatherinfo == Clouds) {
    imgs.style.backgroundImage = "url(cloud.gif)";
  } else if (weatherinfo == Clear) {
    imgs.style.backgroundImage = "url(sky3.gif)";
  } else {
    imgs.style.backgroundImage = "url(background.gif)";
  }
}

不工作功能背景更改。

这是 HTML 代码。

<div id='allbody' class="app" style="background-image: url(background.gif);">

<div>
    <header>
        <input type="text" autocomplete="off" class="search-box" placeholder="Please enter the location."/>

    </header>

    <main>
        <section class="location">
            <div class="city">Location, Country</div>
            <div class="date">Today date info</div>
        </section>
        <div class="current">
            <div class="temp"><span>Temp (°C)</span></div>
            <div id="wdata" class="weather">Weather</div>
            <div class="hi-low">Low and High Temp</div>
        </div>
...

如何根据天气改变背景图片?

谢谢

【问题讨论】:

  • RainClouds 等...是对您的代码中未包含的变量的引用吗?也没有调用backgroundChange,所以我不确定你缺少什么代码。
  • @Chase 如何重新评论?抱歉,我对此一无所知。 Rain can Clouds from html 结果。所以它不是可变的。那我该怎么办?
  • 不完整,只是检查一下。如果完成,我会在这里添加。

标签: javascript html background-image weather openweathermap


【解决方案1】:

不要使用imgs.style.backgroundImage = 'url(rain3.gif)';,也可以尝试使用imgs.style.backgroundImage = 'url('rain3.gif')'; 在图像路径周围加上引号。

【讨论】:

  • 我用双引号和带引号都试过了,还是不行。
【解决方案2】:

而不是创建'imgs'变量,更好地使用; document.body.style.backgroundImage = "url(background.gif)";

function backgroundChange(weather) {
  if (weatherinfo == Rain) {
  document.body.style.backgroundImage = "url(rain3.gif)";
  } else if (weatherinfo == Clouds) {
  document.body.style.backgroundImage = "url(cloud.gif)";
  } else if (weatherinfo == Clear) {
  document.body.style.backgroundImage = "url(sky3.gif)";
  } else {
  document.body.style.backgroundImage= "url(background.gif)";
  }
}

【讨论】:

  • 感谢您的回答。但它不起作用。背景保持不变,搜索雨区时,背景不变。
  • weatherinfo 未在 backgroundChange 函数中定义。函数 backgroundChange(weather) { var weatherinfo = weather.weather[0].main;
  • 我添加了代码,但不起作用... T.T 抱歉让您感到困惑。功能 backgroundChange (天气) { var weatherinfo = weather.weather [0] .main; if (weatherinfo == "Rain") { document.body.style.backgroundImage = "url('rain3.gif')"; } else if (weatherinfo == "Clouds") { document.body.style.backgroundImage = "url('cloud.gif')"; } else if (weatherinfo == "Clear") { document.body.style.backgroundImage = "url('sky3.gif')"; } else { document.body.style.backgroundImage="url('background.gif')"; } } ofc 我试过也删除了“”。
  • 先生,如果我使用 PHP(不是 HTML)有什么不同?
  • 如果我是你,我会更喜欢一些 CSS。您可以查看此链接; thenewcode.com/299/Dynamically-Resized-Backgrounds-With-CSS
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多