`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

The colors of my SVG button remain constant and do not dynamically change when I hover over it

I am facing an issue with a button that contains an SVG element. In my CSS, I have defined styles to change the color of the icon and the SVG when hovered over. However, I noticed that I have to directly hover over the SVG itself for the color to fill. As ...

Aligning Content in the Middle of a Bootstrap Row

I'm struggling to horizontally center the content in the "row" class div. I've attempted using justify-content-center and other variations. <div class="container"> <div class="row"> <div class="col-md-4 portfolio-item ...

Click to enlarge font size across the entire project

I'm working on a project for elderly users and my client wants a feature where they can easily adjust the font size throughout the application with just one click of a button. What is the best approach for implementing this functionality? My initial ...

Applying the active state to the link will cause it to inherit the default styles of the

I am facing a challenge with overriding the active style of links in a universal manner, to restore them to their original state when they cannot be interacted with (such as during scrolling). In my current code, I implemented this: .scrolling a:active { ...

Problem: When trying to access the property 'completed' of an object in Angular 2, an error is

My understanding is that by using @Input(), this component should be able to bind <experiment [experiment]="experiment.completed"></experiment>. Here is the hierarchy: https://i.stack.imgur.com/6UwHt.png experiment.detail.component.ts import ...

What is the best way to combine a navbar with a video background?

Recently, I delved into Bootstrap and attempted to execute a code for a navbar with a video background. This is what my attempt looked like: HTML Code: <html> <head> <title>Medical</title> <meta charset="UTF-8&qu ...

I'm looking to add CSS transitions, like fade-ins, to a list of items in React in a way that the animations occur one after the other upon rendering. How can this be achieved?

I've scoured the depths of Stack Overflow and combed through countless pages on the internet in search of an answer to my query, but alas, I have not found a solution. So, my apologies if this question is a duplicate. Here's my conundrum: https ...

How can the issue of the navbar color not being able to disappear or turn transparent be resolved, given its connection to the body color?

@import url('https://fonts.googleapis.com/css2?family=Bai+Jamjuree:wght@400;500;600;700&display=swap'); :root { --color-body: #b6cbce; --color-heading: #eef3db; --color-base: #033f47; --color-base2: #022a30; --color-brand ...

Unable to center div container with margin set to auto

I'm struggling to center my container on the website. My goal is to have it centered and take up about 80% of the page so I can incorporate an image slider and text inside. Using margin: 0 auto doesn't seem to be achieving what I want. The backgr ...

How can I display milliseconds from a date in Angular2+?

I recently encountered an issue while working with a custom pipe that was used to display time. I attempted to modify it so that it could also show milliseconds: {{log.LogDate|jsonDate|date:'dd.MM.yyyy &nbsp; HH:mm:ss.sss'}} Here is the cod ...

Create a rectangle on the canvas using the Fabric.js library in an Angular application

I am attempting to create a rectangle inside a canvas with a mouse click event, but I am encountering some issues. The canvas.on('selection:created') event is not firing as expected. Below is my code: let can = new fabric.Canvas('fabricCanv ...

Testing the functionality of a condition function using Jest can sometimes lead to errors, such as the following: "Matcher error: the received value must be

I recently started learning how to write unit tests with Jest, and I decided to test whether the modal opens after submitting a form. However, I encountered an error: Matcher error: received value must be a mock or spy function. TS buildform(){ this. ...

Tips for displaying and hiding content in Angular2

I have a component that toggles the visibility of elements by clicking a button. This is the snippet of my HTML code: <div *ngFor="let history of histories | sortdate: '-dateModified'"> <p><b>{{ history.remarks }}</b& ...

I'm interested in knowing how to switch between two functions using Angular

I have developed a grocery list application where each row contains the name of a food item. The layout includes a left column for items that are not needed and a right column for items that are needed. Currently, I am able to change the state by clicking ...

Seeking a method to attach information from a div to a hyperlink

As a complete novice in the world of CSS websites, I find myself struggling to accomplish what seems like a simple task. I am working with an automatically generated CSS/JavaScript website where on the "individual" page, a person's name is listed with ...

Tips for aligning a row with both text and input fields in the center

I'm working on a design that includes text and input fields in rows, and I want everything to be centered. Should I wrap all the content in a div and center it as a whole, or should I center each row individually? Any suggestions? Here is the code sn ...

I am interested in incorporating a vertical scrollbar into a gridview in ASP.NET

I've successfully bound data to a grid view in my ASP.NET application. I've implemented pagination, but instead of that, I'd like to incorporate a scroll bar to prevent lengthy paging. Below is the code for my grid view: <div class="box ...

Is it possible to utilize a separate, standalone stylesheet for media queries?

My understanding of media queries evolved when I discovered them yesterday during my research on creating mobile-optimized pages. I learned that a media query refers to a stylesheet that complements and supersedes the current one, which differed from what ...

Regain the original properties after implementing a universal CSS reset

Working with older code that I must build upon always reminds me of the drawbacks of using global CSS resets. I have this outdated foo.css file that begins with * {margin:0; padding:0;} In the past, I would duplicate it to a new file called bar.css, ma ...

Encountering the ObjectUnsubscribedErrorImpl while utilizing angular datatable with angular version 7

I am encountering an issue when trying to display a DataTable in a modal window. Initially, everything works as expected and I can see the screen perfectly. However, after closing the modal window and opening it again, the DataTable no longer shows the sea ...