Skip to content

Instantly share code, notes, and snippets.

@aab595
Created February 11, 2022 12:25
Show Gist options
  • Save aab595/aaff7f5d1b6b5b3affc70993189dcad1 to your computer and use it in GitHub Desktop.
Save aab595/aaff7f5d1b6b5b3affc70993189dcad1 to your computer and use it in GitHub Desktop.
CSSTransitionsAnimations - 05 - Exercise
<h1>Exercise 5</h1>
Define a transition on the .box for the "left" property.
Add JavaScript code to the clicked function to toggle the position of the box between left=0 and left=300, i.e., make the box move left to right and right to left when clicked.
(Can you make it move in a square path? Left->Right->Bottom->Right->Top)
<div class="box">Lorem</div>
var el = document.querySelector(".box");
var position = { top: 0, left: 0 };
el.addEventListener("click", clickedBox, false);
function clickedBox(evt) {
if (position.top === 0 && position.left === 0) {
el.style.left = "300px";
position.left = 300;
} else if (position.top === 0 && position.left === 300) {
el.style.top = "300px";
position.top = 300;
} else if (position.top === 300 && position.left === 300) {
el.style.left = "0px";
position.left = 0;
} else if (position.top === 300 && position.left === 0) {
el.style.top = "0px";
position.top = 0;
}
}
.box {
width: 50px;
height: 50px;
background-color: red;
position: absolute;
left: 0px;
top: 0px;
transition: 1s ease-in-out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment