function moveRectangles(event) {
var rectangles = document.querySelectorAll('.rectangle');
var gap = 5; // Gap between rectangles
var distance = 100; // Distance to move each rectangle
// Check if the click target is a rectangle
if (event.target.classList.contains('rectangle')) {
// Move the rectangles to the left to create a gap
for (var i = 0; i < rectangles.length; i++) {
rectangles[i].style.left = (i * -(distance + gap)) + 'px';
}
} else {
// Reset the position of all rectangles to 0
for (var i = 0; i < rectangles.length; i++) {
rectangles[i].style.left = '0px';
}
}
// Prevent click event from bubbling up to the body
event.stopPropagation();
}
/* CSS to style the rectangles */
.container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.rectangle {
width: 100px;
height: 50px;
background-color: orange;
position: absolute; /* Set position to absolute */
border-radius: 10px; /* Adjust border radius for rectangle shape */
transform: rotate(90deg); /* Rotate rectangles by 90 degrees */
transform-origin: center; /* Set transform origin to the center */
transition: left 0.5s; /* Add transition for smooth movement */
cursor: pointer; /* Change cursor to pointer when hovering over rectangles */
}
<div class="container" onclick="moveRectangles(event)">
<div class="rectangle" style="left: 0px;"></div>
<div class="rectangle" style="left: 0px;"></div>
<div class="rectangle" style="left: 0px;"></div>
<div class="rectangle" style="left: 0px;"></div>
<div class="rectangle" style="left: 0px;"></div>
<div class="rectangle" style="left: 0px;"></div>
</div>
I tried like making the function such that when someone clicks the blank space other than rectangle then the animation should be reversed but im not getting hold of the function on reversing it cause it just sits without doing anything. Like if i click first on the rectangle then then normally it opens or rises up anything u want to call to the left then it should reverse that action when i click the blank space.
It should be like this :
these are the steps of the whole animation
2
Answers
Your
if condition
is wrong. You’re checking if the element contains elements with classes.rectangle
. Instead, you can check theleft
property of a certain element at a certain index. e.g:In this logic, we are checking if the
left
property of the second rectangle is equal to 0px, if so we animate it out, otherwise we reverse the animation.EDIT: How is this different from before?
Before the if-else condition was checking if the parent element has children element with class
.rectangle
. So the else statement would only run if there were no rectangles. Now, it checks if the left property of the second element is 0px, and in this case else statement would run if it is something else than 0px.Your
else
is never hit, because you can never click inside the container currently. Add a border around the container and you will see why.It is positioned absolutely and does not have dimensions, so it is not affected by its children. Thus its size is 0x0. You can’t click on that.
So, you will have to add the closing code to the
click
of thedocument
.