我希望在滚动时特定DIV出现时将“选定”类添加到[href]。当DIV完全滚动时删除该类。 这段代码正在添加类,但在完全滚动之后,a [href]没有集中注意力。
var aChildren = $(".cd-faq-categories li a"); // find the a children of the list items
var aArray = []; // create the empty aArray
for (var i = 0; i < aChildren.length; i++) {
var aChild = aChildren[i];
var ahref = $(aChild).attr('href');
aArray.push(ahref);
}
$(window).scroll(function () {
var windowPos = $(window).scrollTop(); // get the offset of the window from the top of page
var windowHeight = $(window).height(); // get the height of the window
var docHeight = $(document).height();
for (var i = 0; i < aArray.length; i++) {
var theID = aArray[i];
var divPos = $(theID).offset().top;
// get the offset of the div from the top of page
var divHeight = $(theID).height(); // get the height of the div in question
if (windowPos >= divPos && windowPos < (divPos + divHeight)) {
$("a[href='" + theID + "']").addClass("selected");
} else {
if ($("a[href='#cld9']"))
{
//...
}
else
{
$("a[href='" + theID + "']").removeClass("selected");
}
}
}
if (windowPos + windowHeight == docHeight) {
alert("gfg");
if (!$(".cd-faq-categories li a").hasClass("selected")) {
var navActiveCurrent = $("li").attr("href");
$("a[href='" + navActiveCurrent + "']").removeClass("selected");
$("li a").addClass("selected");
}
}
});
写它的正确方法是什么?
答案 0 :(得分:0)
} else {
if ($("a[href='#cld9']"))
{
//...
}
else
{
$("a[href='" + theID + "']").removeClass("selected");
}
}
我想你可能想要为所有其他ID运行最后一行,你需要做什么
} else if ($("a[href='#cld9']")) {
//...
} else {
$("a[href='" + theID + "']").removeClass("selected");
}
代替