What are some ways to achieve a smooth rotation of an icon?

Does anyone know why I am unable to smoothly rotate the icon? Any help would be greatly appreciated!

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
  <head>
    <script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>
    <script src="https://code.jquery.com/jquery-3.1.1.js"></script>

    <!-- style -->
    <style media="screen">
      div{
          background:red;
          width:20px;
          height:60px;
      }
      .rB {
        transform: rotate(180deg);
        transition: all 2s linear;
      }
    </style>
  </head>
  <body>

    <button id="button" type="button" name="button">Rotate</button>
    <i class="fab fa-twitter"></i>
    <div></div>

    <!-- script -->
    <script>
      $(function(){
        $("#button").on('click', function(){
          $(this).siblings().toggleClass('rB');
          });
      });
    </script>
  </body>
</html>

I have inserted a new div https://codepen.io/SalahGfx/pen/yvJqVO

Answer №1

To achieve this effect, consider utilizing CSS animations

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
  <head>
    <script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>
    <script src="https://code.jquery.com/jquery-3.1.1.js"></script>

    <!-- style -->
    <style media="screen">
  
      .rB {
        color: #03a9f4;
        animation-name: spin;
        animation-duration: 5000ms;
        animation-iteration-count: infinite;
        animation-timing-function: linear; 
      }
      @keyframes spin {
    from {
        transform:rotate(0deg);
    }
    to {
        transform:rotate(360deg);
    }
}
    </style>
  </head>
  <body>

    <button id="button" type="button" name="button">Rotate</button>
    <i class="fab fa-twitter"></i>

    <!-- script -->
    <script>
      $(function(){
        $("#button").on('click', function(){
          $(this).siblings().toggleClass('rB');
          });
      });
    </script>
  </body>
</html>

Ensure not to forget the <p> tag specified in their documentation: https://i.sstatic.net/mfveB.png

If you enclose the <p> tag within another div, it will display a similar behavior as your existing div. However, note that it may result in the icon rotating around the center in an unattractive manner due to the larger size of the enclosing div:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
  <head>
    <script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>
    <script src="https://code.jquery.com/jquery-3.1.1.js"></script>

    <!-- style -->
    <style media="screen">
      div{
          width:4px;
          height:4px;
      }
      .rB {
        transform: rotate(360deg);
        transition: all 2s linear;
      }
    </style>
  </head>
  <body>

    <button id="button" type="button" name="button">Rotate</button>
    <div class="tester"><i class="fab fa-twitter"></i><div>
    <!-- script -->
    <script>
      $(function(){
        $("#button").on('click', function(){
          $(this).siblings().toggleClass('rB');
          });
      });
    </script>
  </body>
</html>

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

Encountering the error of receiving "$ undefined," despite making a reference to the library

I have included the JQuery library in the following way: <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" type="text/javascript"></script> Despite trying to download the library and include it locally in my project, I ...

Comparing jquery ajax jsonp with angularjs $http json: A breakdown of two powerful ways

I have a scenario where I need to switch from using jquery to angularjs for a particular feature. Specifically, I am replacing jquery's ajax method with angularjs' $http method. This piece of code is responsible for enabling users to sign up for ...

How can I attach an event listener to an element that is added dynamically with jQuery?

I added a new div element dynamically using jQuery's on method. However, I'm having trouble getting a listener to work for the newly created element. HTML <input class="123" type="button" value="button" /> <input class="123" type="butt ...

Angular 1.5 - Component for fetching HTML content with dynamic data

