Taran 2
Taran 2
Coding
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">
<title>WeatherAPI</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Merriweather+
Sans:wght@300;400;500;600;700&display=swap" rel="stylesheet" />
<link rel="shortcut icon" href="favicon.png" type="image/x-icon">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="wrapper">
<h1>Weather App</h1>
<div class="tab-container">
<p class="tab" data-userWeather>Your Weather</p>
<p class="tab" data-searchWeather>Search Weather</p>
</div>
<div class="weather-container">
<!-- grant location container-->
<div class="sub-container grant-location-container">
<img src="location.png" width="80" height="80" loading="lazy">
<p>Grant Location Access</p>
[13]
<p>Allow Access to get weather Information</p>
<button class="btn" data-grantAccess>Grant Access</button>
</div>
<!--card 1-->
<div class="parameter">
<img src="wind.png" >
<p>windspeed</p>
<p data-windspeed></p>
</div>
<!--card 2-->
<div class="parameter">
<img src="humidity.png" >
<p>humidity</p>
<p data-humidity></p>
</div>
<!--card 3-->
<div class="parameter">
<img src="cloud.png" >
<p>Clouds</p>
<p data-cloudiness></p>
</div>
</div>
</div>
</div>
</div>
<script src="index.js"></script>
</body>
</html>
[15]
Java script
const userTab = document.querySelector("[data-userWeather]");
const searchTab = document.querySelector("[data-searchWeather]");
const userContainer = document.querySelector(".weather-container");
function switchTab(newTab) {
if(newTab != oldTab) {
oldTab.classList.remove("current-tab");
oldTab = newTab;
oldTab.classList.add("current-tab");
if(!searchForm.classList.contains("active")) {
//kya search form wala container is invisible, if yes then make it
visible
userInfoContainer.classList.remove("active");
grantAccessContainer.classList.remove("active");
searchForm.classList.add("active");
}
else {
[16]
searchForm.classList.remove("active");
userInfoContainer.classList.remove("active");
//ab main your weather tab me aagya hu, toh weather bhi display karna
poadega, so let's check local storage first
//for coordinates, if we haved saved them there.
getfromSessionStorage();
}
}
}
userTab.addEventListener("click", () => {
//pass clicked tab as input paramter
switchTab(userTab);
});
searchTab.addEventListener("click", () => {
//pass clicked tab as input paramter
switchTab(searchTab);
});
//API CALL
try {
const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?lat=$
{lat}&lon=${lon}&appid=${API_KEY}&units=metric`
);
const data = await response.json();
loadingScreen.classList.remove("active");
userInfoContainer.classList.add("active");
renderWeatherInfo(data);
}
catch(err) {
loadingScreen.classList.remove("active");
//HW
function renderWeatherInfo(weatherInfo) {
//fistly, we have to fethc the elements
function getLocation() {
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
else {
//HW - show an alert for no gelolocation support available
}
}
function showPosition(position) {
const userCoordinates = {
lat: position.coords.latitude,
lon: position.coords.longitude,
}
[19]
sessionStorage.setItem("user-coordinates", JSON.stringify(userCoordinates));
fetchUserWeatherInfo(userCoordinates);
try {
const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=$
{API_KEY}&units=metric`
);
const data = await response.json();
loadingScreen.classList.remove("active");
userInfoContainer.classList.add("active");
renderWeatherInfo(data);
}
catch(err) {
//hW
}
} [20]
CSS .tab{
*,*::before,*::after { cursor: pointer;
margin: 0; font-size: 0.875rem;
padding: 0; letter-spacing: 1.75px;
box-sizing: border-box; padding: 5px 8px;
font-family: 'Merriweather Sans', sans-serif; }
}
:root {
.tab.current-tab{
--colorDark1: #112D4E; background-color: rgba(219, 226,
--colorDark2: #3F72AF; 239, 0.5);
--colorLight1: #DBE2EF; border-radius: 4px;
--colorLight2: #F9F7F7; }
}
.weather-container{
.wrapper{
margin-block:4rem;
width:100vw;
height:100vh; }
color: var(--colorLight2);
background-image: linear-gradient(160deg, .btn{
#112d4e 0%, #3f72af 100%);; all: unset;
} /* appearance: none;
border:none;
h1 { color: white; */
text-align: center;
font-size: 0.85rem;
text-transform:uppercase;
padding-top: 20px; text-transform: uppercase;
} border-radius: 5px;
background-color: var(--
.tab-container { colorDark2);
width:90%; cursor: pointer;
max-width: 550px; padding: 10px 30px;
margin: 0 auto; margin-bottom: 10px;
margin-top: 4rem;
}
display: flex;
justify-content: space-between; .sub-container{
} display:flex;
flex-direction:column; [21]
align-items: center; .loading-container p{
} text-transform: uppercase;
}
.grant-location-container{
display:none; .user-info-container{
} display:none;
}
.grant-location-container.active{
display:flex; .user-info-container.active{
} display: flex;
}
.grant-location-container img{
margin-bottom: 2rem; .name{
} display: flex;
align-items: center;
.grant-location-container p:first-of-type{ gap: 0 0.5rem;
font-size: 1.75rem; margin-bottom: 1rem;
font-weight: 600; }
}
.user-info-container p{
.grant-location-container p:last-of-type{ font-size:1.5rem;
font-size:0.85rem; font-weight:200;
font-weight: 500; }
margin-top: 0.75rem;
margin-bottom: 1.75rem; .user-info-container img{
letter-spacing: 0.75px; width:90px;
} height:90px;
}
.loading-container{
display: none; .name p{
} font-size:2rem;
}
.loading-container.active{
display: flex; .name img{
} [22]
width: 30px; height:50px;
height:30px; }
object-fit: contain;
} .parameter p:first-of-type{
font-size: 1.15rem;
.user-info-container p[data-temp] { font-weight:600;
font-size:2.75rem; text-transform: uppercase;
font-weight:700; }
}
.parameter p:last-of-type{
.parameter-container{ font-size: 1rem;
width:90%; font-weight: 200;
display: flex; }
gap: 10px 20px;
justify-content: center; .form-container{
align-items: center; display: none;
margin-top: 2rem; width:90%;
} max-width:550px;
margin:0 auto;
.parameter{ justify-content: center;
width:30%; align-items: center;
max-width:200px; gap: 0 10px;
background-color: rgba(219, 226, 239, margin-bottom: 3rem;
0.5);; }
border-radius: 5px;
padding:1rem; .form-container.active{
display: flex; display: flex;
flex-direction: column; }
gap:5px 0;
/* justify-content: space-between; */ .form-container input{
align-items: center; all:unset;
} width: calc(100% - 80px);
.parameter img{ height:40px;
width:50px; padding: 0 20px;
[23]
background-color:rgba(219, 226, 239,
0.5);
border-radius: 10px;
}
.form-container input::placeholder{
color: rgba(255, 255, 255, 0.7);
}
.form-container input:focus{
outline: 3px solid rgba(255, 255, 255,
0.7);
}
.form-container .btn {
padding:unset;
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 100%;
margin-bottom:1px;
}