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 URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
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.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and 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 esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM 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.
<!-- loading screen -->
<div class="loading-screen">
<h2> Loading... </h2>
<!-- http://www.freepngimg.com/png/16233-pokemon-free-png-image/download -->
<img class='loading-pokemon'>
<!-- https://loading.io/ -->
<img src='https://res.cloudinary.com/ahmedagadir/image/upload/v1530730083/pokemon-API/loader-1.svg'>
</div>
<div class='wrap1'>
<!-- landing page -->
<nav class="navbar navbar-expand-md navbar-dark bg-warning">
<a class="navbar-brand" href="#"> Pokémon Battle Finder <img src='https://res.cloudinary.com/ahmedagadir/image/upload/v1530730099/pokemon-API/pokedex.png'></a>
<a class='navbar=brand ml-auto' href='https://github.com/AhmedAGadir' target="_blank">
<img src='https://res.cloudinary.com/ahmedagadir/image/upload/v1530730074/pokemon-API/github-logo.png'>
</a>
</nav>
<div class="container mt-5">
<header> <h2 class='text-center'> Double Damage Finder </h2> </header>
<p>...So your opponent's team has a range of strong pokémon of different types. Not to worry, type in one or more of those types here to find some excellent pokémon which can deal <span class='text-primary'> double damage </span> to your opponent!</p>
<form class='form-group'>
<div class="row">
<input type="text" class='form-control col-sm-10' placeholder='Enter a comma separated list of pokémon types...' id='pokemon-types-input'>
<input type="submit" class='btn btn-primary btn-block col-sm-2' value='Go'>
</div>
</form>
<!-- outputs -->
<div id="output-header"></div>
<div id='output' class='center-block'>
<!-- have to remove the below element if using main.js -->
<div class="card-columns my-3"></div>
</div>
<!-- errors -->
<div class='container error my-4 row'>
<div class="col-md-6">
<h2 class='text-center'></h2>
<p class='text-center'></p>
</div>
<div class='col-md-6'>
<div class="tenor-gif-embed" data-postid="16694846" data-share-method="host" data-width="100%" data-aspect-ratio="1.3681318681318682"><a href="https://tenor.com/view/sad-pikachu-crying-pokemon-gif-16694846">Sad Pikachu GIF</a> from <a href="https://tenor.com/search/sad-gifs">Sad GIFs</a></div><script type="text/javascript" async src="https://tenor.com/embed.js"></script>
</div>
</div>
<div id='loader'></div>
</div>
</div>
<audio src='https://res.cloudinary.com/ahmedagadir/video/upload/v1530730993/pokemon-API/funny.wav'></audio>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background-color: lightseagreen;
}
nav img {
height: 40px;
margin-left: 10px;
}
@media (max-width: 400px) {
nav a[href='https://github.com/AhmedAGadir'] {
display: none
}
}
.container {
margin-top: 4rem;
padding: 0 60px;
}
/* loading screen */
.loading-screen {
background-color: lightseagreen;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.loading-screen h2 {
position: absolute;
top: 80px;
}
.loading-screen img[src='https://res.cloudinary.com/ahmedagadir/image/upload/v1530730083/pokemon-API/loader-1.svg'] {
position: absolute;
width: 340px;
}
.wrap1 {
display: none;
}
/* typography */
header {
text-transform: uppercase;
text-shadow: 1px 1px 1px #343A40;
}
h2, p, li {
color: #fff;
}
#output-header {
text-transform: uppercase;
}
#output-header p {
text-shadow: 1px 1px 1px #343A40;
}
.card img {
min-width: 160px;
min-height: 160px;
/* filter: drop-shadow(0 0 5px #fff);
-webkit-filter: drop-shadow(0 0 5px #fff);
-moz-filter: drop-shadow(0 0 5px #fff);
-o-filter: drop-shadow(0 0 5px #fff);
-ms-filter: drop-shadow(0 0 5px #fff);*/
}
.card {
background: rgba(255,255,255,0.7);
background-size: auto 100%;
width: 215px;
box-shadow: 1px 2px 2px #343A40,
inset 1px 1px 2px #fff;
}
.card-body {
padding: 1rem;
}
li {
font-size: 15px;
letter-spacing: 0.2px;
text-transform: uppercase;
text-shadow: 0.5px 0.5px 0.5px ;
text-shadow: 0.2px 0.2px 0 grey;
}
.list-group-item {
background-color: #fff;
box-shadow: inset 1px 1px 1px #fff;
}
.list-group-item:first-child {
border-top-left-radius: 0;
border-top-right-radius: 0;
}
.list-group-item:last-child {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}
/* responsive cards */
.card-columns .card {
display: inline-block;
width: 100%
}
.card-columns {
-webkit-column-gap: 1.25rem;
column-gap: 1.25rem;
orphans: 1;
widows: 1;
}
@media (max-width: 575px) {
.card-columns {
-webkit-column-count: 1;
column-count: 1;
}
}
@media (min-width: 576px) {
.card-columns {
-webkit-column-count: 2;
column-count: 2;
}
input.btn {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
input.form-control {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
}
@media (min-width: 768px) {
.card-columns {
-webkit-column-count: 3;
column-count: 3;
}
}
@media (min-width: 992px) {
.card-columns {
-webkit-column-count: 4;
column-count: 4;
}
}
/* error handling */
.error.container {
display: none;
background: rgba(255,255,255,0.8);
border-radius: 5px;
padding: 15px;
margin: 0;
color: #444242;
}
.error h2 {
color: #444242;
}
.error p {
color: #444242;
}
.error img {
width: 100%;
}
.error .col-md-6:last-child {
display: flex;
align-items: center;
}
/* loading more pokemon */
#loader {
text-align: center;
margin-bottom: 50px;
}
#loader img[src='https://res.cloudinary.com/ahmedagadir/image/upload/v1530730083/pokemon-API/loader.svg'] {
width: 80px;
}
#loader .loading-more {
position: fixed;
bottom: 0;
z-index: 1;
margin: 0 auto;
right: calc(50% - 40px);
}
/* overriding default styling */
@media (min-width: 1200px) {
.container {
max-width: 100%;
}
}
@media (min-width: 992px) {
.container {
max-width: 100%;
}
}
@media (min-width: 768px){
.container {
max-width: 100%;
}
}
@media (min-width: 576px) {
.container {
max-width: 100%;
}
}
let typeArray;
let doubleDamageInfo;
let numberOfPokemon;
let doubleDamageFrmArray;
let displayedPokemon;
// error handling
let audio = document.querySelector('audio');
function handleError(error) {
console.log(error)
document.getElementById('output-header').innerHTML = '';
document.getElementById('loader').innerHTML = ``;
document.querySelector('#output .card-columns').innerHTML = ``;
document.querySelector('.error.container').style.display = 'flex';
if (error.message == 'error: not found') {
document.querySelector('.error h2').textContent = error.message.toUpperCase();
document.querySelector('.error p').innerHTML = `you made pikachu cry! <br>Make sure that all your type inputs are included in the list below: <br><br> <strong>${arrToStr(typeArray)}</strong>`;
} else {
document.querySelector('.error h2').textContent = error.message.toUpperCase();
// fix this message (below) - sometimes it's not applicable
document.querySelector('.error p').innerHTML = `We're experiencing some issues :( ... pikachu feels for you`;
}
audio.currentTime = 42.7;
audio.play();
}
// init --- temp solution otherwise: https://crossorigin.me
let init = {
headers: {'content-type': 'application/json'},
mode: 'cors',
}
window.addEventListener('DOMContentLoaded', () => {
//loading pokemon image
document.querySelector('img.loading-pokemon').src = `http://assets.pokemon.com/assets/cms2/img/pokedex/detail/${('00' + (Math.floor(Math.random()*806)+1)).slice(-3)}.png`;
// remove loading screen
function removeLoadingScreen() {
setTimeout(()=> {
document.querySelector('.loading-screen').style.display = 'none';
document.querySelector('.wrap1').style.display = 'initial';
document.querySelector('.form-group input').focus();
},5000)
}
removeLoadingScreen()
// get pokemon types
let p1 = new Promise((resolve, reject) => {
if (!localStorage.getItem('typeArray')) {
fetch(`https://pokeapi.co/api/v2/type/`, init)
.then(res => {
if (res.ok) return res.json();
else throw new Error(`${res.status}: ${res.statusText}`)
}, err => {
throw new Error(`${err.name}: ${err.message}`)
})
.then(data => {
typeArray = data.results.map(type => type.name);
localStorage.setItem('typeArray', JSON.stringify(typeArray));
console.log('typeArray added to local storage: ', typeArray);
resolve(typeArray)
})
.catch(error => {
reject(error)
})
} else {
typeArray = JSON.parse(localStorage.getItem('typeArray'));
console.log('typeArray already in local storage: ', typeArray);
resolve(typeArray)
}
})
// get damage info
if (!localStorage.getItem('doubleDamageInfo')) {
p1.then(typeArray => {
let promiseArray = typeArray.map(type => {
return fetch(`https://pokeapi.co/api/v2/type/${type}`, init)
.then(res => {
if (res.ok) return res.json();
else throw new Error(`${res.status}: ${res.statusText}`)
}, err => {
throw new Error(`${err.name}: ${err.message}`)
})
})
return Promise.all(promiseArray);
})
.then(data => {
doubleDamageInfo = data.map(type => {
return {
type: type.name,
dbl_damage_to: type.damage_relations.double_damage_to,
dbl_damage_from: type.damage_relations.double_damage_from,
}
});
localStorage.setItem('doubleDamageInfo', JSON.stringify(doubleDamageInfo));
console.log('doubleDamageInfo added to local storage', doubleDamageInfo);
})
.catch(error => {
handleError(error)
})
} else {
doubleDamageInfo = JSON.parse(localStorage.getItem('doubleDamageInfo'));
console.log('doubleDamageInfo alreay in local storage', doubleDamageInfo);
}
})
// form submitting
function arrToStr(arr) {
let arrClone = arr.map(i => i);
if (arrClone.length == 1) return `${arrClone[0]}`;
else if (arrClone.length == 2 ) return `${arrClone[0]} and ${arrClone[1]}`;
else {
let a = arrClone.shift();
return `${a}, ${arrToStr(arrClone)}`
}
}
document.querySelector('.form-group').addEventListener('submit', event => {
event.preventDefault();
let input = document.querySelector('#pokemon-types-input');
let inputArray = input.value.replace(/\s*/g, '').toLowerCase().split(',');
let inputStr = arrToStr(inputArray);
// reset
numberOfPokemon = 0;
doubleDamageFrmArray = [];
audio.pause();
input.value = '';
displayedPokemon = [];
document.querySelector('.error.container').style.display = 'none';
document.querySelector('#output .card-columns').innerHTML = '';
// start search
document.getElementById('output-header').innerHTML =
`<p class='mb-3 text-center'><strong> pokémon which are strong against <span class='text-primary'>${inputStr}</span> types</strong></p>`;
document.getElementById('loader').innerHTML =
`<p> hold on, this could take a minute.... </p>
<img src='https://res.cloudinary.com/ahmedagadir/image/upload/v1530730083/pokemon-API/loader.svg' class='my-2'>`
let errorFound = false;
inputArray.forEach(input => {
if (!typeArray.includes(input)) {
let error = new Error('error: not found');
handleError(error);
errorFound = true;
return;
}
for (let data of doubleDamageInfo) {
if (input == data.type) doubleDamageFrmArray.push(data.dbl_damage_from);
}
})
if (!errorFound) {
doubleDamageFrmArray = doubleDamageFrmArray.reduce((a,b) => a.concat(b)).map(i => i.name);
doubleDamageFrmArray = Array.from(new Set(doubleDamageFrmArray));
// https://stackoverflow.com/questions/38373364/the-best-way-to-remove-duplicate-strings-in-an-array
doubleDamageFrmArray = doubleDamageFrmArray.map(type => {
return fetch(`https://pokeapi.co/api/v2/type/${type}`, init)
.then(res => {
if (res.ok) return res.json();
else throw new Error(`${res.status}: ${res.statusText}`)
}, err => {
throw new Error(`${err.name}: ${err.message}`)
})
})
Promise.all(doubleDamageFrmArray)
.then(data => data.map(data => data.pokemon))
.then(data => {
doubleDamageFrmArray = data;
addPokemon(16);
})
.catch(err => handleError(err))
}
})
function addPokemon(n) {
let pokemonArray = [];
for (let i = 0; i < n; i++) {
let r1 = doubleDamageFrmArray[Math.floor(Math.random() * doubleDamageFrmArray.length)];
let r2 = r1[Math.floor(Math.random() * r1.length)].pokemon;
// disallow duplicate pokemon
if (displayedPokemon.map(p => p.name).includes(r2.name)) {
i--;
continue;
} else {
pokemonArray.push(r2);
displayedPokemon.push(r2);
}
}
Promise.all(pokemonArray.map(pokemon => {
return fetch(pokemon.url, init)
.then(res => {
if (res.ok) return res.json();
else throw new Error(`${res.status}: ${res.statusText}`)
}, err => {
throw new Error(`${err.name}: ${err.message}`)
})
})
)
.then(pokemonArray => {
let cards = '';
pokemonArray.forEach((pokemon, ind) => {
console.log(pokemon)
let types = '';
let dbl_dmg_to = [];
pokemon.types.forEach(type => {
types += (pokemon.types.indexOf(type) !== pokemon.types.length-1) ? `${type.type.name}, ` : ` ${type.type.name}`;
doubleDamageInfo.forEach(item => {
if (item.type == type.type.name) {
dbl_dmg_to.push(item.dbl_damage_to);
}
})
})
dbl_dmg_to = dbl_dmg_to.reduce((a,b) => a.concat(b)).reduce((a, b) => {
if (!a.name.split(', ').includes(b.name)) {
return {name: `${a.name}, ${b.name}`}
} else {
return {name: a.name}
}
})
let abilities = '';
pokemon.abilities.forEach(ability => {
abilities += (pokemon.abilities.indexOf(ability) !== pokemon.abilities.length-1) ? `${ability.ability.name}, ` : ` ${ability.ability.name}`
})
let stats = '';
pokemon.stats.forEach(stat => {
stats += (pokemon.stats.indexOf(stat) !== pokemon.stats.length-1) ? `<br>${stat.stat.name}: ${stat.base_stat}` : `<br>${stat.stat.name}: ${stat.base_stat}`
})
let r3 = Math.floor(Math.random() * pokemon.types.length);
let backgroundType = (typeArray.includes(pokemon.types[r3].type.name)) ? pokemon.types[r3].type.name : 'colorless';
let backgroundURLs = {
steel: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730137/pokemon-API/steel.png',
bug: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730134/pokemon-API/bug.png',
unknown: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730131/pokemon-API/unknown.png',
water: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730130/pokemon-API/water.png',
shadow: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730126/pokemon-API/shadow.png',
psychic: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730124/pokemon-API/psychic.png',
poison: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730123/pokemon-API/poison.png',
normal: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730116/pokemon-API/normal.png',
rock: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730115/pokemon-API/rock.png',
ground: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730101/pokemon-API/ground.png',
ice: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730094/pokemon-API/ice.png',
grass: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730091/pokemon-API/grass.png',
colorless: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730083/pokemon-API/colorless.png',
flying: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730082/pokemon-API/flying.png',
fairy: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730079/pokemon-API/fairy.png',
ghost: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730078/pokemon-API/ghost.png',
fire: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730072/pokemon-API/fire.png',
fighting: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730063/pokemon-API/fighting.png',
dragon: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730059/pokemon-API/dragon.png',
dark: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730060/pokemon-API/dark.png',
electric: 'https://res.cloudinary.com/ahmedagadir/image/upload/v1530730034/pokemon-API/electric.jpg'
}
cards += `
<div class='card' style="background-image: url('${backgroundURLs[backgroundType]}')">
<img src="http://assets.pokemon.com/assets/cms2/img/pokedex/detail/${('00' + pokemon.id).slice(-3)}.png" class='card-img-top'>
<div class='card-body'>
<ul class=list-group>
<li class='list-group-item text-primary'>name: ${pokemon.name}</li>
<li class='list-group-item text-warning'>types: ${types}</li>
<li class='list-group-item text-info'>abilities: ${abilities}</li>
<li class='list-group-item text-success'>base stats: ${stats}</li>
<li class='list-group-item text-danger'>double damage to: ${dbl_dmg_to.name} types</li>
</ul>
</div>
</div>
`
if (ind == pokemonArray.length - 1) {
document.getElementById('loader').innerHTML = ``;
window.addEventListener('scroll', addMorePokemon);
numberOfPokemon += n;
document.querySelector('#output .card-columns').innerHTML += cards;
}
})
})
.catch(error => handleError(error))
}
// add more pokemon to the page on scrolling
window.addEventListener('scroll', addMorePokemon);
function addMorePokemon(e) {
if ((pageYOffset + innerHeight)/document.body.scrollHeight >= 0.9 && numberOfPokemon < 80) {
window.removeEventListener('scroll', addMorePokemon);
addPokemon(8);
document.getElementById('loader').innerHTML = `<img src='https://res.cloudinary.com/ahmedagadir/image/upload/v1530730083/pokemon-API/loader.svg' class='my-2 loading-more'>`;
}
}
Also see: Tab Triggers