Help needed with using Angular Component method. Developing a main html file with its main controller containing a JSON list of client data: clients: [{ "name": "John Jackson", "age": "21", "hair": "brown", }, { "name": "Janet Doe", ...

Using canvas in Bootstrap can lead to columns wrapping onto the next line due to margins

My goal is to align two charts side by side with some padding or margins between them. It seems to work fine when the columns contain text, but I'm running into issues when using the canvas tag. Whenever I try to add space between the charts, they end ...

Unexpected behavior: Dropdown sidebar in Rails with JQuery closes abruptly

As I work on integrating a bootstrap theme into my rails application (https://github.com/azouaoui-med/pro-sidebar-template), I'm encountering issues with the dropdowns inside the sidebar not functioning properly. It seems to be related to a JQuery pro ...

How can PHP send a variety of error messages to an Ajax/Jquery script and display them in an HTML DIV?

In an attempt to send various error messages back to an HTML DIV based on the PHP outcome, I have scoured through different resources without a solution that fits my particular case. Hopefully, someone can provide assistance with this. The scenario involv ...

Enhance your web form with the Bootstrap 4 tags input feature that allows users to add tags exclusively

I'm currently utilizing Bootstrap 4 along with Bootstrap Tags Input to create a multiple category selection feature. My goal is to enable users to choose multiple items exclusively from the predefined categories listed in the predefined_list. At pres ...

What is the process for opening a local HTML file in IE compatibility mode?

Having issues with IE compatibility mode while opening my HTML file on desktop. In IE8, unable to switch it to compatibility mode as the icon seems to disappear in local HTML documents. Any insights on this? LATEST UPDATE: Tried three solutions but still ...

Troubleshooting Google Web Fonts

I seem to be using code similar to what I used before, but it appears that the fonts are not loading. <html> <head> <script src="http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz:extralight,light,regular,bold"></s ...

Tips for validating a text input field depending on the selected value in a dropdown menu using AngularJS

When a user selects from the dropdown menu, they can choose between Number and Decimalnumber. If the user selects Number, the text box should only allow whole numbers (e.g. 22, 33, 444, 345436). The user should not be able to enter decimal values like 22 ...

Generate a JSON object specifically for the modified input fields upon submitting the form

Is there a way to generate a JSON object only with the input fields that have been modified before submitting the form? How can I capture and store each changed value in the form as JSON data? $(document).ready(function() { $('#save').clic ...

Retrieving data with jSON through the local storage API

Seeking assistance with a problem I'm facing. Being new to this, the textbook Headfirst into programming isn't very helpful in explaining. After researching on stackoverflows, I'm still struggling. Any guidance would be greatly appreciated. ...

The JSON/jquery call to the second div yields an undefined error

When I fetch a row of data from a MySQL server using PHP, I then encode it into a JSON array. After that, I try to display the information by using the provided PHP script. However, I encountered a strange issue where if I send "vname" to its own div, I ...

"Optimizing meta tags and adjusting text size for better

I recently made some changes to my website by adding a meta tag in order to make it responsive. <meta name="viewport" content="width=1400"> After testing it on an iPad, everything looked great - the text and graphics scaled properly. However, when ...

"Unexpected error: .jqm is not defined as a

Having an issue with a jqm function that I need help with. <span class="smaller gray">[ <span class="blueonly"><a href="javascript:void(0)" rel="nofollow" onclick="javascript:jQuery(\'#s ...

Repeated information in HTML tables

I am currently working with two HTML tables and two sets of JSON data. Initially, I load one table with the tableData which has a default quantity of 0. In my HTML form, there are three buttons - save, load draft, and edit. Upon clicking on load draft, I p ...

Exploring Bootstrap: the ins and outs of customizing styles

How can one determine which bootstrap styles need to be overridden when customizing the appearance? Is there a trick to identifying where to set styles in order for them to take precedence over bootstrap styles? For instance, I've been struggling fo ...

How to retrieve the width of an unspecified element using JavaScript

Seeking help with a div element that adjusts its size based on the elements it contains. How can I determine the final size of this dynamic div without checking the sizes of its internal elements? I've attempted to parse all the properties of the obj ...

Broken functionality: Ajax links malfunctioning

I decided to enhance my code by incorporating ajax. Specifically, I wanted to implement ajax on patients/index #patientenajax <h1>Listing patients</h1> <div id="patientenajax"><%= render "patienten" %></div> Then, in ...