How to make a button in Angular 4 smoothly transition on hover

My goal is to create a transition effect on buttons similar to the ones showcased on this Bootstrap page. However, I require more customization than what Bootstrap provides, so I am attempting to achieve this through CSS styles and effects.

Here is how I currently display my buttons:

<div class="container">
      <div *ngFor="let cat of categories">
        <ul>
          <li>
            <button [ngStyle]="matchButtonColor(cat)" (mouseover)="transition()" class="btn " type="button" (click)="openDialog(cat)"> {{cat.name}} </button>
            <categories *ngIf="cat.children" [categories]="cat.children" (category)="onClickChild($event)"></categories>
          </li>
        </ul>
      </div>
</div>

Within the component, I have two functions (although I know that the transition effect does not work as intended in this setup):

  matchButtonColor(category: AdminCategory) {
    var s = {
      'background-color': 'white',
      'border-color': '#8064A2',
      'color': '#8064A2',
      'transition': 'color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out'
    }
    return s;
  }

  transition(){
    console.log("aasdasd");
  }

Currently, I have achieved this setup. My next aim is to implement the transition effect on hover. Is there a way to make this happen? Simply adding it to a .css file won't suffice because the colors are generated dynamically based on the value of cat.

https://i.sstatic.net/cbmUc.png

Answer №1

To achieve this, you can set the hoverFlag to true when the user hovers over an element and then set it to false when they move their mouse away. You can then have two methods, one for the hover state and another for the normal state (or just one method where you pass a flag based on your needs).

<button [ngStyle]="hoverFlag ? matchHoverButtonColor(cat) : matchButtonColor(cat)" (mouseover)="hoverFlag = true" (mouseout)="hoverFlag = false" class="btn " type="button" (click)="openDialog(cat)"> {{cat.name}} </button>

Therefore, if hoverFlag is true, the matchHoverButtonColor(cat) method will be called. Otherwise, the matchButtonColor(cat) method will be called.

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

Tips on activating two HTML buttons simultaneously

I tried to implement clickable tabs by following a tutorial. I created both horizontal and vertical tabs. However, I want to have both buttons active at the same time. For example, if any of the horizontal buttons is active (number less than 6), then the f ...

retrieve the list of css stylesheets connected to a webpage

Currently, I am running some tests on Firefox's Error Console. My main focus is on identifying and rectifying errors in the stylesheets. For regression testing purposes, I need to compile a list of all HTML pages that are connected to these stylesheet ...

What is the best way to center this div vertically?

My code is structured like this: http://jsfiddle.net/LtzhN/ I want to center the blue button vertically within the container, but the container does not have a fixed height. Is there a way to achieve this using only CSS? I am aware that I could use jQue ...

What's the best way to add two distinct colors as background for a header with css?

h2 {background-color:#C61236; color:#FFFFFF; font-size:medium; line-height:1.5; text-indent:20px;} Although the current setup looks good, I am looking to incorporate a new color block at the start of the text. I'm not sure what steps to take nex ...

Dynamic emblem on a webpage

Recently, I stumbled upon this website. A unique feature caught my attention - when users click on the "Log in" button, the logo on the page starts animating. I'm intrigued by this animation and curious to learn how it can be implemented. Can anyone p ...

Using a variable with the :contains selector

function checkAndUpdateStatus(newName) { $('#myTable').children('tr').remove(":contains('" + newName + "')"); }; The function above is attempting to remove table rows in 'myTable' containing a string matching the ...

Guide to importing a scss file into a scss class

Is there a way to apply a different theme by adding the "dark-theme" class to the body? I've attempted the following implementation: @import '../../../../node_modules/angular-grids/styles/material.scss'; .app-dark { @import '../../. ...

Triggering a subscription by hovering over a specific element on the webpage

I am looking to implement a subscription and perform certain actions when hovering over specific elements within the DOM. For instance, take the example of home.component.html: <div class="container"> <div class=&q ...

resembling the nth-child selection for even elements within a list wrapped in <ul> tags

I recently utilized ng-repeat to iterate over a ul element. Unfortunately, I've run into an issue where I can't simply use nth-child(even) to change the background color of every other ul element. Does anyone have any alternative solutions or s ...

How to create the appearance of multiple line breaks using CSS

While pondering a question, I stumbled upon a humorous solution that wasn't quite finalized. Check out the Fiddle and attempt to remove the <br/> tag. The challenge is to achieve the same effect (red div displayed) without resorting to this ra ...

Bootstrap multiple Angular 9 modules to enable running several applications concurrently on a single page

I have a project where I need to run two applications on the same page. Here's what I'm trying to do: <body> <app-main></app-main> <app-secondary></app-secondary> </body> The goal is for each application ...

What is the best way to utilize the @Import CSS rule in my code?

After using CSS for a considerable amount of time, I have successfully utilized the @import rule in the past. However, I am currently encountering an issue with it on a new project. Is there anyone who can offer assistance? Here is the code: @import url(&a ...

Customizing the appearance of the dragged element in jQuery UI

Is there a way to modify the color of a draggable list item after it has been dragged? Any assistance is greatly appreciated. $("#DragWordList li").draggable({helper: 'clone'}); ...

Encountering [Object] error in Angular's ngbTypeahead functionality

Here is the code for my input field in component.html: <input type="text" class="form-control" [(ngModel)]="searchQuery" [ngbTypeahead]="recommends" name="searchQuery" typeaheadOptionField="user ...

What is the best way to design a bookmarklet that can overlay an HTML/div layer and apply CSS styles from an

Is there a way to incorporate a bookmarklet that can bring in a new layer/div with additional HTML and CSS from an external file, overlaying it on the current page? If anyone has an example of a bookmarklet that achieves this, would you be willing to shar ...

Text positioned in the center alongside an image, or if there is limited space, it will be aligned to

Take a look at this code snippet: img { float: left; } #inner { height: 128px; background-color: yellowgreen; display: table-cell; vertical-align: middle; } #content { background-color: red; } <img src="http://cdn.memegener ...

My Angular frontend is having trouble communicating with my backend controller API route. Can anyone help me figure out what I'm doing wrong?

An error has been encountered in zone.js:3243 while trying to POST https://localhost:44424/api/SlgCorpNotes/Edit (405 Method Not Allowed). Below is the API call made by my service: updateMessage(message: any) { console.log("at service") console ...

Utilizing Bootstrap4 to achieve optimal layout of form components

In my current project, I am developing a form using Bootstrap4 with specific design requirements. Despite researching extensively on Bootstrap4 documentation and implementing various strategies like using tags, I have not achieved the desired outcome. Be ...

Utilizing Material UI with Appbar logo aligned to the left and Tabs featured at the center

I am currently working on creating a react material ui AppBar. The design includes a logo and tabs, with the tabs supposed to be centered in the AppBar and the logo positioned on the left side. However, I have been facing difficulty in making the logo al ...

Creating a menu header similar to Gmail's design is a great way to

How can I create a fixed menu similar to the one in Gmail? I've attempted using CSS, but the div remains centered instead of sliding up on scroll like the Gmail menu. View in full-size image I've experimented with CSS properties, here's an ...