`Is it possible to animate the rotation of an image within an input control?`

After coming across this helpful answer, I successfully added a rotating GIF image as an icon inside an INPUT control on its right side. Simply applying the "busy" class to my control did the trick, and now it looks like it's in progress.

input.busy {
  background-image: url(...);
  ...
}

But now, I want to adjust the speed of the rotation by animating the image itself. I found a solution in this blog post, which works for most elements except for INPUT, unfortunately.

div::after {
  content: "this shows";
  background-image: url(...);
}

input::after {
  content: "this shows not";
  background-image: url(...);
}

So, the question remains: how can I animate the rotation of a PNG image placed at the right edge of an INPUT tag?

Answer №1

To create a loading animation, I recommend using a container with an image and an input element inside. Position the image on top of the input and apply a CSS class of .loading to the container when you want to display the animated image.

A GIF is not necessary for this effect.

If you plan on using this layout in multiple locations, consider creating a custom element for it.

.input-container {
  display: inline-grid;
  place-items: center end;
}

.input-container > * {
  grid-row: 1;
  grid-column: 1;
}

.input-container > img {
  display: none;
  border-radius: 50%;
  animation: spin 4s linear infinite;
}

.input-container.loading > img {
  display: inline-block;
}


@keyframes spin { 
  100% { 
    transform: rotate(360deg); 
  } 
}
<div class="loading input-container">
  <input />
  <img src="https://picsum.photos/id/200/20">
</div>

Answer №2

To create animations in After Effects, you can simply follow these steps:

.loading::after{
  content:'';
  display:inline-block;
  width:50px;
  height:50px;
  background-image:url('https://picsum.photos/id/63/50/50');
 }
 .loading.rotate::after{
   animation: rotation 2s infinite linear;
 }
 @keyframes rotation {
   from {
     transform: rotate(0deg);
   }
   to {
     transform: rotate(359deg);
   }
}

You can use the following code snippet:

<div (click)="toogle=!toogle" class="loading" [class.rotate]="toogle">
  here
</div>

However, issues may arise when using an input field. To address this, a custom directive can be created as shown below:

@Directive({
  selector: '[pending]',
  standalone: true,
  exportAs: 'child',
})
export class InputPending implements OnInit {
  div = this.render.createElement('div');
  @Input('pending') set _(value: boolean) {
    if (value) {
      this.render.insertBefore(
        this.elementRef.nativeElement.parentElement,
        this.div,
        this.render.nextSibling(this.elementRef.nativeElement)
      );
    } else
      this.render.removeChild(
        this.elementRef.nativeElement.parentElement,
        this.div
      );
  }
  constructor(private elementRef: ElementRef, private render: Renderer2) {}
  ngOnInit() {
    this.render.setAttribute(this.div, 'class', 'pendding');
    this.render.setStyle(
      this.div,
      'width',
      this.elementRef.nativeElement.offsetHeight + 'px'
    );
    this.render.setStyle(
      this.div,
      'height',
      this.elementRef.nativeElement.offsetHeight + 'px'
    );
  }
}

To use the directive with an input field:

<input [pending]="toogle">

The corresponding CSS styling would be:

input ~ .pendding{
  background-image: url("https://picsum.photos/id/237/50/50");
  display:inline-block;
  margin-bottom:-6px;
  margin-left:2px;
  animation: rotation 2s linear infinite
 }
 @keyframes rotation {
  from {
   transform: rotate(0deg);
  }
  to {
   transform: rotate(359deg);
  }
}

View the live example on StackBlitz

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

Creating an inverted curve or border-radius for a div element is a design technique that can add

I've been tackling a project that requires me to style the border of a div in an inverted manner. To achieve this, I'm limited to using only CSS and JS without any plugins. I've searched through various online resources and similar questions ...

Graphic selectors: a new take on radio buttons

