原生JS+JSON 天气预报小项目 (含源码)

灰太狼 2023-02-15 15:59 54阅读 0赞

源码

源码链接


index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>天气预报</title>
  7. <link rel="stylesheet" href="css/index.css">
  8. <script src="js/index.js"></script>
  9. </head>
  10. <body>
  11. <div class="container">
  12. <!-- 左边 -->
  13. <div class="left-side">
  14. <!-- 渐变 -->
  15. <div class="gradient"></div>
  16. <div class="date-container">
  17. <h2 class="date-dayname" id="date-dayname">周六</h2>
  18. <span class="date-day" id="date-day">2020-06-06</span>
  19. <!-- location 图标 -->
  20. <span class="iconfont" id="location">开封</span>
  21. </div>
  22. <div class="weather-conteiner">
  23. <!-- 天气图标 -->
  24. <span class="iconfont" id="weather-l"></span>
  25. <div class="weather-temp" id="weather-temp">30℃</div>
  26. <div class="weather-desc" id="weather-desc">晴天</div>
  27. </div>
  28. </div>
  29. <!-- 右边部分 -->
  30. <div class="right-side">
  31. <div class="today-info-container">
  32. <div class="today-info">
  33. <!-- PM2.5 -->
  34. <div class="pm clearfix ">
  35. <span class="title">PM2.5</span>
  36. <span class="value" id="pm">00</span>
  37. </div>
  38. <!-- 湿度 -->
  39. <div class="humidity clearfix a">
  40. <span class="title">湿度</span>
  41. <span class="value" id="humidity">00</span>
  42. </div>
  43. <!-- 风速 -->
  44. <div class="wind clearfix ">
  45. <span class="title">风速</span>
  46. <span class="value" id="wind">00</span>
  47. </div>
  48. </div>
  49. </div>
  50. <div class="week-container">
  51. <ul class="week-list">
  52. <li class="active" id="day1">
  53. <!-- 天气图标 -->
  54. <span class="iconfont"></span>
  55. <span class="day-name">周六</span>
  56. <span class="day-temp">30℃</span>
  57. </li>
  58. <li id="day2">
  59. <!-- 天气图标 -->
  60. <span class="iconfont"></span>
  61. <span class="day-name">周日</span>
  62. <span class="day-temp">30℃</span>
  63. </li>
  64. <li id="day3">
  65. <!-- 天气图标 -->
  66. <span class="iconfont"></span>
  67. <span class="day-name">周一</span>
  68. <span class="day-temp">30℃</span>
  69. </li>
  70. <li id="day4">
  71. <!-- 天气图标 -->
  72. <span class="iconfont"></span>
  73. <span class="day-name">周二</span>
  74. <span class="day-temp">30℃</span>
  75. </li>
  76. </ul>
  77. </div>
  78. <div class="location-container">
  79. <input type="text" id="city" placeholder="位置">
  80. <button class="location-button" id="location-button">
  81. <!-- location图标 -->
  82. <span class="iconfont" >查询</span>
  83. </button>
  84. </div>
  85. </div>
  86. </div>
  87. </body>
  88. </html>

