I have a flex container that contains several items that have the flex-grow
property set to 1 and a certain max-width
.
I want to add an event listener to the group of items, but I do not want the event to fire if the event location is in the empty space of the flex container. For example, for a click
event, I want the event to fire only if the blue space of the image below is clicked.
If I add the event listener to the parent container, the event fires even if I click the purple. If I add the event listener to the child items, the event does not fire when if I click on the margins of the items. In addition, for events like onmouseenter
or onmouseleave
, the event fires 3 times if I move my mouse over all the items, whereas my desire is for it to only fire once.
Here is a snippet of the current scenario:
$(".flex-container").on("mouseenter", () => {
$("#hidden").show();
})
$(".flex-container").on("mouseleave", () => {
$("#hidden").hide();
})
$(".flex-item").on("mouseenter", () => {
$("#hidden2").show();
})
$(".flex-item").on("mouseleave", () => {
$("#hidden2").hide();
})
.flex-container {
display: flex;
align-items: center;
justify-content: center;
border: 1px solid black;
margin: 5px;
padding: 5px;
}
.item-container {
display: flex;
align-items: center;
width: 100%;
justify-content: center;
}
.flex-item {
padding: 5px;
margin: 10px;
outline: 1px solid black;
flex: 1 1 auto;
height: 24px;
max-width: 24px;
display: flex;
align-items: center;
justify-content: center;
}
#hidden, #hidden2 {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="flex-container">
<div class="flex-item">
1
</div>
<div class="flex-item">
2
</div>
<div class="flex-item">
3
</div>
</div>
<div id="hidden">
Flex Container Hovered
</div>
<div id="hidden2">
Flex Item Hovered
</div>
Adding an extra flex container as a wrapper achieves close to the intended effect, but the intention of the flex-grow
property of the items is not preserved, as seen below.
The items do not grow to the max-width
of the items to fill up the outer box. Here is a snippet of the above scenario:
$(".flex-container").on("mouseenter", () => {
$("#hidden").show();
})
$(".flex-container").on("mouseleave", () => {
$("#hidden").hide();
})
$(".flex-item").on("mouseenter", () => {
$("#hidden2").show();
})
$(".flex-item").on("mouseleave", () => {
$("#hidden2").hide();
})
$(".extra-wrapper").on("mouseenter", () => {
$("#hidden3").show();
})
$(".extra-wrapper").on("mouseleave", () => {
$("#hidden3").hide();
})
.flex-container {
display: flex;
align-items: center;
justify-content: center;
border: 1px solid black;
margin: 5px;
padding: 5px;
}
.extra-wrapper {
display: flex;
}
.item-container {
display: flex;
align-items: center;
width: 100%;
justify-content: center;
}
.flex-item {
padding: 5px;
margin: 10px;
outline: 1px solid black;
flex: 1 1 auto;
height: 24px;
max-width: 24px;
display: flex;
align-items: center;
justify-content: center;
}
#hidden, #hidden2, #hidden3 {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="flex-container">
<div class="extra-wrapper">
<div class="flex-item">
1
</div>
<div class="flex-item">
2
</div>
<div class="flex-item">
3
</div>
</div>
</div>
<div id="hidden">
Flex Container Hovered
</div>
<div id="hidden2">
Flex Item Hovered
</div>
<div id="hidden3">
Extra Wrapper Hovered
</div>
How do I add an event listener to the items in a flex container when the items have a flex-grow
and a max-width
property?