How can we incorporate an algorithmic solution into an Angular brand carousel?

I am seeking to create a brand slider in Angular without relying on any external libraries or packages. The functionality I desire is as follows:

1 2 3(active) 4 5

2 3 4(active) 5 6

3 4 5(active) 6 7

4 5 6(active) 7 (empty)

5 6 7(active) (empty) (empty)

The desired slider layout is:

6 7 1(active) 2 3

7 1 2(active) 3 4

1 2 3(active) 4 5

2 3 4(active) 5 6

3 4 5(active) 6 7

4 5 6(active) 7 1

5 6 7(active) 1 2

This is the related Angular code:

visibleLogos: Array<{ id: string; name: string; isActive: boolean, src: string }> = [];
private logoChangeInterval: Subscription;

...

ngOnInit() {
    this.updateVisibleLogos();
}

...

updateVisibleLogos() { //this is the problematic algorithm
    const startIndex = this.activeLogoIndex % this.logos.length;

    let endIndex = (startIndex + 4) % this.logos.length;

    if (endIndex === 0) {
        endIndex = this.logos.length - 1;
    } else if (endIndex < startIndex) {
        endIndex = endIndex + this.logos.length;
    }

    if (endIndex >= this.logos.length) {
        endIndex = this.logos.length - 1;
    }

    this.visibleLogos = this.logos.slice(startIndex, endIndex + 1);

    for (const logo of this.visibleLogos) {
        logo.isActive = false;
    }

    this.visibleLogos[2].isActive = true;

    this.activeLogoIndex = (this.activeLogoIndex + 1) % this.logos.length;

    if (this.logoChangeInterval) {
        this.logoChangeInterval.unsubscribe();
    }
    this.logoChangeInterval = interval(3000).subscribe(() => this.updateVisibleLogos());
}

In the HTML code:

<div class="logo-slider">
       <ul class="w-full">
           <li class="bg-gradient" *ngFor="let logo of visibleLogos" [class.active]="logo.isActive"><img src="{{logo.src}}" alt="{{logo.name}}"></li>
       </ul>
  </div>

This is the corresponding SCSS code:

.logo-slider {
    display: flex;
    flex-wrap: nowrap;
    justify-content: center;
    align-items: center;
    width: 100%;
}

.logo-slider ul {
    display: flex;
    list-style-type: none;
    padding: 0;
    margin: 0;
}

.logo-slider li {
    width: 20%; 
    margin: 10px;
    border-radius: 10px;
    padding: 10px;
    opacity: 0.25;
    transition: opacity 0.5s ease-in-out;
    cursor: pointer;
}

.logo-slider li.active {
    opacity: 1;
}

Answer №1

refreshLogosDisplay() {
    const initIndex = this.activeLogoIndex % this.logos.length;
    let finalIndex = (initIndex + 4) % this.logos.length;
    if (finalIndex === 0) {
        finalIndex = this.logos.length - 1;
    } else if (finalIndex < initIndex) {
        finalIndex = finalIndex + this.logos.length;
    }
    if (finalIndex >= this.logos.length) {
        finalIndex = this.logos.length - 1;
    }
    const displayLogos = [];
    for (let j = initIndex; j <= finalIndex; j++) {
        displayLogos.push(this.logos[j]);
    }
    if (displayLogos.length < 5) {
        const remainingItems = this.logos.slice(0, 5 - displayLogos.length);
        displayLogos.push(...remainingItems);
    }
    for (const element of displayLogos) {
        element.isActive = false;
    }
    displayLogos[2].isActive = true;
    this.activeLogoIndex = (this.activeLogoIndex + 1) % this.logos.length;
    this.displayLogos = displayLogos;
    if (this.refreshInterval) {
        this.refreshInterval.unsubscribe();
    }
    this.refreshInterval = interval(3000).subscribe(() => this.refreshLogosDisplay());
}

The solution is shown above.

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

moving the array generated on the HTML page to a different page

Can anyone help me with transferring an array from one page to another? I have a list of names in the code below and I want to be able to access and print this array on another page using JavaScript. Your assistance is greatly appreciated! <html> ...

The style attribute is triggering an error stating that 'Every child in a list must possess a distinct "key" property.'

Can anyone explain why I'm encountering an error when storing JSX code in a variable like this? const centerStyle = {textAlign: 'center'}; viewState.myContent = ( <Fragment> <p style={centerStyle}>Some text</p> < ...

Three.js Photometric Function

Is there a way to specify a photometric function in Three.js? Currently, I am utilizing a Lambert material: new THREE.MeshLambertMaterial({ color: 0xffffff }) However, I am interested in using a Lommel Seeliger but I am unsure of the process and locatio ...