inded.css

  1. @font-face {
  2. font-family: 'iconfont';
  3. /* project id 1866935 */
  4. src: url('//at.alicdn.com/t/font_1866935_jaopflkhzf.eot');
  5. src: url('//at.alicdn.com/t/font_1866935_jaopflkhzf.eot?#iefix') format('embedded-opentype'), url('//at.alicdn.com/t/font_1866935_jaopflkhzf.woff2') format('woff2'), url('//at.alicdn.com/t/font_1866935_jaopflkhzf.woff') format('woff'), url('//at.alicdn.com/t/font_1866935_jaopflkhzf.ttf') format('truetype'), url('//at.alicdn.com/t/font_1866935_jaopflkhzf.svg#iconfont') format('svg');
  6. }
  7. .iconfont {
  8. font-family: "iconfont"!important;
  9. font-size: 16px;
  10. font-style: normal;
  11. }
  12. /* 清除默认样式 */
  13. * {
  14. margin: 0px;
  15. padding: 0px;
  16. }
  17. ul {
  18. list-style: none;
  19. }
  20. /* 高度塌陷问题 */
  21. .clearfix::after, .clearfix::before {
  22. content: "";
  23. display: table;
  24. clear: both;
  25. }
  26. body {
  27. width: 100%;
  28. height: 100vh;
  29. background-color: #424242;
  30. display: flex;
  31. align-items: center;
  32. justify-content: center;
  33. }
  34. .container {
  35. height: 435px;
  36. width: 740px;
  37. background-color: #1F252D;
  38. color: #ffffff;
  39. border-radius: 25px;
  40. box-shadow: 0 70px -10px rgba(0, 0, 0, 0.2);
  41. }
  42. .left-side {
  43. position: relative;
  44. width: 300px;
  45. height: 100%;
  46. /* background-color: red; */
  47. border-radius: 25px 0 0 25px;
  48. box-shadow: 0 35px -10px rgba(0, 0, 0, 0.2);
  49. float: left
  50. }
  51. .gradient {
  52. width: 100%;
  53. height: 100%;
  54. /* background-color: red; */
  55. position: absolute;
  56. background-image: linear-gradient(to right, #0acffe 0%, #495aff 100%);
  57. border-radius: 25px 0 0 25px;
  58. opacity: 0.8;
  59. }
  60. .date-container {
  61. position: absolute;
  62. top: 25px;
  63. left: 25px;
  64. }
  65. .date-container .date-dayname {
  66. display: block;
  67. margin-top: 4px;
  68. }
  69. #location {
  70. display: block;
  71. margin-top: 4px;
  72. }
  73. .weather-conteiner {
  74. position: absolute;
  75. bottom: 25px;
  76. left: 25px;
  77. }
  78. #weather-l {
  79. font-size: 70px;
  80. }
  81. .weather-temp {
  82. font-size: 4em;
  83. font-weight: bold;
  84. }
  85. .weather-desc {
  86. font-size: 20px;
  87. }
  88. /* 右边部分 */
  89. .right-side {
  90. float: right;
  91. /* background-color: red; */
  92. padding-top: 25px;
  93. margin-right: 10px;
  94. height: 100%;
  95. box-sizing: border-box;
  96. position: relative;
  97. }
  98. .today-info {
  99. /* background-color: royalblue; */
  100. padding: 15px 15px 0 15px;
  101. margin: 0 25px 25px 25px;
  102. box-shadow: 0px 0px 50px rgba(0, 0, 0, 0.3);
  103. border-radius: 10px;
  104. }
  105. .today-info div {
  106. border-bottom: 10px;
  107. }
  108. .today-info .a {
  109. margin: 6px 0px;
  110. }
  111. .today-info .title {
  112. float: left;
  113. }
  114. .today-info .value {
  115. float: right;
  116. }
  117. .week-list {
  118. margin: 10px 35px;
  119. border-radius: 10px;
  120. box-shadow: 0px 0px 50px rgba(0, 0, 0, 0.3);
  121. }
  122. .week-list li {
  123. float: left;
  124. /* background-color: red; */
  125. padding: 15px;
  126. border-radius: 10px;
  127. text-align: center;
  128. cursor: pointer;
  129. transition: 200ms;
  130. }
  131. .week-list li span {
  132. display: block;
  133. }
  134. .week-list {
  135. position: relative;
  136. z-index: 99999;
  137. }
  138. .week-list li:hover {
  139. transform: scale(1.1);
  140. background-color: #ffffff;
  141. color: #222831;
  142. }
  143. .week-list .iconfont {
  144. font-size: 37px;
  145. }
  146. .active {
  147. transform: scale(1.1);
  148. background-color: #ffffff;
  149. color: #222831;
  150. }
  151. .week-list .day-name {
  152. margin-top: 10px;
  153. text-align: center;
  154. }
  155. .week-list .day-temp {
  156. margin-top: 10px;
  157. text-align: center;
  158. font-size: 12px;
  159. font-weight: bold;
  160. }
  161. .location-container {
  162. padding: 25px 35px;
  163. position: relative;
  164. /* background-color: red; */
  165. width: 330px;
  166. height: 126px;
  167. }
  168. .location-container input {
  169. outline: none;
  170. width: 300px;
  171. height: 28px;
  172. padding: 6px;
  173. display: block;
  174. box-sizing: border-box;
  175. /* font-weight: bold; */
  176. position: absolute;
  177. bottom: -15px;
  178. left: calc(50% - 150px);
  179. border: none;
  180. border-radius: 5px;
  181. color: #222831;
  182. }
  183. .location-container button {
  184. outline: none;
  185. width: 320px;
  186. /* height: 28px; */
  187. padding: 8px;
  188. box-sizing: border-box;
  189. border-radius: 25px;
  190. border: none;
  191. color: #FFFFFF;
  192. /* font-size: 18px; */
  193. font-weight: bold;
  194. cursor: pointer;
  195. box-shadow: 0px 50px -5px rgba(0, 0, 0, 0.3);
  196. background-image: linear-gradient(to right, #0acffe 0%, #495aff 100%);
  197. transition: 200ms;
  198. position: absolute;
  199. bottom: -80px;
  200. left: calc(50% - 160px);
  201. }
  202. .location-container button:hover {
  203. transform: scale(1.05);
  204. }

index.js

  1. // 接口返回的数据就是一个函数的调用
  2. function weather(data) {
  3. var dateDayname = document.getElementById("date-dayname");
  4. var dateDay = document.getElementById("date-day");
  5. var location = document.getElementById("location");
  6. var weatherL = document.getElementById("weather-l")
  7. var weatherTemp = document.getElementById("weather-temp")
  8. var weatherDesc = document.getElementById("weather-desc")
  9. var pm = document.getElementById("pm")
  10. var humidity = document.getElementById("humidity")
  11. var wind = document.getElementById("wind")
  12. var day1 = document.getElementById("day1");
  13. var span1 = day1.getElementsByTagName("span");
  14. var day2 = document.getElementById("day2");
  15. var span2 = day2.getElementsByTagName("span");
  16. var day3 = document.getElementById("day3");
  17. var span3 = day3.getElementsByTagName("span");
  18. var day4 = document.getElementById("day4");
  19. var span4 = day4.getElementsByTagName("span");
  20. dateDayname.innerHTML = data.weather[0].date.slice(0, 3);
  21. dateDay.innerHTML = data.date;
  22. location.innerHTML = data.city;
  23. weatherL.innerHTML = weatherIcon(1);
  24. weatherTemp.innerHTML = data.weather[0].temp.slice(0, 3) + "℃";
  25. weatherDesc.innerHTML = data.weather[0].weather;
  26. //判断是否有pm25
  27. if (data.pm25) {
  28. pm.innerHTML = data.pm25;
  29. } else {
  30. pm.innerHTML = "暂无";
  31. }
  32. humidity.innerHTML = "暂无";
  33. wind.innerHTML = data.weather[0].wind;
  34. // 封装判断天气的一个函数
  35. // t 第几天
  36. function weatherIcon(t) {
  37. if (data.weather[t - 1].icon1.search("duoyun") != -1) {
  38. return "";
  39. }
  40. if (data.weather[t - 1].icon1.search("yin") != -1) {
  41. return "";
  42. }
  43. if (data.weather[t - 1].icon1.search("qing") != -1) {
  44. return "";
  45. }
  46. if (data.weather[t - 1].icon1.search("xiaoyu") != -1) {
  47. return "";
  48. }
  49. if (data.weather[t - 1].icon1.search("zhongyu") != -1) {
  50. return "";
  51. }
  52. if (data.weather[t - 1].icon1.search("dayu") != -1 || data.weather[0].icon1.search("baoyu") != -1) {
  53. return "";
  54. }
  55. if (data.weather[t - 1].icon1.search("leizhenyu") != -1) {
  56. return "";
  57. }
  58. if (data.weather[t - 1].icon1.search("zhenyu") != -1) {
  59. return "";
  60. }
  61. if (data.weather[t - 1].icon1.search("zhongxue") != -1) {
  62. return "";
  63. }
  64. if (data.weather[t - 1].icon1.search("zhongxuezhuandaxue") != -1) {
  65. return "";
  66. }
  67. if (data.weather[t - 1].icon1.search("zhenxue") != -1) {
  68. return "";
  69. }
  70. if (data.weather[t - 1].icon1.search("yangsha") != -1) {
  71. return "";
  72. }
  73. if (data.weather[t - 1].icon1.search("xiaoxue") != -1) {
  74. return "";
  75. }
  76. if (data.weather[t - 1].icon1.search("xiaoxuezhuanzhongxue") != -1) {
  77. return "";
  78. }
  79. if (data.weather[t - 1].icon1.search("mai") != -1) {
  80. return "";
  81. }
  82. if (data.weather[t - 1].icon1.search("shachenbao") != -1) {
  83. return "";
  84. }
  85. if (data.weather[t - 1].icon1.search("fuchen") != -1) {
  86. return "";
  87. }
  88. if (data.weather[t - 1].icon1.search("daxue") != -1) {
  89. return "";
  90. }
  91. if (data.weather[t - 1].icon1.search("daxuezhuanbaoxue") != -1) {
  92. return "";
  93. }
  94. if (data.weather[t - 1].icon1.search("baoxue") != -1) {
  95. return "";
  96. }
  97. }
  98. span1[0].innerHTML = weatherIcon(1);
  99. span1[1].innerHTML = data.weather[0].date.slice(0, 3);
  100. span1[2].innerHTML = data.weather[0].temp;
  101. span2[0].innerHTML = weatherIcon(2);
  102. span2[1].innerHTML = data.weather[1].date.slice(0, 3);
  103. span2[2].innerHTML = data.weather[1].temp;
  104. span3[0].innerHTML = weatherIcon(3);
  105. span3[1].innerHTML = data.weather[2].date.slice(0, 3);
  106. span3[2].innerHTML = data.weather[2].temp;
  107. span4[0].innerHTML = weatherIcon(4);
  108. span4[1].innerHTML = data.weather[3].date.slice(0, 3);
  109. span4[2].innerHTML = data.weather[3].temp;
  110. }
  111. window.onload = function() {
  112. var btn = document.getElementById("location-button");
  113. var city = document.getElementById("city");
  114. var oldScript = document.createElement("script");
  115. oldScript.src = `https://api.asilu.com/weather/?city=${ "开封"}&callback=weather`;
  116. //插入到页面中去
  117. document.body.appendChild(oldScript);
  118. btn.onclick = function() {
  119. // 加个简单判断,输入内容是否为空
  120. if (city.value) {
  121. var newScript = document.createElement("script");
  122. newScript.src = `https://api.asilu.com/weather/?city=${ city.value}&callback=weather`;
  123. //插入到页面中去
  124. document.body.replaceChild(newScript, oldScript);
  125. oldScript = newScript;
  126. } else {
  127. alert("请输入城市名称!");
  128. }
  129. }
  130. }

没事可以来看看,万一有收获呢。

个人博客地址https://aqingya.cn

愿你的坚持终有回报。

发表评论

表情:
评论列表 (有 0 条评论,54人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android 天气预报

    从开始到完成基本功能差不多一个星期 因为不知道怎么入手和很多导包的问题 在这个项目里我第一次用了线程 网络请求 json解析 所以觉得还是挺有意义的 我在写的时候没