首页 > 解决方案 > Javascript 从 unsplash api 更新背景图像的时间太长

问题描述

在 background() 函数中查看 js。从字面上看,更新背景图像需要花费 2 秒的时间,这看起来不太好。请帮助我如何改进它。

rffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffrffffffffffff

html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="style.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css">
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
    integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
    crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
    integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
    crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
    integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
    crossorigin="anonymous"></script>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootswatch@4.5.2/dist/litera/bootstrap.min.css"
    integrity="sha384-enpDwFISL6M3ZGZ50Tjo8m65q06uLVnyvkFO3rsoW0UC15ATBFz3QEhr3hmxpYsn" crossorigin="anonymous">
</head>

<body>

  <h1 class="text-center mt-3">My WEATHER APP</h1>

  <div class="inputcity">

    <input type="text" id="cityname" name="cityname" placeholder="Enter City Name">
    <button type="submit" class="button" onclick="city()">Submit</button>
  </div>


  <div class="center">


    <div class="card ">
      <div class="top">
        <a href="#" class="source">View Page Source</a>


        <div class="descr">

          <div class="weather-icon"><img src="icons/unknown.png" /></div>
          <h3 class="weath-descr font-weight-bold">Clear Skies</h3>
        </div>
      </div>

      <div class="bottom">
        <h1 class="city">
          <div class="loc">Mansa, Punjab, IN</div>
        </h1>
        <p class="date">11.21 AM. 02-02-2021</p>
        <h1 class="temp">32.00 C</h1>
        <p class="minmax"><span class="min"> Min 5C</span> || <span class="max"> Max 35C</span></p>
      </div>

      <footer>Created By :Khagesh Bansal</footer>


    </div>


  </div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="script.js"></script>

</html>

css

 *{
    padding: 0;

    box-sizing: border-box;
}

body{
  
  
  font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  
}


.center{
 
  display:flex;
  justify-content: center;
  align-items: center;
  display: none;

}



.inputcity{
  display:flex;
  margin: 5px 70px 30px 70px;
}

input[type=text] {
  width: 100%;
  padding: 12px 20px;
  margin: 8px 0;
  box-sizing: border-box;
}

.button {
  background-color: #4CAF50; /* Green */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}

.card{
    width: 37vw;
    background: white;
    box-shadow: 5px 5px 15px black;
    border-radius: 10px;
}
.source{
    position: absolute;
    colon:white;
    top: 0;
    right: 0;
    margin: 5px;
}

.top{
    position: relative;
    background-color: skyblue;
    height: 30vh;
    display:flex;
justify-content: center;
align-items: center;
border-radius: 10px 10px 0 0;
}
.fa-sun{
    color: rgb(214, 181, 108);
    font-size: 600%;
    animation: mymove 6s  linear infinite;
}
.descr{
  display: flex;
  justify-content: center;
  align-items: center;
}
@keyframes mymove {
    0% {font-size: 650%;}
    50%{font-size: 450%;}
    100%{font-size: 650%;}
  }

.bottom{
    display:flex;
justify-content: center;
align-items: center;
flex-direction: column;
background:rgb(179, 197, 231);
position: relative;
}

footer{
        background: black;
    color: grey;
    padding: 3px;
    font-size: medium;
    border-radius: 0 0 10px 10px;
    display:flex;
justify-content: center;
align-items: center;
font-size: medium;
    
}
.temp{  animation: heartbeat 3s ease-in-out infinite both;
    font-size: xxx-large;
  
}
@keyframes temp {
    0% {margin-left: -50px;}
    50%{margin-left: 50px;}
    100%{margin-left: -50px;}
  }




@media only screen and (max-width: 768px) {
    /* For mobile phones: */
    .card{
        width: 95%;
    }
  }


  @keyframes heartbeat {
    from {
      transform: scale(1);
      transform-origin: center center;
      animation-timing-function: ease-out;
    }
    10% {
      transform: scale(0.91);
      animation-timing-function: ease-in;
    }
    17% {
      transform: scale(0.98);
      animation-timing-function: ease-out;
    }
    33% {
      transform: scale(0.87);
      animation-timing-function: ease-in;
    }
    45% {
      transform: scale(1);
      animation-timing-function: ease-out;
    }
  }

javascript

date = document.querySelector(".date");
temp = document.querySelector(".temp");
loc = document.querySelector(".loc");
min = document.querySelector(".min");
max = document.querySelector(".max");
locationIcon = document.querySelector('.weather-icon');
cityname = document.getElementById("cityname");
weathdescr=document.querySelector(".weath-descr");

//moment js for time function
function time() {
    var d = moment().format('Do-MMM-YY, h:mm:ss a  dddd');
    date.innerHTML = d;
}
time();
setInterval(time, 1000);




//1st city() is run when submit is cricked
function city() {
    getText(`http://api.openweathermap.org/data/2.5/weather?q=${cityname.value}&units=metric&appid=dd2148c708035bd9df9580495995612a`);
    cityname.value = ""

    document.querySelector(".center").style.display = "flex";
}

//2nd getText() is run to fetch openweather api
async function getText(file) {
    let myObject = await fetch(file);
    myText = await myObject.json();
    populate(myText);
}

//3rd populate()- to display results
function populate(data) {

    temp.innerHTML = `${myText.main.temp} C`; 
    loc.innerHTML = ` ${myText.name}, ${myText.sys.country}`;
    min.innerHTML = `Min ${myText.main.temp_min} C`;
    max.innerHTML = `Max ${myText.main.temp_max} C`;
    weathdescr.innerHTML = myText.weather[0].description;
    const icon = myText.weather[0].icon;
    locationIcon.innerHTML = "<img src='http://openweathermap.org/img/wn/" + icon + "@4x.png'>"

    background(myText.weather[0].description);

}

//4th background()- to add new background from unsplash api
function background(data) {
    document.body.style.background =
        "linear-gradient(rgba(255,255,255,.3), rgba(255,255,255,.3)),url("+`"https://source.unsplash.com/600x300/?${data}"`+")";
        document.body.style.backgroundRepeat = "no-repeat";
        document.body.style.backgroundSize = "100vw 100vh";
}

标签: javascriptapifetch

解决方案


推荐阅读