Weather - HTML:: Doctype HTML Head Meta Meta Meta Link Title Title Head Body h1 h1 P Input P Button Button h2 h2
Weather - HTML:: Doctype HTML Head Meta Meta Meta Link Title Title Head Body h1 h1 P Input P Button Button h2 h2
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="weather.css">
<title>Major Project</title>
</head>
<body onload="DefaultScreen()">
<center>
<h1 id="Para1">7 Days Weather Forecast</h1>
<p id="inputContainer">City: <input type="text" id="cityInput"></p>
<button type="button" onclick="GetInfo()">Click Me</button>
<h2 id="cityName">---Delhi---</h2>
</center>
<div class="time" id="time">
12:30 <span id="am-pm">PM</span>
</div>
<div class="date" id="date">
Monday, 25 May
</div>
<div class="current-info">
<div class="current-info">humidity</div>
<div class="weather-item"></div>
</div>
<!-- <div id = "weatherContainer"> -->
<div class="future-forecast">
<div id="iconsContainer">
<div class="today">
<div class = "icons">
<p class="weather" id="day1"></p>
<div class="image"><img src="dots.png" class="imgClass"
id="img1"></div>
<p class="minValues" id="day1Min">Loading...</p>
<p class="maxValues" id="day1Max">Loading...</p>
</div>
</div>
weather.css:
*{/*overall body*/
color: rgb(9, 15, 17);
/*background: rgb(7, 243, 113);*/
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
box-sizing: border-box;
margin:0;
padding:0;
}
button{/*button*/
margin: 3px;
padding:5px;
background-color: antiquewhite;
text-align:center;
font-size:20px;
font-weight:bold;
z-index:5;
}
#weatherContainer{/*first line*/
position: absolute;
top: 70%;
left: 51.5%;
margin-right: -50%;
transform: translate(-50%, -50%);
height: 300px;
width: 1300px;
border: 5px solid rgba(97, 95, 95, 0.829);
border-radius: 10px;
}
#iconsContainer{/*second line*/
display: flex;
position: absolute;
background-color: rgba(0,0,0,0.2);;
top: 80%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%);
height: 240px;
width: 1400px;
border: 5px solid rgb(31, 82, 82);
border-radius: 25px;
justify-content: start;
align-items: none;
/* overflow-y: hidden; */
}
.icons{/*boxes*/
display: inline-bock;
float: left;
height: 200px;
width: 130px;
margin: 20px;
border: 5px solid rgb(48, 47, 47);
border-radius: 10px;
}
.imgClass{/*icons*/
height: 140px;
width: 100%;
}
/* */
@import url('https://fonts.googleapis.com/css2?
family=Poppins:wght@100;200;400;700&display=swap');
body{
background:url('https://images.unsplash.com/photo-1621274403997-
37aace184f49?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-
1.2.1');
background-repeat:round;
background-size:cover;
/* overflow:scroll; */
height: 100vh;
font-family: 'Poppins', sans-serif;
}
.current-info{/*imp/current-info->co1.date-container,col2.place-container*/
display: flex;
padding: 20px 70px;
justify-content: space-between;
flex-wrap: wrap;
}
.date-container{/*date time font size*/
font-weight: 600;
}
.date-container .time{/*time:12:30*/
color: white;
font-size: 170px;
}
.date-container #am-pm{/*am/pm*/
color: white;
font-size: 30px;
margin-left: 20px;
}
.date-container .date{/*date*/
color: white;
font-size: 30px;
}
.place-container{/**/
color: rgb(199, 25, 25);
text-align: end;
}
/*place-container*/.time-zone{/*Asia/Kolkatta*/
color: white;
text-align: right;
font-size: 30px;
font-weight: 100;
text-align: right;
}
.future-forecast .weather-forecast{
display: flex;
}
.weather-forecast .weather-forecast-item{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin: 0 10px;
border: 1px solid #eee;
padding: 15px;
border-radius: 10px;
background: rgba(0,0,0,0.2)
}
.date-container .time{
font-size: 50px;
}
.date-container #am-pm{
font-size: 20px;
}
.date-container .date{
font-size: 20px;
}
/* .place-container{
text-align: end;
margin-top: 15px;
}
.place-container .time-zone{
font-size: 20px;
}*/
.current-info .others{
padding: 12px;
}
}
@media only screen and (max-width: 1400px){
.future-forecast{
justify-content: start;
align-items: none;
overflow-x: scroll;
}
}
weather.js:
function GetInfo()
{
var newName = document.getElementById("cityInput");
var cityName = document.getElementById("cityName");
cityName.innerHTML = "--"+newName.value+"--";
fetch('https://api.openweathermap.org/data/2.5/forecast?
q='+newName.value+'&appid=f02d68936905fe8381426343096ae320')
.then(response => response.json())
.then(data => {
//Getting the min and max values for each day
for(i = 0; i<8; i++)
{
document.getElementById("day" + (i+1) + "Min").innerHTML = "Min: " +
Number(data.list[i].main.temp_min - 273.15).toFixed(1)+ "°";
//Number(1.3450001).toFixed(2); // 1.35
}
for(i = 0; i<8; i++)
{
document.getElementById("day" + (i+1) + "Max").innerHTML = "Max: " +
Number(data.list[i].main.temp_max - 273.15).toFixed(2) + "°";
}
//------------------------------------------------------------
/////
const timeEl = document.getElementById('time');
const dateEl = document.getElementById('date');
const currentWeatherItemsEl = document.getElementById('current-weather-
items');
const timezone = document.getElementById('time-zone');
const countryEl = document.getElementById('country');
const weatherForecastEl = document.getElementById('weather-forecast');
const currentTempEl = document.getElementById('current-temp');
setInterval(() => {
const time = new Date();
const month = time.getMonth();
const date = time.getDate();
const day = time.getDay();
const hour = time.getHours();
const hoursIn12HrFormat = hour >= 13 ? hour %12: hour
const minutes = time.getMinutes();
const ampm = hour >=12 ? 'PM' : 'AM'
}, 1000);
getWeatherData()
function getWeatherData()
{
navigator.geolocation.getCurrentPosition((success) => {
let {latitude, longitude } = success.coords;
fetch(`https://api.openweathermap.org/data/2.5/onecall?lat=$
{latitude}&lon=${longitude}&exclude=hourly,minutely&units=metric&appid=$
{API_KEY}`).then(res => res.json()).then(data => {
console.log(data)
showWeatherData(data);
})
})
}
function showWeatherData(data)
{
let {humidity, pressure, sunrise, sunset, wind_speed} = data.current;
timezone.innerHTML = data.timezone;
countryEl.innerHTML = data.lat + 'N ' + data.lon+'E'
currentWeatherItemsEl.innerHTML =
`<div class="weather-item">
<div>Humidity</div>
<div>${humidity}%</div>
</div>
<div class="weather-item">
<div>Pressure</div>
<div>${pressure}</div>
</div>
<div class="weather-item">
<div>Wind Speed</div>
<div>${wind_speed}</div>
</div>
<div class="weather-item">
<div>Sunrise</div>
<div>${window.moment(sunrise * 1000).format('HH:mm a')}</div>
</div>
<div class="weather-item">
<div>Sunset</div>
<div>${window.moment(sunset*1000).format('HH:mm a')}</div>
</div>
`;
}
/////
function DefaultScreen()
{
document.getElementById("cityInput").defaultValue = "Delhi";
GetInfo();
}
//Getting and displaying the text for the upcoming five days of the week
var d = new Date();
var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday",];
//Function to get the correct integer for the index of the days array
function CheckDay(day)
{
if(day + d.getDay() > 6)
{
return day + d.getDay() - 7;
}
else
{
return day + d.getDay();
}
}
/*document.getElementById("day1Min").innerHTML =
Math.round(data.list[0].main.temp_min - 273.15, -2);
document.getElementById("day2Min").innerHTML =
Math.round(data.list[1].main.temp_min - 273.15, -2);
document.getElementById("day3Min").innerHTML =
Math.round(data.list[2].main.temp_min - 273.15, -2);
document.getElementById("day4Min").innerHTML =
Math.round(data.list[3].main.temp_min - 273.15, -2);
document.getElementById("day5Min").innerHTML =
Math.round(data.list[4].main.temp_min - 273.15, -2);
document.getElementById("day6Min").innerHTML =
Math.round(data.list[5].main.temp_min - 273.15, -2);
document.getElementById("day7Min").innerHTML =
Math.round(data.list[6].main.temp_min - 273.15, -2);
document.getElementById("day8Min").innerHTML =
Math.round(data.list[7].main.temp_min - 273.15, -2);*/
/*document.getElementById("day1Max").innerHTML =
Math.round(data.list[0].main.temp_max - 273.15, -2);
document.getElementById("day2Max").innerHTML =
Math.round(data.list[0].main.temp_max - 273.15, -2);
document.getElementById("day3Max").innerHTML =
Math.round(data.list[0].main.temp_max - 273.15, -2);
document.getElementById("day4Max").innerHTML =
Math.round(data.list[0].main.temp_max - 273.15, -2);
document.getElementById("day5Max").innerHTML =
Math.round(data.list[0].main.temp_max - 273.15, -2);*/
/*document.getElementById("img1").src = "http://openweathermap.org/img/w/"+
data.list[0].weather[0].icon
+".png";
document.getElementById("img2").src = "http://openweathermap.org/img/w/"+
data.list[1].weather[0].icon
+".png";
document.getElementById("img3").src = "http://openweathermap.org/img/w/"+
data.list[2].weather[0].icon
+".png";
document.getElementById("img4").src = "http://openweathermap.org/img/w/"+
data.list[3].weather[0].icon
+".png";
document.getElementById("img5").src = "http://openweathermap.org/img/w/"+
data.list[4].weather[0].icon
+".png";*/
/*
document.getElementById("day1").innerHTML = weekday[CheckDay(0)];
document.getElementById("day2").innerHTML = weekday[CheckDay(1)];
document.getElementById("day3").innerHTML = weekday[CheckDay(2)];
document.getElementById("day4").innerHTML = weekday[CheckDay(3)];
document.getElementById("day5").innerHTML = weekday[CheckDay(4)];*/
/*weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";*/