Removing dynamically selected options from one select box that have also been chosen in another select box

Two select boxes are present in my JQuery Mobile application with identical data options. Upon initializing the page, I dynamically populate them as follows: var first = document.getElementById("selectBoxFirst"); var second = document.getElementBy ...

Node.JS AJAX request with no data payload

Encountering an issue where no data is being received on the server side from an AJAX request because the req.body is empty (console logs it as {}). script.js: $("#button").click(function(){ var number = $("#number").val(); $.ajax({ ...

Issue when passing JSON as props: Unable to access the property 'name' because it is undefined

I have a JSON request that returns a JSON object. I want to pass this data to a component function for display purposes, but I'm encountering the following error: Cannot read property 'name' of undefined I've outlined the issue below ...

Troubleshooting: jQuery unable to retrieve input value on initial page load

Has anyone experienced an issue with getting input value using jQuery? It seems to only work on the second instance when the button is pressed, after going back to the page: Here's the HTML code: <input type="text" class="form-control email_inpu ...

Steps to display content post authentication using JWT

Utilizing Nodejs and Express for application development. Utilizing JWT for authentication. I have successfully implemented the JWT-based authentication system and tested it with Postman. However, I am facing an issue when passing the request through the ...

Is there a way to modify the text color of the navigation bar in Bootstrap?

How can I customize the text color of the navigation links in my Bootstrap navbar? I want to specifically change the color of the nav-links to purple while keeping the main title on the left unchanged. Here is the code snippet: <!DOCTYPE html> < ...

Tips for sending a Postgres Pool or Client over a socket

After deploying my server to Heroku, I encountered an issue with sending a request to my Raspberry Pi for running a scraping function and updating a table in the Heroku Postgres database. The problem seems to be related to the client.query() function not b ...

I'm encountering a problem with handling errors in Express.js: A critical error has occurred while attempting to execute the _runMicro

Currently, I am utilizing the Blizzard API Battle.Net to fetch information regarding a character's name and the realm they inhabit. In certain cases, a user may request a character that does not exist, prompting Blizzard to return a 404 error response ...

Select only the top-level items in a dropdown menu using jQuery

Wondering how to create a fade effect that only triggers when hovering over the parent LI's without affecting the items in the dropdown menu? Any suggestions or solutions? Tried numerous examples with no success, struggling to make it work and gettin ...

Difficulty encountered when trying to map an array to JSX - Error: Unexpected token

I am struggling to properly map an employees array to a new array using ReactJS and JSX. It seems like there is an issue with my return method. Please keep in mind that I am a complete beginner when it comes to React and ES6. Can someone guide me on how t ...

Ways to access an element within a function triggered by an event listener

Is there a way to dynamically add a class to an element with a specific class when it is clicked? Specifically, I want to target elements with the "date" class and give them an additional class upon click. $(".date").on("click", function() { // Code t ...

What is the best way to connect specific nodes to the edge of a canvas in cytoscape and create perfectly straight lines?

In the center column of my article, I have two graphs created with cytoscape showing "ancestors" and "descendants" on the sides. I am interested in displaying the connections ("edges") from the articles in the final generation of "ancestors" to the articl ...

How to Validate Ionic 2 Radio Button Selections with TypeScript

Imagine having a list like the one shown below: <ion-list radio-group [(ngModel)]="autoManufacturers"> <ion-list-header> Auto Manufacturers </ion-list-header> <ion-item> <ion-label>Cord</ion-label> &l ...

Tips for properly formatting the bound value prior to its display while utilizing ng-model

Currently, my code looks like this: $scope.value = 0; /// <input type="number" ng-model="value" placeholder="This is not displaying" /> As you can see, the default value of 0 shows up inside the number input instead of the desired placeholder. I ...

What is the best way to make an exit pop up disappear when clicking on any area?

I'm new to programming in JavaScript and jQuery and I wanted to implement an exit pop-up feature. While I came across a helpful tutorial online, I encountered an issue where users could only exit by clicking on the "X" button. I want them to be able t ...

AJAX loading footer content before images are fully loaded

I am a beginner when it comes to ajax and I'm facing an issue where the footer loads before the images, causing the images to overlap the footer. The problem is illustrated in the image below. <!doctype html> <html lang="en"> <head ...

Obtaining the value of an identification number from one service using the identification number from another service

I am currently working on an angular application that retrieves job information, including the customer's name. When storing this data in the jobs table, I make sure to include the customerId within the JobModel. Within my angular job component, I in ...