Tips for updating the class of a button as you switch between slides

My ionic slide box features 4 slides that can be navigated using buttons at the bottom of the screen or by finger-scrolling. The goal is to have the 'active' class added to the button corresponding to the currently active slide.

For instance, if the second slide is active, the second button should have the 'active' class. When transitioning to the third slide, the 'active' class should move from the second button to the third button.

Currently, classes change when clicking on a button but not while scrolling between slides. How can I address this issue using AngularJS?

Link to Codepen: http://codepen.io/lu-kanemon/pen/doBNGz

Here is the code snippet:

menu.html

<ion-view view-title="Menu">

    <ion-content class="padding">

        <ion-slide-box show-pager="false" on-slide-changed="slideHasChanged($index)">

            <ion-slide>
                <h1 class="slide-title"> Matches </h1>
            </ion-slide>

            <ion-slide>
                <h1 class="slide-title"> Fitting jobs </h1>
            </ion-slide>

            <ion-slide>
                <h1 class="slide-title"> Interesting events </h1>
            </ion-slide>

            <ion-slide>
                <h1 class="slide-title"> Group conversations with open positions </h1>
            </ion-slide>

        </ion-slide-box>

    </ion-content>

    <div class="contacts-filters felix-filters button-bar">
        <a class="button button-stable" ng-class="{active: selected=='m'}" ng-click="slide(0); selected='m'"><i class="fa fa-users"></i></a>
        <a class="button button-stable" ng-class="{active: selected=='j'}" ng-click="slide(1); selected='j'"><i class="fa fa-suitcase"></i></a>
        <a class="button button-stable" ng-class="{active: selected=='e'}" ng-click="slide(2); selected='e'"><i class="fa fa-map"></i></a>
        <a class="button button-stable" ng-class="{active: selected=='c'}" ng-click="slide(3); selected='c'"><i class="fa fa-comments"></i></a>
    </div>

</ion-view>

MenuCtrl.js

myApp

.controller('MenuCtrl', function($scope, $stateParams, $ionicSlideBoxDelegate){

//switch slides
$scope.slide = function(to) {
    $scope.current = to;
    $ionicSlideBoxDelegate.slide(to);
}

});

Answer №1

To easily toggle classes in jQuery, you can utilize the addClass(className) and removeClass(className) functions

currentElement.removeClass('active');
nextActiveElement.addClass('active');

An efficient way to handle this is by initializing a variable (current) with the first element, then upon triggering the next active element, remove the class from current and assign it to the newest element. This process continues iteratively!

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

How can we briefly make the list item color and size return to default after clicking the Bootstrap navbar-toggler button?

