I have a problem with changing the font after pressing a button. In the font array I have a list of font names to which the written function refers. When you press the button, the font changes from default to undefined. I imported a link to the fonts from googlefonts in the CSS file, but I don’t know how to connect them.
The font change button does not work.
HTML
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://kit.fontawesome.com/072211b0fb.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div class="buttons">
<button class="sizeUp">+</button>
<button class="sizeDown">-</button>
<button class="color">color</button>
<button class="font">font</button>
</div>
<div class="text">
<p>Lorem ipsum dolor sit amet.</p>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
@import url('https://fonts.googleapis.com/css?family=Abril+Fatface|Anton|Arvo|Baloo+Bhaina|Droid+Sans+Mono|Josefin+Slab|Lato|Roboto|Ubuntu|Volkhov');
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
height: 100vh;
font-family: 'Montserrat', sans-serif;
color: rgb(255, 255, 255);
background-color: rgba(82, 82, 82);
}
div {
padding: 50px;
}
p {
font-size: 36px;
}
JS
const sizeUp = document.querySelector('.sizeUp')
const sizeDown = document.querySelector('.sizeDown')
const colorBtn = document.querySelector('.color')
const fontBtn = document.querySelector('.font')
const p = document.querySelector('p')
let fontSize = 36
const increase = () => {
if(fontSize>200) return
fontSize += 5
p.style.fontSize = fontSize + 'px'
}
const decrease = () => {
if(fontSize<=21) return
fontSize -= 5
p.style.fontSize = fontSize + 'px'
}
const colorChanger = () => {
const r = Math.floor(Math.random() * 255)
const g = Math.floor(Math.random() * 255)
const b = Math.floor(Math.random() * 255)
p.style.color = `rgb(${r},${g},${b})`
};
const fonts = [
"baloo-bhaina",
"josefin-slab",
"arvo",
"lato",
"volkhov",
"abril-fatface",
"ubuntu",
"roboto",
"droid-sans-mono",
"anton",
];
const fontCharger = (fonts) => {
const number = fonts[Math.floor(Math.random() * fonts.length)];
p.style.fontFamily = fonts[number]
}
sizeUp.addEventListener('click', increase)
sizeDown.addEventListener('click', decrease)
colorBtn.addEventListener('click', colorChanger)
fontBtn.addEventListener('click', fontCharger)
The font change button does not work.
New contributor
Katarzynkah A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.