Implementing the rotation of an element using 'Transform: rotate' upon loading a webpage with the help of Jquery, Javascript, and

A div element designed to showcase a speedometer;

 <div class="outer">
   <div class="needle" ></div>
 </div>

The speedometer animates smoothly on hover;

.outer:hover .needle {
  transform: rotate(313deg); 
  transform-origin: center center;
}

To make the animation occur on page load, I attempted changing the class name from .outer:hover .needle to .animateNow and used jQuery to apply this to .circle;

$(document).ready(function() {
  $('.outer').addClass('animateNow');
});

However, this method did not work as expected. Any suggestions?

Here is the complete CSS code for reference;

@charset "utf-8";
/* CSS Document */

body {
    background-color: black;
}

.outer {
  position: relative;
  /*display: inline-block;*/
    height:100vw;
    width: 100%;
  background-image: url(../CentreDial3.png);
    background-size: cover;
  border-radius: 50%;
}
.needle {
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
  border-radius: 50%;
  transition: all 3s ease-in;

}
.needle:before {

  left: calc(50% - 2.5px);
  content:"";
  position: absolute;
  top: 50%;
  left: calc(50% - 2.5px);
  height: 45%;
  width: 5px;
background: #b30000;
    border-radius: 40%;

}
.needle:after {
  content: "";
  position: absolute;
  top: calc(100% + 5px);
  left: 50%;
  height: 10px;
  width: 10px;
  transform: rotate(-135deg);
  transform-origin: top left;
  border-top: 0px solid white;
  border-left: 0px solid black;


}


.outer:hover .needle,
 .outer.animateNow .needle{
  transform: rotate(313deg); 
  transform-origin: center center;
}

Answer №1

Before we proceed with calling the addClass() function in jQuery, it's important to remember that you should exclude the . selector. Your code should look like this:

$('.circle').addClass('animateNow');

To ensure that the animation takes effect when the class is added, you need to adjust the CSS to apply the effect on both hover and when the .circle element has the specified class.

It's worth noting that you should add the class either during window.load or after a short delay using setTimeout(), otherwise, the final position will be immediately displayed. Here is an example:

$(window).load(function() {
  $('.outer').addClass('animateNow');
});
@charset "utf-8";

/* Customize your CSS here */

body {
  background-color: black;
}

.outer {
  position: relative;
  /*display: inline-block;*/
  height: 100vw;
  width: 100%;
  background-image: url(../CentreDial3.png);
  background-size: cover;
  border-radius: 50%;
}

.needle {
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
  border-radius: 50%;
  transition: all 3s ease-in;
}

.needle:before {
  left: calc(50% - 2.5px);
  content: "";
  position: absolute;
  top: 50%;
  left: calc(50% - 2.5px);
  height: 45%;
  width: 5px;
  background: #b30000;
  border-radius: 40%;
}

.needle:after {
  content: "";
  position: absolute;
  top: calc(100% + 5px);
  left: 50%;
  height: 10px;
  width: 10px;
  transform: rotate(-135deg);
  transform-origin: top left;
  border-top: 0px solid white;
  border-left: 0px solid black;
}

