Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

266
Views
¿Cómo cambiar la imagen de fondo según el resultado de una API?

función para cambiar la imagen de fondo:

 function backgroundChange(checkDay) { if (checkDay === 0) { document.body.style.backgroundImage = "url('night.jpg')"; } else if (checkDay === 1) { document.body.style.backgroundImage = "url('day.jpg')"; } }

función para obtener resultados de api
api utilizada es: https://rapidapi.com/weatherapi/api/weatherapi-com/ He utilizado la función api meteorológica en tiempo real para axios (js) directamente desde el enlace anterior

 const options = { method: "GET", url: "https://weatherapi-com.p.rapidapi.com/current.json", params: { q: "Los angeles" }, headers: { "X-RapidAPI-Key": "API_KEY", "X-RapidAPI-Host": "weatherapi-com.p.rapidapi.com" } }; var checkDay; axios .request(options) .then(function (response) { console.log("-->", response.data.current.is_day); checkDay = response.data.current.is_day; console.log(checkDay); }) .catch(function (error) { console.error(error); });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El mismo código que proporcionaste está funcionando para mí. Verifique que tenga algún componente (y estilo adjunto) configurado que superponga el cuerpo por completo.

Esto es lo que probé, solo cambie API_key y funcionará:

 function getWeatherInfo() { var location = document.getElementById("location").value; var options = { method: "GET", url: "https://weatherapi-com.p.rapidapi.com/current.json", params: { q: location }, headers: { "X-RapidAPI-Key": "API_Key", "X-RapidAPI-Host": "weatherapi-com.p.rapidapi.com", }, }; console.log(options); var checkDay; axios .request(options) .then(function (response) { console.log("-->", response.data.current.is_day); checkDay = response.data.current.is_day; console.log(checkDay); document.getElementById("results").innerHTML = JSON.stringify(response); if (checkDay === 0) { document.body.style.backgroundImage = "url('night.jpg')"; } else if (checkDay === 1) { document.body.style.backgroundImage = "url('day.jpg')"; } }) .catch(function (error) { console.error(error); }); }
 body{ background-color: lightseagreen; } #results{ height:600px; width:100%; background-color: white; }
 <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="style.css"> <script src="getWeather.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> <h1>Weather</h1> <input id="location" type="text" value="Los Angeles" placeholder="enter location here"> <button onclick="getWeatherInfo()">Get Weather Info</button> <main> <code id="results"></code> </main> </body> </html>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!