Click-triggered Animation of Glyphicon

I'm attempting to create an animation for a glyphicon-refresh icon that rotates when clicked. I've been trying to achieve this effect using CSS3, but haven't had much success.

Below is the code I've used:

<a id="update" href="#"><i class="glyphicon glyphicon-refresh"></i></a>

Here is the CSS I've written:

<style>

.glyphicon-refresh-animate {
  animation-name: rotateThis;
  animation-duration: .5s;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}

@keyframes "rotateThis" {
 from { transform: scale( 1 ) rotate( 0deg );   }
 to   { transform: scale( 1 ) rotate( 360deg ); }
}

</style>

This is the JavaScript implementation:

<script type="text/javascript">
    $( document ).ready( function() {
        $( "#update" ).on( "click", function( e ) {
            var $icon = $( this ).find( ".glyphicon glyphicon-refresh" ),
            animateClass = "glyphicon-refresh-animate";

            $icon.addClass( animateClass );
            // setTimeout is to indicate some async operation
            window.setTimeout( function() {
                $icon.removeClass( animateClass );
            }, 2000 );
        });    
    });
</script>

If anyone has suggestions or an alternative method to achieve this animation effect, it would be greatly appreciated.

Answer №1

First: Your jQuery selector is incorrect:

.glyphicon glyphicon-refresh

should be written as

.glyphicon.glyphicon-refresh

Second: It is important to include vendor prefixes for all animate properties, the keyframes keyword and the transform property. (Except Firefox and IE10+ which support them without prefix. http://caniuse.com/#feat=css-animation) For Safari and Chrome, it should look like this:

.glyphicon-refresh-animate {
   -webkit-animation-name: rotateThis;
   -webkit-animation-duration: 2s;
   -webkit-animation-iteration-count: infinite;
   -webkit-animation-timing-function: linear;
}

@-webkit-keyframes "rotateThis" {
 from { 
        -webkit-transform: rotate( 0deg );  
    }
 to  { 
        -webkit-transform: rotate( 360deg ); 
    }
}

http://jsfiddle.net/DX4AJ/ This example only functions correctly in Safari and Chrome!

Answer №2

There is a built-in solution using the Font Awesome library, by simply utilizing the fa-spin class.

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 for updating the background color of a specific row

I have a piece of code that I am trying to modify with a specific condition. If {row.BB} is less than or equal to 100, I want to change the background color of the row with this value to red. Can someone help me achieve this? The code is linked to a data ...

The unique capabilities of services and factories in Angular 1 - understanding their differences and what each excels at

After extensively combing through numerous stackoverflow posts and articles, the consensus seems to be that an angular service returns an instance, while an angular factory returns any desired object. This raises the question: what unique capabilities do ...

Adjusting the hue of a specific element in HTML5/CSS

Is there a way to change the background color of individual items in a list without affecting the entire list? I specifically want the background of items in one list to be green, and items in another list to be teal. How can I achieve this? This is the c ...

Having trouble with the functionality of the cascading menu?

I am having trouble with a drop-down menu. The first level works fine, but I can't seem to get the second level of the menu to display. Appreciate any help you can offer. Thank you. javascript <script type="text/javascript"> $(document).ready( ...

retrieving elements from an array object results in an undefined value

Utilizing an API to retrieve holidays and their corresponding dates, I extracted values from a JSON file and organized them into an array of arrays. function getHolidays(){ (...) var ulHoliday = new Array(); var txt = JSON.parse(this.responseText); const h ...

Utilizing a modular perspective in JavaScript to load JSON files as a view

I've been working on implementing a modular view approach for retrieving data from a JSON file in my JavaScript code, but I'm facing some issues. The snippet of code where I am trying to load the JSON file is contained within a separate JS file a ...

incorrect application of margin and padding

Check out my HTML & CSS code. I'm facing an issue with the top padding, which should be 20px but is currently set at 10px. Here's a screenshot showing the problem. I've attempted to adjust margins and padding on different elements, but noth ...

Tips for setting up a typeorm entity with attention to its nullable fields

How can I assign values to typeorm entities and insert them into the database? import { PricingPatternElement } from file const Element:PricingPatternElement = { displayOrder: 10, elementName: "test", createdAt : getCurrentDate(), createdBy: &qu ...

Maximizing code reusability in Javascript and React: A guide to avoiding repetition

While creating a back button feature for query processing, I found myself constantly using if statements to handle additional queries in the URL. Unfortunately, the '(([query, value]' format is necessary and requires an extra if statement each ti ...

Using jQuery UI autocomplete with special characters

Recently, I put together a simple jQuery example utilizing jQuery UI autocomplete feature. $(function() { //autocomplete $(".selector").autocomplete({ source: "getdata.php", minLength: 1 }); }) getdata.php: <?php if (isse ...

Widget remains static until job triggers data refresh, preventing CSS transition initialization

I am struggling with the html/coffee/scss aspects, although I'm comfortable with Ruby. On my website, I have implemented a hotlist widget sourced from this link: https://gist.github.com/andre-morassut/8705385. While it functions properly, I encounter ...

Organize your file dependencies efficiently using NPM

I'm currently part of a medium-sized team working on a large front-end application. Up until now, we have been using requirejs and AMD modules to manage our project with approximately 500 files. However, we recently decided to transition to commonjs a ...

Converting JSON data into an array of JavaScript objects

Here is some of the JSON data I have: [ { "items": [ { "retailername": "Zop Now", "value": 475 }, { "retailername": "Snap Deal", "value": 265 ...

Is there a simple way to display all the data from a JSON object even if the contents are unknown beforehand?

Greetings! I am Pearson's dictionary api. Here is a glimpse of what I receive from an api call: { "status": 200, "offset": 0, "limit": 10, "count": 10, "total": 135, "url": "/v2/dictionaries/entries?headword=dog", "results": [ { ...

The Final Div Fluttering in the Midst of a jQuery Hover Effect

Check out my code snippet here: http://jsfiddle.net/ZspZT/ The issue I'm facing is that the fourth div block in my example is flickering quite noticeably, especially when hovering over it. The problem also occurs occasionally with the other divs. Ap ...

Module 'xhr2' not located

Snippet of code : let XMLHttpRequest = require('xhr2'); let xhr = new XMLHttpRequest(); xhr.open('GET', 'data.json', true); xhr.send(); Issue : internal/modules/cjs/loader.js:969 throw err; ^ Error: Module 'xhr2' n ...

Altering the content of a span within a list item using AngularJS

Recently, I encountered some HTML code that looks like this... <ul id="sidemenu" class="wraplist wrapper-menu"> <li class="auto" ng-class='{"active": active == 1 }' ng-click="makeActive(1)"> <span class="arrow material-icons" ...

How can I avoid transpiling in TypeScript when setting ESNext as the target in tsconfig.json?

I came across a similar question that I thought would be helpful: How to maintain ES6 syntax when transpiling with Typescript, but unfortunately, it didn't solve my issue... It's slightly different. When running yarn tsc --project tsconfig.json ...

How to incorporate a background image using CSS in Ruby on Rails 2

I created a scaffold using ruby and now I'm trying to add a background image to all pages of the website. However, I am uncertain about what the correct image link should be. The image is located in app/assets/images/"dep.jpg" This is what I have at ...

The footer fails to adhere to the bottom of the page

Struggling to keep my footer at the bottom of the page when there is minimal content. I attempted using: position:absolute; and bottom:0; It appears to stay in place, but when more content is added, it ends up going "under" the footer that remains n ...