【发布时间】:2015-02-18 15:43:04
【问题描述】:
我有以下 DOM 结构:
<div class=”wrapper active”>
<p>content</p>
</div>
<div class=”wrapper”>
<p>content</p>
</div>
<div class=”wrapper”>
<p>content</p>
</div>
<div class=”wrapper”>
<p>content</p>
</div>
<div class=”wrapper”>
<p>content</p>
</div>
每个wrapper div 都有一个全屏的高度和宽度。当用户从一个全屏 wrapper 滚动到下一屏时,active 类将切换到当前显示在视口中的 div。
这些wrapper div 是从 CMS 动态生成的。我需要能够计算wrapper 的总数,然后确定哪个具有active 类。这将允许我使用 jQuery 更新寻呼机以对应于 active 孩子。
这是我的尝试:
$('body').each(
function() {
var numberOfDivs = $('.full-screen-wrapper', $(this)).length);
}
);
$(".pager span:nth-child(1)").addClass("activenav");
我知道这段代码需要一些爱,但我需要一些关于如何解决它的指导。如您所见,我将activenav 类添加到列表中的第一项。我需要这个nth-child 值等于当前的wrapper active div。
只是为了帮助消除任何混乱。这是pager的结构:
<ul class="pager">
<span class="activenav">•</span>
<span class="">•</span>
<span class="">•</span>
<span class="">•</span>
<span class="">•</span>
</ul>
基本上,我只需要正确计算 active 类之前有多少个 div,以便我可以更新 nth-child 值以匹配。
【问题讨论】: