Assign a unique item number to a text element within each CMS item.
// repeats for each .work-cms-item on the page$(".work-cms-item").each(function (index) {// select only the text element inside this current itemlet childText =$(this).find(".child-text");// set its text to the current index + 1childText.text(index +1);});
This each loop runs for each work-cms-item on the page.
Inside the loop, $(this) refers to the item we're currently looping through instead of every cms item.
index starts at zero. Each time the loop runs, index increases by 1.
For item 1, index equals 0.
For item 2, index equals 1.
For item 3, index equals 2.
Multiple instances of the same section
When we have multiple instances of the same section type on a page, this each loop ensures that elements within a section only impact other elements within that same section.
// repeats for each cta-section on the page$(".cta-section").each(function (index) {// selects only the heading & button inside this current sectionlet currentHeading =$(this).find(".cta-heading");let currentButton =$(this).find(".cta-button");// on mouseenter of this section's buttoncurrentButton.on("mouseenter",function () {// add class to this section's headingcurrentHeading.addClass("is-large"); });// on mouseleave of this section's buttoncurrentButton.on("mouseleave",function () {// remove class from this section's headingcurrentHeading.removeClass("is-large"); });});
Connecting two collection lists
When we hover over a cms-trigger-item, we want to show only the matching target item and hide all other target items.
// repeats for each .cms-section on our page$(".cms-section").each(function () {// find the trigger & target items inside this sectionlet triggerItems =$(this).find(".cms-trigger-item");let targetItems =$(this).find(".cms-target-item");// pass any number into the function to make that item activefunctionmakeItemsActive(index) {// removes is-active from all triggers & targets inside this sectiontriggerItems.add(targetItems).removeClass("is-active");// adds is-active to the trigger at the selected indexlet currentTrigger =triggerItems.eq(index).addClass("is-active");// adds is-active to the target at the selected indexlet currentTarget =targetItems.eq(index).addClass("is-active"); }makeItemsActive(0); // makes the first item active on page load// on hover of any trigger items within this sectiontriggerItems.on("mouseenter",function () {// get the index of this itemlet itemIndex =$(this).index();// make it activemakeItemsActive(itemIndex); });});
Animating only the currently active and previously active items
<scriptsrc="https://cdn.jsdelivr.net/npm/gsap@3.12.5/dist/gsap.min.js"></script><script>$(".cms-section").each(function () { let triggerItems =$(this).find(".cms-trigger-item"); let targetItems =$(this).find(".cms-target-item"); let prevTriggerItem; let prevTargetItem;functionmakeItemsActive(index) {// get the current active itemslet currentTriggerItem =triggerItems.eq(index);let currentTargetItem =targetItems.eq(index);// animate textgsap.to(prevTriggerItem, { opacity:0.4, color:"white" });gsap.fromTo(currentTriggerItem, { opacity:0.4, color:"white" }, { opacity:1, color:"yellow" });// animate imagesgsap.to(prevTargetItem, { opacity:0, scale:0.8 });gsap.fromTo(currentTargetItem, { opacity:0, scale:0.8 }, { opacity:1, scale:1 });// store previously active items for next time function runs prevTriggerItem = currentTriggerItem; prevTargetItem = currentTargetItem; }makeItemsActive(0);triggerItems.on("mouseenter",function () {if (!$(this).is(prevTriggerItem)) {let itemIndex =$(this).index();makeItemsActive(itemIndex); } });});</script>