2 回答

TA貢獻1993條經(jīng)驗 獲得超6個贊
使用index()確定集合中哪個元素的索引被懸停
const content = ['Item 1','Item 2','Item 3','Item 4','Item 5','Item 6'];
const $sq = $('.hov-sq').hover(function() {
? const idx = $sq.index(this);
? $('.c-1').attr('data-content', content[idx]);
}, function(){
? ?// remove when mouse leaves if wanted?
? ?$('.c-1').attr('data-content','')
});
.landing-hov-s {
? width: 100vw;
? height: 100vh;
? position: absolute;
? top: 0;
? left: 0;
? right: 0;
? bottom: 0;
? display: flex;
? flex-wrap: wrap;
}
.hov-sq {
? width: 33.3333333vw;
? height: 50vh;
? z-index: 5000;
}
.c-1::after {
? /* other styling not relevant to issue */
? content: attr(data-content) '';
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="landing-hov-s">
? <div class="hov-sq">One</div>
? <div class="hov-sq">Two</div>
? <div class="hov-sq">Three</div>
? <div class="hov-sq">Four</div>
? <div class="hov-sq">Five</div>
? <div class="hov-sq">Six</div>
</div>
<div class="c-1">
? <h1>Laurent<br> Chevrette</h1>
</div>

TA貢獻2021條經(jīng)驗 獲得超8個贊
jQuery 的索引方法
如果您使用的是 jQuery,則可以訪問該index()
方法。
out
我還添加了一旦鼠標位于懸停的 div 上就會運行的函數(shù)。
也就是說,請注意您不能選擇偽元素和 Javascript,因此您應(yīng)該使用 CSS 中的屬性選擇器來設(shè)置它們的樣式,如下所示。
$('.hov-sq').hover(function() {
? $('.c-1')
? ? .attr('data-content', 'frontend developer')
? ? .attr('data-index', $(this).index());
? console.log($(this).index())
}, function() {
? $('.c-1')
? ? .attr('data-content', '')
? ? .attr('data-index', '');
});
.landing-hov-s {
? width: 100vw;
? height: 100vh;
? position: absolute;
? top: 0;
? left: 0;
? right: 0;
? bottom: 0;
? display: flex;
? flex-wrap: wrap;
}
.hov-sq {
? width: 33.3333333vw;
? height: 50vh;
? z-index: 5000;
}
.c-1::after {
? /* other styling not relevant to issue */
? content: attr(data-content) '';
}
.c-1[data-index="0"]::after {
? /* Style for index 0 */
}
/* Rest of the styles */
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="landing-hov-s">
? <div class="hov-sq"></div>
? <div class="hov-sq"></div>
? <div class="hov-sq"></div>
? <div class="hov-sq"></div>
? <div class="hov-sq"></div>
? <div class="hov-sq"></div>
</div>
<div class="c-1">
? <h1>Laurent<br> Chevrette</h1>
</div>
添加回答
舉報