Question:
I’m trying to make a CMS-based navigation bar using a repeater in Wix Studio that highlights the active link as the user scrolls through each section on the page. However, .addClass()
and .removeClass()
do not seem to work inside the repeater’s onItemReady or forEachItem handlers.
Product:
Wix Studio
What are you trying to achieve:
When the user scrolls to a section, the corresponding nav item should get visually highlighted (e.g., using .addClass("activeMenuBtn")
) — this is not working.
What have you already tried:
- I’m using a Repeater (
#repeaterButtomNav
) connected to a CMS that includes ananchorId
field (e.g.,dimsumSection
,appetizerSection
). - Each nav item is now a Text element with ID
#scrollLink
. I previously used a native Button, but switched to text inside a container. - I’ve added a class called
activeMenuBtn
to the text element in the Design panel.
//scroll highlight
$w.onReady(() => {
$w("#repeaterButtomNav").onItemReady(($item, itemData) => {
const anchorId = itemData.anchorId;
const section = $w(`#${anchorId}`); // cms button
if (section) {
section.onViewportEnter(() => {
$w("#repeaterButtomNav").forEachItem(($repItem, repItemData) => {
const $target = $repItem("#scrollLink"); // my button id
if (repItemData.anchorId === anchorId) {
$target.addClass("activeMenuBtn"); // my button class
} else {
$target.removeClass("activeMenuBtn");
}
});
});
}
});
});
Additional information:
my previous post: Error: scrollTo() Not Working in Wix Studio Repeater with Dynamic Anchor Sections