index.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>天气查询</title>
  7. <style>
  8. /* 全局样式 - 从风格参考继承 */
  9. * {
  10. color: var(--secondary-font-color);
  11. font-family: "Courier New", Courier, monospace;
  12. box-sizing: border-box;
  13. transition: all 0.2s ease;
  14. }
  15. ::selection {
  16. background: var(--accent-bg-color);
  17. color: var(--accent-font-color);
  18. }
  19. :root,
  20. body {
  21. height: 100%;
  22. margin: 0;
  23. background: var(--content-bg-color);
  24. padding: var(--margin-padding);
  25. --accent-color: #64ffda; /* 强调色:边框 */
  26. --secondary-color: #00695c; /* 次要色:未激活边框 */
  27. --accent-bg-color: #009688;
  28. --secondary-bg-color: #616161;
  29. --content-bg-color: #424242;
  30. --accent-font-color: #f5f5f5;
  31. --secondary-font-color: #eeeeee;
  32. --margin-padding: 0.5rem;
  33. --border-thickness: 0.2rem;
  34. --border-radius: 0.5rem;
  35. --shadow: 0.6rem 0.6rem 0.1rem 0.1rem rgba(0, 0, 0, 0.2);
  36. }
  37. body {
  38. max-width: 800px;
  39. margin: 0 auto;
  40. padding: 20px;
  41. }
  42. .container {
  43. display: flex;
  44. flex-direction: column;
  45. gap: 20px;
  46. }
  47. .search-box {
  48. display: flex;
  49. gap: 10px;
  50. }
  51. input {
  52. padding: 8px;
  53. flex-grow: 1;
  54. background: var(--secondary-bg-color);
  55. border: var(--border-thickness) solid var(--secondary-color);
  56. border-radius: var(--border-radius);
  57. color: var(--secondary-font-color);
  58. font-size: 1rem;
  59. outline: none;
  60. }
  61. input:focus {
  62. border-color: var(--accent-color);
  63. }
  64. button {
  65. padding: 8px 16px;
  66. background: var(--accent-bg-color);
  67. color: var(--accent-font-color);
  68. border: none;
  69. border-radius: var(--border-radius);
  70. cursor: pointer;
  71. font-weight: bold;
  72. }
  73. button:hover {
  74. opacity: 0.9;
  75. transform: translateY(-2px);
  76. box-shadow: var(--shadow);
  77. }
  78. .weather-info {
  79. background: var(--secondary-bg-color);
  80. border-radius: var(--border-radius);
  81. padding: 20px;
  82. box-shadow: var(--shadow);
  83. }
  84. .weather-detail {
  85. margin-bottom: 10px;
  86. padding-bottom: 8px;
  87. border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  88. }
  89. .weather-detail:last-child {
  90. border-bottom: none;
  91. margin-bottom: 0;
  92. }
  93. h1,
  94. h2 {
  95. color: var(--accent-color);
  96. border-bottom: var(--border-thickness) solid var(--accent-color);
  97. padding-bottom: 0.3rem;
  98. }
  99. /* 翻译按钮相关 */
  100. #translate {
  101. position: relative;
  102. }
  103. .translateSelectLanguage {
  104. background: inherit;
  105. color: inherit;
  106. opacity: 0;
  107. cursor: pointer;
  108. position: absolute;
  109. top: 0;
  110. right: 0;
  111. bottom: 0;
  112. left: 0;
  113. }
  114. </style>
  115. <script
  116. src="https://lib.baomitu.com/translate.js/3.17.0/translate.js"
  117. type="text/javascript"
  118. ></script>
  119. <link rel="icon" type="image/svg+xml" href="favicon.svg" />
  120. </head>
  121. <body>
  122. <div class="container">
  123. <h1>天气查询</h1>
  124. <div class="search-box">
  125. <input type="text" id="cityInput" placeholder="输入城市名称" />
  126. <button onclick="getWeather()">获取天气</button>
  127. <button id="translate">语言</button>
  128. </div>
  129. <div id="weatherResult" class="weather-info">
  130. <p>正在获取当前位置天气……</p>
  131. </div>
  132. </div>
  133. <script>
  134. // 页面加载时获取当前位置天气
  135. document.addEventListener("DOMContentLoaded", function () {
  136. fetch("https://wttr.in/?format=j1")
  137. .then((response) => response.json())
  138. .then((data) => {
  139. updateWeatherUI(data);
  140. })
  141. .catch((error) => {
  142. const resultDiv = document.getElementById("weatherResult");
  143. resultDiv.innerHTML = `
  144. <p>无法获取当前位置天气</p>
  145. <p>请输入城市名称手动查询</p>
  146. `;
  147. });
  148. });
  149. function updateWeatherUI(data) {
  150. const current = data.current_condition[0];
  151. const area = data.nearest_area[0];
  152. let html = `
  153. <h2>${area.areaName[0].value}天气</h2>
  154. <div class="weather-detail">
  155. <strong>当前天气:</strong> ${current.weatherDesc[0].value}
  156. </div>
  157. <div class="weather-detail">
  158. <strong>温度:</strong> ${current.temp_C}°C (体感 ${current.FeelsLikeC}°C)
  159. </div>
  160. <div class="weather-detail">
  161. <strong>湿度:</strong> ${current.humidity}%
  162. </div>
  163. <div class="weather-detail">
  164. <strong>风速:</strong> ${current.windspeedKmph} km/h (${current.winddir16Point})
  165. </div>
  166. <div class="weather-detail">
  167. <strong>降水量:</strong> ${current.precipMM} mm
  168. </div>
  169. <div class="weather-detail">
  170. <strong>能见度:</strong> ${current.visibility} km
  171. </div>
  172. <div class="weather-detail">
  173. <strong>气压:</strong> ${current.pressure} hPa
  174. </div>
  175. <div class="weather-detail">
  176. <strong>更新时间:</strong> ${current.observation_time}
  177. </div>
  178. `;
  179. const res = document.getElementById("weatherResult");
  180. res.innerHTML = html;
  181. translate.execute([res]);
  182. refreshTranslate();
  183. }
  184. function getWeather() {
  185. const city = document.getElementById("cityInput").value.trim();
  186. if (!city) {
  187. alert("请输入城市名称");
  188. return;
  189. }
  190. const resultDiv = document.getElementById("weatherResult");
  191. resultDiv.innerHTML = "<p>加载中……</p>";
  192. fetch(`https://wttr.in/${city}?format=j1`)
  193. .then((response) => response.json())
  194. .then((data) => {
  195. updateWeatherUI(data);
  196. })
  197. .catch((error) => {
  198. resultDiv.innerHTML = `<p>获取天气信息失败: ${error.message}</p>`;
  199. });
  200. }
  201. // 加载 CSS
  202. function loadCSS(url) {
  203. return new Promise((resolve, reject) => {
  204. //// 检查是否已加载
  205. const existingLinks = document.querySelectorAll(
  206. `link[href="${url}"]`
  207. );
  208. if (existingLinks.length > 0) {
  209. return resolve(url);
  210. }
  211. //// 创建新的 link 元素
  212. const link = document.createElement("link");
  213. link.rel = "stylesheet";
  214. link.type = "text/css";
  215. link.href = url;
  216. //// 成功加载处理
  217. link.onload = () => {
  218. link.onload = null;
  219. link.onerror = null;
  220. resolve(url);
  221. };
  222. //// 加载失败处理
  223. link.onerror = () => {
  224. link.onerror = null;
  225. link.onload = null;
  226. document.head.removeChild(link);
  227. reject(new Error(`无法加载 CSS 文件: ${url}`));
  228. };
  229. //// 添加到文档头部
  230. document.head.appendChild(link);
  231. });
  232. }
  233. // translate-js 翻译系统
  234. //// 初始化翻译系统
  235. function initTranslate() {
  236. try {
  237. translate.service.use("client.edge");
  238. translate.request.listener.start();
  239. translate.setAutoDiscriminateLocalLanguage();
  240. translate.language.setUrlParamControl();
  241. translate.language.translateLocal = true;
  242. translate.ignore.class.push("notTranslate");
  243. translate.execute();
  244. } catch (e) {
  245. console.error("翻译系统出错:" + e);
  246. }
  247. }
  248. //// 刷新翻译
  249. function refreshTranslate() {
  250. try {
  251. translate.selectLanguageTag.refreshRender();
  252. } catch (e) {
  253. console.error("刷新翻译出错:" + e);
  254. }
  255. }
  256. // 初始化
  257. function initialize() {
  258. initTranslate(); //// 初始化页面翻译
  259. loadCSS(
  260. "https://chinese-fonts-cdn.deno.dev/packages/maple-mono-cn/dist/MapleMono-CN-Regular/result.css"
  261. ); //// 加载字体 CSS
  262. }
  263. // 触发器
  264. //// 网页加载完毕后触发初始化
  265. window.addEventListener("DOMContentLoaded", () => initialize());
  266. </script>
  267. </body>
  268. </html>