.outer:hover .needle,
.outer.animateNow .needle {
  transform: rotate(313deg);
  transform-origin: center center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="outer">
  <div class="needle"></div>
</div>

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

Passing PHP values to JavaScript and then to AJAX requires using the appropriate syntax and techniques for

Currently, I am populating a table with data retrieved from my database. Here is a snippet of the code: <?php //mysqli_num_rows function while($row=mysqli_fetch_array //I know this may be wrong, but that's not the point echo "<tr><t ...

Revamping status and backend systems

Seeking advice on the most effective method to execute a HTTP PUT request within my react application. A Post component is responsible for fetching data from https://jsonplaceholder.typicode.com/posts/1 and displaying it. There's another component na ...

Mobile webpage utilizing jQuery live click event binding

I am currently in the process of creating a mobile-friendly website that includes a list of names. I have successfully implemented a click event using jQuery for all devices, including Android, Blackberry, and Nokia. However, when testing on an iPhone, I ...

What is the best way to incorporate the parallax effect into a v-carousel?

Currently, I have a "v-carousel" containing multiple images and now I am looking to incorporate a parallax effect into it, similar to "v-parallax". <v-carousel cycle height="600" hide-delimiter-background show-arrows-on-hover> <v-carousel-i ...

How can I resolve the "web page not found" error when using jQuery .replace()?

Working on HTML and javascript/jquery scripts, I have encountered a peculiar issue. My script utilizes a for-in loop to iterate through a JavaScript object, substituting specific patterns in HTML-formatted lines with data from the object using jQuery .appe ...

Positioning two distinct Div elements using CSS

In an attempt to align two lists within a div, I have one list containing .jpg files and another with text files. <!DOCTYPE html> <html> <style> .left{height:auto; float:left;} .right{height:auto; float:right;} </style> &l ...

Determine the position of an element in relation to a specific ancestor using JavaScript

Let's consider the following example of HTML code: <div id="site_header_container" class="site_bar_container"> <div id="site_header"> <div id="header_logo_container"> <a class="sliced" id="header_ ...

dynamically open ngx-bootstrap accordion panels

I implemented the ngx-bootstrap accordion feature to display a list of blog posts. Below is the template structure: <accordion id="blog-list"> <accordion-group *ngFor="let post of posts; let first = first;" [isOpen]="first" id="post-{{post.i ...

When the if-else statement is executed, it showcases two strings:

Learning Experience: Unveiling My Lack of Cleverness Update: It appears that utilizing PHP in my current setup is not possible. As a result, I'll take some time to contemplate while banging my head against a wall. Despite that, thank you all for your ...

Explore vue3 components using vue-test-library and universal components

I started creating unit tests for a production app using jest, @testing-library/vue, and supporting libraries. The first test I created is as follows: import vue from "vue"; import { render } from "@testing-library/vue"; import LibBtn f ...

What is the best way to eliminate duplicate values from an Array in ReactJS?

Hi there, I'm new to JavaScript and React. I need some help with a project I found on the React blog. I want to try solving it in my own way. This is the content of todoList.js: const todoList = [ {category: 'Sporting Goods', price: &a ...

Scrolling with React Event

I am attempting to create a scrollbar that only appears when I scroll within a particular area using React. I am utilizing debounce and useState in my implementation. The issue: When I reach the end of the scroll, the event continues to repeat indefinitel ...

Autocomplete suggestions tailored to specific categories in real-time

Having trouble creating an autocomplete feature with dynamic values based on a combobox using CodeIgniter. I've attempted using AJAX without success. Below is my AJAX code for calling items in a category: <script type="text/javascript"> $(docu ...

Understanding the document.domain feature in Javascript

I have a website called foo.com hosted on server bar. Within this website, I have created a subdomain called api.foo.com. To connect the subdomain with Google Apps, I have set up a CNAME entry pointing to ghs.google.com. Now, I am facing an issue when att ...

Is it possible for me to display dynamic content and utilize a template engine like (ejs)?

Currently, I am in the process of developing a weather application to enhance my skills with express and ejs. At the moment, I'm utilizing app.get to fetch data from darkSky's API successfully, and I can display it on my index.ejs page. My next ...

Ensuring the existence of a MySQL database prior to executing a Node.js application

I am currently working on a Node.js/Express application that communicates with a MySQL server using Sequelize. I want to make sure that a particular database is created before the app starts running when using npm start. I think I need to create a one-ti ...

Cannot get v-if to work with multiple conditions

<template> <div> <h2>{{weatherData.city}}</h2> <h3>{{weatherData.weather}}</h3> <rain-cloud v-if="iconSelect==='09d'"/> <sun-cloud v-if="iconSelect==='04d'"/> <sunsh ...

initial render results in undefined data

function Story() { let { id } = useParams(); const pin = useSelector(state => state.pins.pin); const dispatch = useDispatch(); const userid = 2 useEffect(() => { dispatch(getPin(id)); }, [dispatch, id]); return ( <div classN ...

What is the best method to insert multiple rows of the same height into a table cell in

My datatable is causing me trouble as I try to add more rows in the td after the Name column. The rows are not aligning properly, and I need a solution. I want these new rows to be aligned with each other, but the number of rows may vary based on user inp ...

Is it feasible to add to an ID using ngx-bootstrap's dropdown feature?

In the documentation for ngx dropdown, there is a feature called "append to body." I recently tried changing this to append to a table element instead and it worked successfully. Now, on another page, I have two tables displayed. If I were to assign each ...