当我悬停在
问题是,我将有数百个这些容器,所有容器都具有
.winner-container
上时,JS函数会告诉.headline
移出.winner-container
,并告诉.bottom
移入.winner-container
。当我取消悬停时,相反的情况发生。问题是,我将有数百个这些容器,所有容器都具有
.winner-container
类。所以我意识到,当我悬停在其中一个容器上时,该函数会同时应用于数百个不同的容器。我只想将该函数应用于我正在悬停的特定容器。我可以通过为每个容器分配一个id,然后为每个id编写新的JS代码来实现此目的,但考虑到将有数百个这些div,这将需要很多工作。是否有更优雅的解决方案?
https://jsfiddle.net/6sm6ajht/
HTML
<div class="winner-container">
<div class="top">
<h4 class="headline">SME Example 1</h4>
</div>
<div class="bottom">
<div class="winner-words">
<h6>SME Examle 1 is a technology company.</h6>
<h6><a>Learn more...</a></h6>
</div>
</div>
</div>
<div class="winner-container">
<div class="top">
<h4 class="headline">SME Example 2</h4>
</div>
<div class="bottom">
<div class="winner-words">
<h6>SME Examle 2 is an e-commerce company.</h6>
<h6><a>Learn more...</a></h6>
</div>
</div>
</div>
CSS
.winner-container {
position: relative;
box-shadow: 0px 2.5px 1px 0px rgba(0, 0, 0, 0.25);
border: 1px solid #074E68;
}
.winner-container,
.top,
.bottom {
width: 10em;
height: 12em;
overflow: hidden;
}
.bottom {
position: absolute;
height: 12em;
width: 100%;
top: 12em;
transition: 0.5s ease-in-out;
}
.top .headline {
position: absolute;
top: 2.5em;
width: 100%;
background: rgba(255, 255, 255, 0.8);
box-shadow: 0px 1px 1px 2px rgba(0, 0, 0, 0.1);
transition: 0.5s ease-in-out;
}
.top-up .headline {
top: -2.5em;
}
.bottom-up.bottom {
top: 0em;
background-color: rgba(0, 0, 0, 0.65);
}
JavaScript
$(".winner-container").on("mouseenter", function() {
$(".top").addClass('top-up');
$(".bottom").addClass('bottom-up');
});
$(".winner-container").on("mouseleave", function() {
$(".top").removeClass('top-up');
$(".bottom").removeClass('bottom-up');
});