HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URL's added here will be added as <link>
s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
If the stylesheet you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="main-container row justify-content-center">
<div class="col-3">
<div class="location-container">
<input type="search" name="location-search" placeholder="Enter city, country" class="location-search_text">
<button class="btn btn-primary location-search_button">❯</button>
</div>
<div class="city"></div>
</div>
<div class="current-temp-container col-3">
<div class="temp"></div>
<div class="tempform__button-container">
<button class="btn btn-primary convert__button">
°F ↔ °C
</button>
</div>
<div class="icon_image">
<div class="icon sun-shower">
<div class="cloud"></div>
<div class="sun">
<div class="rays"></div>
</div>
<div class="rain"></div>
</div>
<div class="icon thunder-storm">
<div class="cloud"></div>
<div class="lightning">
<div class="bolt"></div>
<div class="bolt"></div>
</div>
</div>
<div class="icon cloudy">
<div class="cloud"></div>
<div class="cloud"></div>
</div>
<div class="icon flurries">
<div class="cloud"></div>
<div class="snow">
<div class="flake"></div>
<div class="flake"></div>
</div>
</div>
<div class="icon sunny">
<div class="sun">
<div class="rays"></div>
</div>
</div>
<div class="icon rainy">
<div class="cloud"></div>
<div class="rain"></div>
</div>
<div class="wthr"></div>
</div>
</div>
<div class="five-day-forecast_container col-3">
<div>
<div class="humidity_container">
<span class="humidity_text">Humidity</span>
<span class="humidity"></span>
</div>
<div class="wind_container">
<span class="wind_text">Wind</span>
<span class="wind"></span>
</div>
</div>
<div class="day1-container">
<div class="day1 future future-day"></div>
<div class="day1-temp future"></div>
<div class="day1-weather future"></div>
</div>
<div class="day2-container">
<div class="day2 future future-day"></div>
<div class="day2-temp future"></div>
<div class="day2-weather future"></div>
</div>
<div class="day3-container">
<div class="day3 future future-day"></div>
<div class="day3-temp future"></div>
<div class="day3-weather future"></div>
</div>
<div class="day4-container">
<div class="day4 future future-day"></div>
<div class="day4-temp future"></div>
<div class="day4-weather future"></div>
</div>
<!--<div class="day5-container">
<div class="day5 future future-day"></div>
<div class="day5-temp future"></div>
</div>-->
</div>
</div>
<script src="main.js"></script>
</body>
</html>
body {
background-color: #212529 !important;
color: #212529 !important;
margin: 0 auto !important;
font-family: 'Roboto', sans-serif;
text-align: center;
}
.wrapper {
width: 100%;
}
h2 {
color: white;
}
.location-container {
display: flex;
justify-content: center;
margin-bottom: 20px;
}
.location-search_text {
/* padding: 5px 0px;*/
border: none;
width: 80%;
padding: 20px;
font-size: 2.5rem;
background-color: #212529 !important;
color: white;
border-bottom: 1px solid white;
}
.location-search_button {
margin-left: 5px;
}
.main_container {
display: flex;
justify-content: center;
flex-direction: column;
}
.current-temp-container {
display: flex;
flex-direction: column;
align-items: center;
}
.temp {
font-size: 60px;
padding: 10px 0px;
color: white;
width: 140px;
}
.city {
font-size: 20px;
color: #333;
padding: 5px 40px;
background-color: white;
border-radius: 5px;
margin: 0 auto;
}
.convert__button {
cursor: pointer;
}
.wthr {
color: white;
font-size: 45px;
padding-top: 20px;
}
.btn {
border-radius: 5px;
padding: 6px 20px !important;
}
.humidity_wind-container {
display: flex;
justify-content: space-around;
}
.humidity_container, .wind_container {
display: flex;
flex-direction: row;
}
.humidity_text, .wind_text, .humidity, .wind {
color: white;
font-size: 20px;
padding: 5px;
}
.five-day-forecast_container {
display: flex;
justify-content: space-evenly;
padding-top: 20px;
}
.future {
color: white;
}
.future-day {
padding-bottom: 10px;
}
html { box-sizing: border-box; }
html *,
html *:before,
html *:after { box-sizing: inherit; }
h1 {
margin-bottom: 1.375em;
color: #fff;
font-weight: 100;
font-size: 2em;
text-transform: uppercase;
}
p,
a {
color: rgba(255,255,255,0.3);
font-size: small;
}
p { margin: 1.375rem 0; }
.icon {
position: relative;
display: none;
width: 12em;
height: 7em;
font-size: 1em; /* control icon size here */
}
.cloud {
position: absolute;
z-index: 1;
top: 50%;
left: 50%;
width: 3.6875em;
height: 3.6875em;
margin: -1.84375em;
background: #212529;
border-radius: 50%;
box-shadow:
-2.1875em 0.6875em 0 -0.6875em,
2.0625em 0.9375em 0 -0.9375em,
0 0 0 0.375em #fff,
-2.1875em 0.6875em 0 -0.3125em #fff,
2.0625em 0.9375em 0 -0.5625em #fff;
}
.cloud:after {
content: '';
position: absolute;
bottom: 0;
left: -0.5em;
display: block;
width: 4.5625em;
height: 1em;
background: #212529;
box-shadow: 0 0.4375em 0 -0.0625em #fff;
}
.cloud:nth-child(2) {
z-index: 0;
background: #fff;
box-shadow:
-2.1875em 0.6875em 0 -0.6875em #fff,
2.0625em 0.9375em 0 -0.9375em #fff,
0 0 0 0.375em #fff,
-2.1875em 0.6875em 0 -0.3125em #fff,
2.0625em 0.9375em 0 -0.5625em #fff;
opacity: 0.3;
transform: scale(0.5) translate(6em, -3em);
animation: cloud 4s linear infinite;
}
.cloud:nth-child(2):after { background: #fff; }
.sun {
position: absolute;
top: 50%;
left: 50%;
width: 2.5em;
height: 2.5em;
margin: -1.25em;
background: #fff96d;
border-radius: 50%;
box-shadow: 0 0 0 0.375em #fff;
animation: spin 12s infinite linear;
}
.rays {
position: absolute;
top: -2em;
left: 50%;
display: block;
width: 0.375em;
height: 1.125em;
margin-left: -0.1875em;
background: #fff;
border-radius: 0.25em;
box-shadow: 0 5.375em #fff;
}
.rays:before,
.rays:after {
content: '';
position: absolute;
top: 0em;
left: 0em;
display: block;
width: 0.375em;
height: 1.125em;
transform: rotate(60deg);
transform-origin: 50% 3.25em;
background: #fff;
border-radius: 0.25em;
box-shadow: 0 5.375em #fff;
}
.rays:before {
transform: rotate(120deg);
}
.cloud + .sun {
margin: -2em 1em;
}
.rain,
.lightning,
.snow {
position: absolute;
z-index: 2;
top: 50%;
left: 50%;
width: 3.75em;
height: 3.75em;
margin: 0.375em 0 0 -2em;
background: #212529;
}
.rain:after {
content: '';
position: absolute;
z-index: 2;
top: 50%;
left: 50%;
width: 1.125em;
height: 1.125em;
margin: -1em 0 0 -0.25em;
background: #0cf;
border-radius: 100% 0 60% 50% / 60% 0 100% 50%;
box-shadow:
0.625em 0.875em 0 -0.125em rgba(255,255,255,0.2),
-0.875em 1.125em 0 -0.125em rgba(255,255,255,0.2),
-1.375em -0.125em 0 rgba(255,255,255,0.2);
transform: rotate(-28deg);
animation: rain 3s linear infinite;
}
.bolt {
position: absolute;
top: 50%;
left: 50%;
margin: -0.25em 0 0 -0.125em;
color: #fff;
opacity: 0.3;
animation: lightning 2s linear infinite;
}
.bolt:nth-child(2) {
width: 0.5em;
height: 0.25em;
margin: -1.75em 0 0 -1.875em;
transform: translate(2.5em, 2.25em);
opacity: 0.2;
animation: lightning 1.5s linear infinite;
}
.bolt:before,
.bolt:after {
content: '';
position: absolute;
z-index: 2;
top: 50%;
left: 50%;
margin: -1.625em 0 0 -1.0125em;
border-top: 1.25em solid transparent;
border-right: 0.75em solid;
border-bottom: 0.75em solid;
border-left: 0.5em solid transparent;
transform: skewX(-10deg);
}
.bolt:after {
margin: -0.25em 0 0 -0.25em;
border-top: 0.75em solid;
border-right: 0.5em solid transparent;
border-bottom: 1.25em solid transparent;
border-left: 0.75em solid;
transform: skewX(-10deg);
}
.bolt:nth-child(2):before {
margin: -0.75em 0 0 -0.5em;
border-top: 0.625em solid transparent;
border-right: 0.375em solid;
border-bottom: 0.375em solid;
border-left: 0.25em solid transparent;
}
.bolt:nth-child(2):after {
margin: -0.125em 0 0 -0.125em;
border-top: 0.375em solid;
border-right: 0.25em solid transparent;
border-bottom: 0.625em solid transparent;
border-left: 0.375em solid;
}
.flake:before,
.flake:after {
content: '\2744';
position: absolute;
top: 50%;
left: 50%;
margin: -1.025em 0 0 -1.0125em;
color: #fff;
list-height: 1em;
opacity: 0.2;
animation: spin 8s linear infinite reverse;
}
.flake:after {
margin: 0.125em 0 0 -1em;
font-size: 1.5em;
opacity: 0.4;
animation: spin 14s linear infinite;
}
.flake:nth-child(2):before {
margin: -0.5em 0 0 0.25em;
font-size: 1.25em;
opacity: 0.2;
animation: spin 10s linear infinite;
}
.flake:nth-child(2):after {
margin: 0.375em 0 0 0.125em;
font-size: 2em;
opacity: 0.4;
animation: spin 16s linear infinite reverse;
}
/* Animations */
@keyframes spin {
100% { transform: rotate(360deg); }
}
@keyframes cloud {
0% { opacity: 0; }
50% { opacity: 0.3; }
100% {
opacity: 0;
transform: scale(0.5) translate(-200%, -3em);
}
}
@keyframes rain {
0% {
background: #0cf;
box-shadow:
0.625em 0.875em 0 -0.125em rgba(255,255,255,0.2),
-0.875em 1.125em 0 -0.125em rgba(255,255,255,0.2),
-1.375em -0.125em 0 #0cf;
}
25% {
box-shadow:
0.625em 0.875em 0 -0.125em rgba(255,255,255,0.2),
-0.875em 1.125em 0 -0.125em #0cf,
-1.375em -0.125em 0 rgba(255,255,255,0.2);
}
50% {
background: rgba(255,255,255,0.3);
box-shadow:
0.625em 0.875em 0 -0.125em #0cf,
-0.875em 1.125em 0 -0.125em rgba(255,255,255,0.2),
-1.375em -0.125em 0 rgba(255,255,255,0.2);
}
100% {
box-shadow:
0.625em 0.875em 0 -0.125em rgba(255,255,255,0.2),
-0.875em 1.125em 0 -0.125em rgba(255,255,255,0.2),
-1.375em -0.125em 0 #0cf;
}
}
@keyframes lightning {
45% {
color: #fff;
background: #fff;
opacity: 0.2;
}
50% {
color: #0cf;
background: #0cf;
opacity: 1;
}
55% {
color: #fff;
background: #fff;
opacity: 0.2;
}
}
(function($) {
var dom;
// initialize the module
var init = function() {
console.clear();
cacheDom();
bindHandlers();
getWeatherFromUserLocation();
};
// caching the DOM
var cacheDom = function() {
dom = {};
dom.container = $(".main-container");
dom.city = dom.container.find('.city');
dom.wthr = dom.container.find('.wthr');
dom.temp = dom.container.find('.temp');
dom.searchText = dom.container.find('.location-search_text');
dom.searchButton = dom.container.find('.location-search_button');
dom.button = dom.container.find('.convert__button');
dom.clouds = dom.container.find('.cloudy');
dom.rain = dom.container.find('.rainy');
dom.sunny = dom.container.find('.sunny');
dom.humidity = dom.container.find('.humidity');
dom.wind = dom.container.find('.wind');
dom.day1 = dom.container.find('.day1');
dom.day1Temp = dom.container.find('.day1-temp');
dom.day1Wthr = dom.container.find('.day1-weather');
dom.day2 = dom.container.find('.day2');
dom.day2Temp = dom.container.find('.day2-temp');
dom.day2Wthr = dom.container.find('.day2-weather');
dom.day3 = dom.container.find('.day3');
dom.day3Temp = dom.container.find('.day3-temp');
dom.day3Wthr = dom.container.find('.day3-weather');
dom.day4 = dom.container.find('.day4');
dom.day4Temp = dom.container.find('.day4-temp');
dom.day4Wthr = dom.container.find('.day4-weather');
};
// binding event handlers
var bindHandlers = function() {
dom.button.on('click', convertTemp);
dom.searchButton.on('click', requestForecast);
dom.searchText.on('keyup', onEnterPressed);
};
// setting up temperature variables
var temperatures = {
fahrenheit: '',
fahrenheitDay1: '',
fahrenheitDay2: '',
fahrenheitDay3: '',
fahrenheitDay4: '',
celsius: '',
celsiusDay1: '',
celsiusDay2: '',
celsiusDay3: '',
celsiusDay4: '',
convertTempToCelsius: function() {
var fahrenheit = parseInt(this.fahrenheit);
return Math.round(((fahrenheit) - 32) * (5 / 9)) + "°C";
}
};
// load forecast based on user input
var requestForecast = function() {
var city = dom.searchText[0].value;
let api = 'https://api.openweathermap.org/data/2.5/forecast?q=' + city + '&appid=4d8b02dd6a0915262d9f1d723f477f27&units=imperial';
$.ajax({
method: "GET",
url: api,
cache: false,
datatype: "json"
}).then(insertData);
}
// when user presses enter
var onEnterPressed = function(e) {
e.preventDefault();
if (event.keyCode === 13) {
requestForecast();
}
}
var insertData = function(data) {
// console.log(data);
let d = new Date().getTime();
console.log('d', d);
console.log('new d', new Date(d));
let currentTime = d + data.city.timezone;
console.log('cT', currentTime);
console.log('new ct', new Date(currentTime));
console.log(new Date(1394104654000));
// console.log(Date(Date.now()).toString());
// console.log(d + data.city.timezone);
// console.log(data.city.timezone);
dom.city.html(`${data.city.name}, ${data.city.country}`);
//getting dates from api call
let d1 = new Date(data.list[8].dt_txt);
let d2 = new Date(data.list[16].dt_txt);
let d3 = new Date(data.list[24].dt_txt);
let d4 = new Date(data.list[32].dt_txt);
//creating fahrenheit temp variable
temperatures.fahrenheit = Math.round(data.list[0].main.temp) + "°F";
temperatures.fahrenheitDay1 = Math.round(data.list[8].main.temp) + "°F";
temperatures.fahrenheitDay2 = Math.round(data.list[16].main.temp) + "°F";
temperatures.fahrenheitDay3 = Math.round(data.list[24].main.temp) + "°F";
temperatures.fahrenheitDay4 = Math.round(data.list[32].main.temp) + "°F";
//creating description variable
let description = data.list[0].weather[0].main;
//populating HTML with data from ajax call
dom.temp.html(temperatures.fahrenheit);
dom.wthr.html(description);
dom.humidity.html(data.list[0].main.humidity + '%');
dom.wind.html(Math.round(data.list[0].wind.speed) + 'mph');
if (dom.wthr[0].textContent == "Clouds") {
dom.clouds.css({
"display": "block"
})
} else if (dom.wthr[0].textContent == "Rain" || dom.wthr[0].textContent == "Mist") {
dom.rain.css({
"display": "block"
})
} else if (dom.wthr[0].textContent == "Clear") {
dom.sunny.css({
"display": "block"
});
}
dom.day1.html(d1.toString().slice(0, 3));
dom.day1Temp.html(temperatures.fahrenheitDay1);
dom.day1Wthr.html(data.list[8].weather[0].main);
dom.day2.html(d2.toString().slice(0, 3));
dom.day2Temp.html(temperatures.fahrenheitDay2);
dom.day2Wthr.html(data.list[16].weather[0].main);
dom.day3.html(d3.toString().slice(0, 3));
dom.day3Temp.html(temperatures.fahrenheitDay3);
dom.day3Wthr.html(data.list[24].weather[0].main);
dom.day4.html(d4.toString().slice(0, 3));
dom.day4Temp.html(temperatures.fahrenheitDay4);
dom.day4Wthr.html(data.list[32].weather[0].main);
}
//getting location from user
var getWeatherFromUserLocation = function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
let lat = position.coords.latitude;
let lon = position.coords.longitude;
let api = 'https://api.openweathermap.org/data/2.5/forecast?lat=' + lat + '&lon=' + lon + '&appid=4d8b02dd6a0915262d9f1d723f477f27&units=imperial';
$.ajax({
method: "GET",
url: api,
cache: false,
datatype: "json"
//populating HTML with data from ajax call
}).then(insertData);
});
}
}
var convertTemp = function() {
if (dom.temp[0].innerHTML == temperatures.fahrenheit || dom.day1Temp.html == temperatures.fahrenheitDay1 || dom.day2Temp.html == temperatures.fahrenheitDay2 || dom.day3Temp.html == temperatures.fahrenheitDay3 || dom.day4Temp.html == temperatures.fahrenheitDay4) {
dom.temp.html(temperatures.convertTempToCelsius())
dom.day1Temp.html(temperatures.convertTempToCelsius());
dom.day2Temp.html(temperatures.convertTempToCelsius());
dom.day3Temp.html(temperatures.convertTempToCelsius());
dom.day4Temp.html(temperatures.convertTempToCelsius());
} else {
dom.temp.html(temperatures.fahrenheit);
dom.day1Temp.html(temperatures.fahrenheitDay1);
dom.day2Temp.html(temperatures.fahrenheitDay2);
dom.day3Temp.html(temperatures.fahrenheitDay3);
dom.day4Temp.html(temperatures.fahrenheitDay4);;
}
};
init();
})(jQuery);
Also see: Tab Triggers