In need of some creative brainstorming, I'm working on a website design featuring ten overlapping cards. When the mouse hovers over a card, it expands while others move away. My goal is for each card to return to its original position once the cursor moves off it. Here's the HTML code snippet:
Exploring solutions, I attempted using onMouseOver and onMouseOut in HTML but hit roadblocks. Similarly, my trials with GSAP's timeline animation proved challenging to control.
The current setup involves adding and removing event listeners with timeouts to regulate function calls' speed.
var cards = document.getElementsByClassName('card');
var currentCard;
var currentIndex;
var leftSpread = 150;
var rightSpread = 200;
var initialOffset = 100;
(function initialLoad() {
for (var i = 0; i < cards.length; i++) {
cards[i].style.zIndex = i;
cards[i].addEventListener("mouseenter", this);
if (i > 0) {
cards[i].style.left = cards[i - 1].offsetLeft + initialOffset + 'px';
}
}
})();
function handleEvent(evt) {
switch (evt.type) {
case "mouseenter":
this.cardMouseOver(evt);
break;
case "mouseout":
this.cardMouseOut(evt);
break;
default:
return;
}
}
function cardMouseOver(event) {
currentIndex = event.target.style.zIndex;
event.target.style.zIndex = 10;
for (var i = 0; i < cards.length; i++) {
if (event.target == cards[i]) {
currentCard = i;
} else {
cards[i].removeEventListener("mouseenter", this);
}
}
setTimeout(function() {
cards[currentCard].addEventListener("mouseout", this);
}, 50);
for (var i = 0; i < cards.length; i++) {
if (i < currentCard) {
cards[i].style.left = cards[i].offsetLeft - leftSpread + 'px';
} else if (i > currentCard) {
cards[i].style.left = cards[i].offsetLeft + rightSpread + 'px';
}
}
cards[currentCard].removeEventListener("mouseenter", this);
}
function cardMouseOut(event) {
cards[currentCard].style.zIndex = currentIndex;
setTimeout(function() {
for (var i = 0; i < cards.length; i++) {
cards[i].addEventListener("mouseenter", this);
}
}, 100);
for (var i = 0; i < cards.length; i++) {
if (i === currentCard) {
cards[i].removeEventListener("mouseout", this);
}
}
for (var i = 0; i < cards.length; i++) {
if (i < currentCard) {
cards[i].style.left = cards[i].offsetLeft + leftSpread + 'px';
} else if (i > currentCard) {
cards[i].style.left = cards[i].offsetLeft - rightSpread + 'px';
}
}
}
body {
background-color: #242424;
padding: 0;
margin: 0;
}
.cards-container {
background: #fff;
margin: 20px auto;
position: absolute;
left: 21%;
top: 375px;
}
.card {
position: absolute;
background: rgb(255, 255, 255);
border: 1px solid black;
height: 250px;
transition: 0.2s;
width: 200px;
box-shadow: -1px 0px 1px 1px rgba(0, 0, 0, 0.747);
}
.card:hover {
transition: all 0.2s ease;
width: 250px;
height: 350px;
top: -75px;
}
<body>
<header>
<div class="cards-container">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
</div>
</header>
</body>
<script type="text/javascript" src="http://code.jquery.com/jquery-3.4.0.min.js"></script>
My aim is to achieve smooth transitions based on the highlighted card and reset positions when no card is highlighted.