A Pen by Abdoul Aziz BA on CodePen.
Created
February 11, 2022 12:25
-
-
Save aab595/aaff7f5d1b6b5b3affc70993189dcad1 to your computer and use it in GitHub Desktop.
CSSTransitionsAnimations - 05 - Exercise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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