EventListenners

This commit is contained in:
2024-10-11 19:01:08 +02:00
parent 46be1154bf
commit a49264a1d4
2 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fancy Smiley</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f4f8;
font-family: 'Arial', sans-serif;
}
.smiley-box {
width: 300px;
height: 300px;
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(135deg, #ffcc33, #ffdd55);
border-radius: 50%;
border: 10px solid #ffd700;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
position: relative;
overflow: hidden;
cursor:default;
margin: 0;
}
.smiley {
font-size: 150px;
position: absolute;
}
.smiley-box:hover {
box-shadow: 0 20px 50px rgba(0, 0, 0, 0.4);
transform: scale(1.05);
transition: all 0.3s ease-in-out;
cursor:default;
}
</style>
</head>
<body>
<div class="smiley-box" id="box">
<div class="smiley">😊</div>
</div>
<script src="script.js"></script>
</body>
</html>

View File

@ -0,0 +1,65 @@
//EVENTLISTENNERS
const boxe = document.getElementById("box");
const moveamount = 10;
let y = 0;
boxe.addEventListener("click", changebg);
function changebg(event){
document.body.style.backgroundColor = `rgb(${Math.floor(Math.random() * 256)}, ${Math.floor(Math.random() * 256)}, ${Math.floor(Math.random() * 256)})`;
}
function rotate(){
boxe.style.rotate = "180deg"
}
function backrotate(){
boxe.style.transition = "all 0.3s ease-in-out";
boxe.style.rotate = "0deg";
}
boxe.addEventListener("mouseover", rotate);
boxe.addEventListener("mouseout", backrotate);
//KEEEEY EVENTS
//document.addEventListener("keydown", () => console.log("KEY PRESSED!!"));
//document.addEventListener("keyup", () => console.log("KEY released!!!"));
document.addEventListener("keydown", event =>{
console.log(`Key ${event.key} was pressed!`);
switch (event.key) {
case 'ArrowUp':
y -= moveamount;
break;
case 'ArrowDown':
y += moveamount;
break;
}
boxe.style.top = `${y}px`;
});
//HIDE BLOCKS
let hiden = false;
document.addEventListener("keyup", key => {
if(key.key === ' ' && hiden === false){
boxe.style.display = "none";
hiden = true;
}
else if(key.key === ' ' && hiden === true){
boxe.style.display = "block";
hiden = false;
}
});
document.addEventListener("keyup", key => {
if(key.key === 'h' && hiden === false){
boxe.style.visibility = "hidden";
hiden = true;
}
else if(key.key === 'h' && hiden === true){
boxe.style.visibility = "visible";
hiden = false;
}
});