當我將鼠標懸停在.winner-container
,在JS函數告訴.headline
搬出.winner-container
,它告訴.bottom
遷入.winner-container
。當我離開的時候,恰恰相反。的JavaScript - 應用功能爲特定的div的div集合中使用相同的類名
問題是,我將有數百個這樣的容器,全部都是.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');
});
你應該添加一個id爲您需要管理單獨 – scaisEdge