-
Notifications
You must be signed in to change notification settings - Fork 0
/
tenki.html
207 lines (181 loc) · 6.77 KB
/
tenki.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>天気予報</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f9;
}
h1 {
text-align: center;
color: #333;
margin: 20px 0;
}
#region-select {
display: block;
margin: 20px auto;
padding: 10px;
font-size: 16px;
border-radius: 5px;
border: 1px solid #ccc;
background: #fff;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
cursor: pointer;
width: 80%;
max-width: 300px;
text-align: center;
appearance: none;
background-image: url('https://example.com/icons/down-arrow.svg');
background-repeat: no-repeat;
background-position: right 10px center;
background-size: 20px;
}
#region-select:hover {
border-color: #888;
}
#weather {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 20px;
margin: 20px;
}
.forecast {
background: #fff;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
width: 250px;
padding: 20px;
text-align: center;
position: relative;
transition: transform 0.3s;
cursor: pointer;
}
.forecast:hover {
transform: scale(1.05);
}
.forecast img {
width: 80px;
height: 80px;
margin: 10px auto;
}
.forecast h3 {
margin: 10px 0;
font-size: 18px;
color: #555;
}
.forecast p {
font-size: 16px;
color: #666;
}
.forecast a {
text-decoration: none;
color: inherit;
display: block;
height: 100%;
}
.sunny {
background: linear-gradient(135deg, #ffeb3b, #ff9800);
color: #333;
}
.cloudy {
background: linear-gradient(135deg, #cfd8dc, #90a4ae);
color: #333;
}
.rainy {
background: linear-gradient(135deg, #bbdefb, #2196f3);
color: #fff;
}
.snowy {
background: linear-gradient(135deg, #e3f2fd, #90caf9);
color: #333;
}
</style>
</head>
<body>
<h1>3日間の天気</h1>
<select id="region-select">
<option value="170000">金沢</option>
<option value="130000">東京</option>
<option value="270000">大阪</option>
<option value="400000">九州</option>
</select>
<div id="weather"></div>
<script>
const baseUrl = "https://www.jma.go.jp/bosai/forecast/data/forecast/";
const weatherIcons = {
"晴れ": { icon: "https://www.jma.go.jp/bosai/forecast/img/100.svg", className: "sunny" },
"くもり": { icon: "https://www.jma.go.jp/bosai/forecast/img/200.svg", className: "cloudy" },
"雨": { icon: "https://www.jma.go.jp/bosai/forecast/img/300.svg", className: "rainy" },
"雪": { icon: "https://www.jma.go.jp/bosai/forecast/img/400.svg", className: "snowy" },
"雷雨": { icon: "https://www.jma.go.jp/bosai/forecast/img/500.svg", className: "rainy" }
};
const regionLinks = {
"170000": "https://www.jma.go.jp/bosai/forecast/#area_type=offices&area_code=170000", // 北陸地方リンク
"130000": "https://www.jma.go.jp/bosai/forecast/#area_type=offices&area_code=130000", // 東京地方リンク
"270000": "https://www.jma.go.jp/bosai/forecast/#area_type=offices&area_code=270000", // 大阪地方リンク
"400000": "https://www.jma.go.jp/bosai/forecast/#area_type=offices&area_code=400000" // 九州北部地方リンク
};
function getWeatherStyle(weather) {
// 天気説明の最初の単語を抽出
const firstWord = weather.split(/[\s、。]/)[0]; // 空白、読点、句点で分割
// 最初の単語でスタイルを決定
for (const key in weatherIcons) {
if (firstWord === key) {
return weatherIcons[key];
}
}
// デフォルトスタイル(該当しない場合)
return { icon: "https://example.com/icons/unknown.png", className: "" };
}
async function fetchWeatherData(regionCode) {
try {
const response = await fetch(`${baseUrl}${regionCode}.json`);
if (!response.ok) {
throw new Error("データの取得に失敗しました");
}
const data = await response.json();
const forecasts = data[0].timeSeries[0];
const dates = forecasts.timeDefines;
const weatherCodes = forecasts.areas[0].weathers;
const areaName = data[0].publishingOffice;
const weatherDiv = document.getElementById("weather");
weatherDiv.innerHTML = `<h2></h2>`;
dates.forEach((date, index) => {
const weatherText = weatherCodes[index];
const { icon, className } = getWeatherStyle(weatherText);
const forecastDiv = document.createElement("div");
forecastDiv.className = `forecast ${className}`;
forecastDiv.innerHTML = `
<a href="${regionLinks[regionCode]}" target="_blank">
<img src="${icon}" alt="${weatherText}">
<h3>${new Date(date).toLocaleDateString("ja-JP", {
weekday: "short",
year: "numeric",
month: "2-digit",
day: "2-digit"
})}</h3>
<p>${weatherText}</p>
</a>
`;
weatherDiv.appendChild(forecastDiv);
});
} catch (error) {
console.error(error);
document.getElementById("weather").textContent = "天気予報を取得できませんでした。";
}
}
document.getElementById("region-select").addEventListener("change", (event) => {
const regionCode = event.target.value;
fetchWeatherData(regionCode);
});
// 初期表示
fetchWeatherData("170000");
</script>
</body>
</html>