.navbar .collapse .navbar-nav .nav-item .nav-link { font-family: 'yekan'; color: #fff; opacity: .8; font-size: .9rem; padding-left: 15px; padding-right: 15px } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery. ...

When the enter key is pressed, the anchor tag does not function properly after setting the tabindex value to "0" for the surrounding div element

Having an issue with Safari not focusing on my anchor element. I added tabindex="0" around the div to address this, but now the link doesn't work when pressing enter in any browser. Am I missing something here? <div class="used__button&quo ...

What is the best way to sort and organize this JSON data?

There is an array of JSON objects named events that contains fields such as eventId, eventName, and the date of the event. [{event1}, {event2}, {event3}]; The goal is to iterate through this array and filter out events that occur on the same day. The des ...

Is IBM's Single Sign On Service compatible with JWT tokens for authentication?

Am I able to implement IBM Single Sign On service using JWT tokens instead of a cookie-based session approach? I am working on a web project with Node.js for the backend and Angular.js for the frontend, both as separate applications. Is there an SSO solut ...

How can I adjust this button to match the size of the input field?

I'm in the process of creating a simple landing page and I've encountered an issue with the newsletter signup component. The button in the bootstrap input group template appears much larger than the input field. Here is the current code snippet: ...

Having trouble figuring out how to make my Bootstrap Navbar more responsive

I'm having trouble with the responsive design of my Bootstrap navbar. I want all the buttons to be displayed in a row when on desktop, and then collapsed under the fas fa-bars navbar toggler as the viewport size gets smaller. All necessary stylesheet ...

Toggle Visibility of Div or Element

While exploring a website, I came across a feature that really caught my attention but I'm having trouble figuring out how to recreate it! XD Can anyone lend a hand? I am interested in emulating the copyright notification on this site for my own webs ...

Is the Angular index detection failing due to issues with the $$hasKeys property in indexOf()?

Scenario: role: { roleid=3, name="admin"} availableRoles: [ { roleid=3, name="admin", $$hashKey="object:222"}, { roleid=4, name="plain user", $$hashKey="object:223"} ] currentRoles: [ { roleid=3, name="admin"} ...

The background image fails to load the specified image

I am encountering an issue with utilizing background-image in the style of my HTML page. I am currently developing a login page for my Django application, and when I preview the page, the background image does not appear. Strangely, this code was functioni ...

The module is currently unavailable, however, it can be easily loaded with the correct module name

I've been struggling with understanding dependency injection in AngularJS. Recently, I encountered an error that has me stumped. It seems like there might be some confusion and misuse of dependency injection going on. Here is a snippet of my code: I ...

Refresh Angular user interface modal without having to refresh the entire page

As part of my project, I am incorporating a 3D object using three.js within an Angular UI Modal. The initial loading of the 3D image works perfectly fine. However, I am facing an issue where updating the texture wrapping around the 3D object does not refle ...

Troubleshooting Angular2 Error: Incompatibility with TypeScript - Unable to Assign String

While working on creating a custom pipe in Angular2 for filtering, I encountered the following build error: Error TS2322: Build: Type '() => string' is not assignable to type 'string' Below is my sample code: import { PipeTransf ...

unable to locate public folder in express.js

I recently created a basic server using Node.js with Express, and configured the public folder to serve static files. Within my main index.js file, I included the following code: const express = require('express'); const app = express(); const h ...

Aligning a group of divs within a right-floated div in a precise manner

I believe the concept can be explained best through an example: http://jsfiddle.net/kV9yn/16/ This is my simplified code. The issue arises when the rectangular divs on the right don't align properly. Ideally, I would like them to line up from left t ...

Generate an array of checked inputs to be used when posting to a REST API

I have been using .push() to create a checked array of "List" inputs for posting to a REST API. However, it doesn't seem to be working correctly. When unchecking an item, it is not automatically removed from the array. Does anyone have a better solut ...

Fixing the alignment of the left column table in Bootstrap 3.3.7

Is there a way to fix the left table column (date and all column names) in Bootstrap 3.3.7? I attempted to use position: fixed; but it didn't work as expected. Any suggestions on what I should do next? Check out this fiddle for reference <scr ...

When Content Div and Menu Div collide: Resolving HTML and CSS Dilemmas

I am having an issue with my content navigation div overlapping the menu navigation div. Can anyone please help me figure out what I am missing here? You can find the fiddle link below: https://jsfiddle.net/y4c2xs5j/1/ HTML: <div class="top-nav"> ...

Display the content as a clickable link using Jquery

Here is the view I have created using .NET MVC3: function DisplayingGrid () { var Geo=$('#ddlGeo').val(); var Vertical=$('#ddlVertical').val(); var Month=$('#ddlMonth').val(); if(Vertical=="All") { var Flag=1; ...

Unsuccessful attempt at a basic CSS transition - no success

I'm currently trying to create an image with a gradient that disappears on hover, but I'm struggling to get the transition effect to work. I've experimented with various webkit transitions without success. Here's the HTML: <a href= ...

Sleek animation designed for a personalized cursor when hovering over an object

I'm currently working on improving the transition of the cursor for a smoother experience. Right now, the size abruptly changes when hovered over the target element. Any suggestions on how I can achieve a smoother increase in size, with a better anima ...