I have 2 cards with the class card
displayed within a card-stack
. There can be any number of such cards.
<div class="card-stack">
<div class="clear"><button name="clear" value="Clear all" onclick="removeAllCards(this)">Clear All</button></div>
<div class="card" style="--y:1" onclick="remove(this)">
<div class="card-header">
<div class="card-avatar">
<img src="https://static.thenounproject.com/png/363633-200.png">
</div>
<div class="card-title">Announcement 1</div>
</div>
<div class="card-body">
This is announcement 1.
</div>
</div>
<div class="card" style="--y:2" onclick="remove(this)">
<div class="card-header">
<div class="card-avatar">
<img src="https://static.thenounproject.com/png/363633-200.png">
</div>
<div class="card-title">Announcement 1</div>
</div>
<div class="card-body">
This is announcement 1.
</div>
</div>
</div>
When hovering on the card-stack
, CSS is applied to each card
.card-stack:hover .card{
transform: translateY(calc((var(--y) * -105% + 200px)));
box-shadow: none;
}
I want the HOVER effect to apply only if there are more than one elements with the class card
. How can this be achieved using CSS?
CodePen link for the snippet above
function remove(element) {
element.remove();
}
function removeAllCards(element) {
element.remove();
document.querySelectorAll(".card").forEach(el => el.remove());
}
body {
background-color: #e8eaed;
}
.card {
width: 300px;
min-height: 150px;
background-color: white;
position: fixed;
top: 75vh;
right: 100px;
transform: translate(-50%, -50%);
display: grid;
border-radius: 5px;
font-family: sans-serif;
font-size: 14px;
color: #00000080;
box-shadow: 0 5px 15px 0 #00000040, 0 5px 5px 0#00000020;
transition: transform 200ms;
padding:15px;
cursor:pointer;
}
.card {
transform: translateY(calc((var(--y) * 20px) - 50%)) scale(calc(1.0 + var(--y) * 0.05));
}
.card-avatar {
display:block;
}
.card-avatar img {
width:40px;
height:40px;
float:left;
}
.card-title {
font-size:16px;
display:inline-block;
margin-top:10px;
margin-left:10px;
}
.card-header {
height:30px;
display: inline-block;
}
.card-body {
margin-top:-30px;
}
.card-stack:hover .card{
transform: translateY(calc((var(--y) * -105% + 200px)));
box-shadow: none;
}
.card-stack:hover .clear {
visibility: visible;
transform: translateY(calc((var(--y) * -105% + 200px)));
position:absolute;
top:2vh;
right:40px;
}
.card-stack {
position:fixed;
width: 400px;
min-height: 100vh;
position: fixed;
right: 65px;
}
.clear {
visibility:hidden;
}
<div class="card-stack">
<div class="clear"><button name="clear" value="Clear all" onclick="removeAllCards(this)">Clear All</button></div>
<div class="card" style="--y:1" onclick="remove(this)">
<div class="card-header">
<div class="card-avatar">
<img src="https://static.thenounproject.com/png/363633-200.png">
</div>
<div class="card-title">Announcement 1</div>
</div>
<div class="card-body">
This is announcement 1.
</div>
</div>
<div class="card" style="--y:2" onclick="remove(this)">
<div class="card-header">
<div class="card-avatar">
<img src="https://static.thenounproject.com/png/363633-200.png">
</div>
<div class="card-title">Announcement 1</div>
</div>
<div class="card-body">
This is announcement 1.
</div>
</div>
</div>