Exploring Two Arrays in JavaScript Using GSAP

I'm struggling with iterating through two arrays in order to adjust my gsap timeline accordingly.

const subTabs = Array.from(document.querySelectorAll(".subtab"));
const expandTabs = Array.from(document.querySelectorAll(".expandtab"));
const tl = gsap.timeline({ defaults: { duration: 1, yoyo: true, } });

tl.set(expandTabs, {
    visibility: "hidden",
    opacity: 0,
    scale: 0,
});

I want the index values to match up, so if subTabs[0] triggers a "mouseover" event, then I want the corresponding expandTabs[0] to undergo the new animations. The same should happen for the "mouseout" event. What am I missing here?

subTabs.forEach((subTab, index) => {
    const expandTab = expandTabs[index];
    console.log(subTab, expandTab);

    // Event Listener for Hover On
    subTab.addEventListener("mouseover", (event) => {
        console.log("you clicked on region number " + index);
        tl.to(expandTab, {
            visibility: "visible",
            opacity: 1,
            scale: 1,
        });
    });

    // Event Listener for Hover Off
    subTab.addEventListener("mouseout", (event) => {
        console.log("you exited region number " + index);
        tl.to(expandTab, {
            visibility: "hidden",
            opacity: 0,
            scale: 0,
        });
    });
});

Answer №1

Consider switching your second event listener to mouseout instead of another mouseover.

subTab.addEventListener("mouseout", (event) => {

Instead of the index issue, have you thought about using a dynamically generated id to access the element?

You may want to adjust this line for better functionality:

tl.to(expandTab, {
            visibility: "visible",
            opacity: 1,
            scale: 1,
        });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing automatic hyperlinking of words to a webpage address with the help of jQuery

I have a JavaScript array structured as follows: let associativeArray = []; associativeArray["1"] = "First"; associativeArray["2"] = "Second"; associativeArray["3"] = "Third"; My goal is to utilize jQuery to automatically link certain words from the arra ...

React Highchart issue: The synchronized chart and tooltip are failing to highlight the data points

I am currently utilizing the highchart-react-official library to create two types of charts: 1) Line chart with multiple series 2) Column Chart. My objective is to implement a synchronized behavior where hovering over a point in the first line chart hig ...

Exploring a JSON file to generate a customized array for implementing autocomplete functionality in jQuery

I'm currently working on developing an autocomplete feature for a search bar that will display names of places in Norway. The data is being fetched from a REST API URL provided by a third party. As an example, when the input is "st" there are two res ...

Guide to automatically update mysql posts every second

Utilizing ajax, I am able to save user posts/comments into a mysql table without the need for a page refresh. Initially, there is this <div id="posts-container"></div> Next, I attempted to use Jquery load() to iterate through a table and disp ...

Using AngularJS and SpringMVC for uploading multiple files at once

Having recently delved into the world of angularJS, I've been attempting to upload a file using angular JS and Spring MVC. However, despite my efforts, I have not been able to find a solution and keep encountering exceptions in the JS Controller. Bel ...

Incompatibility between PHP and AJAX causes functionality issues

I am facing problems with creating a delete system using PHP, JavaScript, and AJAX. Even though I have done it before, this time PHP and AJAX are not communicating, and the JavaScript script is also inactive. In the JavaScript file (server-log.js), alerts ...

What could be the reason my black overlay doesn't show up when the button is clicked?

Attempting to craft a pop-up window on my own, I encountered an issue. Upon pressing the button, instead of the anticipated pop-up appearing and darkening the background below it, the entire page freezes with no sign of the pop-up box. If I eliminate the ...

Activate scroll event when image src changes in Angular using jQuery

Seeking assistance with utilizing jQuery to scroll to a specific thumbnail inside a modal when left/right arrows are pressed. The modal should appear when the user clicks on an image. I've managed to implement scrolling upon clicking a thumbnail, but ...

"In strict mode, the object is subjected to specific rules

I am facing a challenge with an object in my project that needs to run the content of the page within strict mode. Even after attempting to return it for global scope usage, I still haven't been able to resolve the issue. (function($) { "use stric ...

Simple Steps to Convert an HTML String Array into Dynamic HTML with AngularJS Using ng-repeat

Below is an array consisting of HTML strings as values. How can I convert these strings into executable HTML code to display them? [ {html:'<button>name</button>'}, {html:'<table > <thead> <tr> <th>#</ ...

The problem arises when using `$state.go` as it does not properly transfer the value to `$stateParams`

Within componentA, I am using an angular code: $state.go('home', {selectedFilter: "no_filter"}); In componentB, I have the following code: if ($stateParams.selectedFilter === 'no_filter') However, I am encountering an issue where $s ...

Do the Push Notification APIs in Chrome and Firefox OS follow the same set of guidelines and standards?

Do Chrome and Firefox OS both use Push Notifications APIs that adhere to the same standard? If not, is either one moving towards standardization? ...

Picking the juiciest data from specific dates with MongoDB

I have a large amount of hourly recorded price data that I am organizing in mongodb, and I have a specific requirement to extract the value/price of an item within certain time intervals like 1 day, 1 week, 1 month, 3 months, and so on. The data is stored ...

PHP For Loop Fails to Create Links in Directory Listing

Currently, I am working on a PHP application that is designed to read a directory of images and folders, some possibly containing nested folders and images. The main goal is to scan the directory, then pass the list to a JavaScript array which will display ...

AngularJS textbox validation for numbers and required in repeating mode ensures that the user input is a

For more information, please visit the following link: https://plnkr.co/edit/9HbLMBUw0Q6mj7oyCahP?p=preview var app = angular.module('plunker', []); app.controller('MainCtrl', function($scope) { $scope.NDCarray = [{val: '' ...

Optimal approach for handling large JSON files

I am in possession of a JSON file containing approximately 500 lines. I am hesitant to simply dump this JSON data into the end of my Node.JS file as it doesn't seem like the most efficient approach. What alternatives or best practices can be recommend ...

Use React to increment a variable by a random value until it reaches a specific threshold

I am currently working on creating a simulated loading bar, similar to the one seen on YouTube videos. My goal is for it to last 1.5 seconds, which is the average time it takes for my page to load. However, I have encountered an issue with the following co ...

A guide to troubleshoot and resolve the 500 (Internal Server Error) encountered during an Ajax post request in Laravel

I am facing an issue in my project where I am trying to store data into a database using ajax. However, when I submit the post request, I get an error (500 Internal Server Error). I have tried searching on Google multiple times but the issue remains unreso ...

Unable to integrate a new third-party script into a Next.js application

In my attempt to integrate the following script, I have tried adding it first to _document.js, then to app.js, and finally to a specific page. <Script src="https://anywebsite.ai/chatbot/chatbot.js"></Script> <Script id=" ...

The PHP query that was sent through an AJAX POST request is not being executed correctly

I have a situation where I am working with two pages: Page 1: <input type="hidden" name="rateableUserID" value="<?php echo $rateableUserID;?>"/> <input type="hidden" name="rateablePictureID" value="<?php echo $rateablePictureID;?>"/& ...