【问题标题】:How to change the background to match the user's weather conditions and time of day?如何更改背景以匹配用户的天气条件和一天中的时间?
【发布时间】:2017-08-14 08:56:09
【问题描述】:

到目前为止,我的代码正在运行,但仅适用于白天的照片。我忘了为用户的位置放入夜间照片。因此,当它说晚安并且天气状况说晴朗的天空时,背景应该变成晴朗的天空和夜间照片,而不是像我已经拍过的晴天照片。如何按位置和一天中的时间将背景更改为用户的天气?这是我的 codepen 的链接,以防您希望看到它的实际效果。 https://codepen.io/Brown95/pen/MvvBLK

var weatherImage = "";

  var imagePhoto = {

    thunder: "https://s-media-cache-ak0.pinimg.com/originals/2e/43/73/2e4373184057ab029b5ca15aca484b09.jpg",
    rainy: "https://i.ytimg.com/vi/q76bMs-NwRk/maxresdefault.jpg",
    cloudy: "https://static1.squarespace.com/static/57523357c2ea515ccf6c1adb/58dcea75bebafb06e997da9c/58dcece61e5b6cf38585d46b/1490873606398/mostly+cloudy.jpg",
    snow: "https://static.bhphotovideo.com/explora/sites/default/files/Correct.jpg",
    clear: "https://mota.ru/upload/wallpapers/2010/05/14/08/01/22099/mota_ru_0051405-2560x1600.jpg",
    drizzle: "https://s-media-cache-ak0.pinimg.com/736x/e4/43/f5/e443f59b4f03dd487d090a279c2f08ab--rain-drops-water-drops.jpg",
    haze: "https://ak3.picdn.net/shutterstock/videos/3578564/thumb/1.jpg",
    tornado: "https://i.ytimg.com/vi/5QnsRXUbsK4/maxresdefault.jpg",
    cold: "https://static.pexels.com/photos/374/snow-dawn-sunset-winter.jpg",
    windy: "https://ak9.picdn.net/shutterstock/videos/4337360/thumb/1.jpg",
    hail: "https://s-media-cache-ak0.pinimg.com/236x/7c/60/3d/7c603d9183ff32c92330780a2ebdcfca--rainy-weather-rainy-days.jpg",
    sunny: "https://i.ytimg.com/vi/rG9fev-m0ag/maxresdefault.jpg"
};


function selectImage (id){

    if (id >= 200 && id <= 232){
        weatherImage = imagePhoto.thunder;
    }else if(id >= 300 && id <= 321){
        weatherImage = imagePhoto.drizzle;
    }else if (id >= 500 && id <= 531){
        weatherImage = imagePhoto.rainy;
    }else if (id >= 600 && id <= 622){
        weatherImage = imagePhoto.snow;
    }else if (id >= 701 && id <= 721 ){
        weatherImage = imagePhoto.haze;
    }else if (id === 800){
        weatherImage = imagePhoto.clear;
    }else if (id >= 801 && id <= 804){
        weatherImage = imagePhoto.cloudy;
    }else if (id >= 900 && id <= 902){
        weatherImage = imagePhoto.tornado;
    }else if (id === 903){
        weatherImage = imagePhoto.cold;
    }else if (id === 904){
        weatherImage = imagePhoto.sunny;
    }else if (id === 905){
        weatherImage = imagePhoto.windy;
    }else if (id === 906){
        weatherImage = imagePhoto.hail;
    }else{
        weatherImage = imagePhoto.windy;
    }

    $('body').css('background-image', 'url(' + weatherImage + ')');


}

【问题讨论】:

  • 问题是,你能知道他们所在位置的天气吗?
  • 不,我已经知道如何获取天气并更改背景以匹配他们的天气。我只是问是否是夜间并且是晴朗的天空,那么月亮的图片应该显示晴朗的天空,而不是太阳的图片晴朗的天空。你知道吗?
  • 回答了让你得到当前日期时间的功能

标签: javascript


【解决方案1】:

这是获取当前日期时间的方法:

console.log(new Date());

在 w3c 上,您会发现许多函数可以从日期中检索特定数据:

https://www.w3schools.com/jsref/jsref_obj_date.asp

【讨论】:

  • 我已经在我的代码中使用了获取日期。我会检查的,等等。我一直在使用 w3schools,顺便说一句,它很有帮助。
  • 此命令检索本地信息,因此每个用户都会向您发送他自己的日期时间,您将能够设置正确的图像
  • 好的,非常感谢。感谢您的帮助。
  • 如果对您有帮助,请标记答案^^
  • 我是否将背景放在时间的 if/else 语句中?
猜你喜欢
  • 1970-01-01
  • 2018-10-16
  • 2011-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-12
  • 2020-05-30
  • 1970-01-01
相关资源
最近更新 更多