I've been attempting to make this work, but it's not functioning correctly. Below is the CSS code: .input_hidden { position: absolute; left: -9999px; } .selected { background-color: #000000; } #carte label { display: inline-bl ...

Testing the Window beforeunload event method in an Angular unit test: A guide

Currently, I am actively involved in a project using Angular 8. One of the features I implemented was to show a confirm prompt when a user tries to navigate away from the site. This functionality was added by integrating the following function into my com ...

Align an image in the center vertically within a left-floated div with a minimum height

I am currently attempting to vertically align an image within a div that is positioned to the left, and has a minimum height of 150 pixels. The issue I am facing is that none of my attempts seem to be successful. I have tried various methods, including ut ...

The boundary extends fully to the right

Recently, I created a calculator to convert kilograms to pounds and encountered an issue when trying to add a border to a p element. The problem was that the border extended all the way to the right side of the page. Here is the corresponding HTML code: &l ...

Angular - Acquire reference to the <audio> element

Is there a way to access the methods of an audio tag within my component in order to implement play and pause functions based on click events? The current method I tried does not allow me to access the play() function. How can I correctly approach this? ...

Issues with loading Angular 4 Bootstrap JS post compiling

Utilizing normal bootstrap 3 within an Angular 4 application has been working well when compiling for development using ng serve. However, upon building the application with ng build, I encounter an issue where my bootstrap.js fails to load. Below are th ...

Steps for designing a single-column content-focused layout

Creating a single column fixed-width layout with a header and footer, the order of view should be as follows: header navigation dynamic content 1 dynamic content 2 main content footer The dynamic contents (3, 4, 5) will expand vertically with unknown he ...

MaxwidthLG in Material UI design

Hi there, I've encountered an issue with Material UI CSS. Even after attempting to override it, the desired effect is still not achieved. My goal is for the entire div to occupy the full page but this is the current result: https://i.stack.imgur.com/b ...

What is the process for defining a series of tests for karma through code?

Currently, I am in the process of developing an Angular Test Explorer. The exciting part is that I can view all the tests and execute them collectively by leveraging the karma module as shown in this code snippet: public async runWithModule(): Promise&l ...

What is the best way to customize the CSS of the Skype contact me button?

I am struggling with customizing the Skype contact me button. The standard Skype button has a margin of 24px and vertical-align set to -30px. I have tried removing these styles but have had no success. Here is the code snippet I am working with: Skype ...

Having trouble loading AngularJS 2 router

I'm encountering an issue with my Angular 2 project. Directory : - project - dev - api - res - config - script - js - components - blog.components.js ...

State in Angular stubbornly refuses to switch despite condition changes

Here is the Typescript code, followed by the HTML: public verifySelection() { let choice = false; if (typeof this.formUser.permissionsTemplateID === undefined) { choice = true; } return choice; } <div class="form-group" ...

Implementing a boolean value in PrimeNG's p-dropdown version 7.x

I need a p-dropdown with two options to be selected based on the boolean value of the control. The control (NOTIF_ALL) is assigned a boolean value oldPendingTasksOptions=[ {"oldPendingTaskId": false, "oldPendingTasksName": "Not a ...

Challenges arise when utilizing CSS3 animations in conjunction with transitions triggered by toggling a JavaScript class

Struggling to activate an animation while updating a class using JavaScript for a PhoneGap app. Planning on utilizing -webkit- prefixes for compatibility. However, the animations are currently unresponsive in Chrome during testing, both when applied to th ...

What is the best way to implement this design using CSS flexbox?

I'm working on designing a simple layout that looks like this: https://i.stack.imgur.com/oCzyV.png Currently, I have the following HTML structure with some variations in class names and additional markup within each element: <div class="site ...

Equalizing the space between table headings and content

I am struggling with aligning the heading and entries in my table properly. https://i.stack.imgur.com/IYtY1.jpg My goal is to ensure that the heading and entries are perfectly aligned with each other. This is how my Table.jsx looks like: <div classNa ...

Angular's detectChanges function does not trigger the ngDoCheck method

When I run cdr.detectChanges() in a nested child component (Child1) which has a parent and another nested child component (Child2), only ngDoCheck is invoked in Child2. Why doesn't it invoke DoCheck in the current component (Child1) as well? How can I ...

Utilizing FullCalendar to fetch information via HTTP

Looking to incorporate FullCalendar with the capability to fetch data via HTTP and open a dialog box when a date is selected. Is there a recommended library for this specific setup? Alternatively, does anyone have a pre-existing example I could reference ...

"Alert box displaying error message is not appearing on the screen

In order to display an error message using a tooltip, I have hidden the tooltip by setting the style of a span with an id of demo to display:none. Then I use JavaScript's getElementById method to retrieve the value of the input field with an id of use ...