Can a rotation animation be incorporated into an image in next.js when it is clicked?

Looking to enhance a next.js image with an animation? How about making it rotate 360 degrees upon each click? Despite my attempts at toggling classes, I can't seem to achieve the desired outcome. Any guidance would be greatly appreciated. Thank you in advance!

I've already experimented with this code snippet, but haven't been able to make it work as intended:

<Image
src="/images/user_rotate.png"
height={20}
width={20}
alt="user_rotate"    
onClick={rotate_user}       
id="rotate_user"         
/>

function rotate_user(){
    var rotation_symbol = document.getElementById("rotate_user");
    rotation_symbol.classList.toggle("rotated");
}

Here's a glimpse of my CSS-Module:

.rotated {
    animation: rotation 2s;
}

@keyframes rotation {
    0% {
        transform: rotate(32deg)
    }

    10% {
        transform: rotate(64deg)
    }

    20% {
        transform: rotate(96deg)
    }

    30% {
        transform: rotate(128deg)
    }

    40% {
        transform: rotate(160deg)
    }

    50% {
        transform: rotate(192deg)
    }

    60% {
        transform: rotate(224deg)
    }

    70% {
        transform: rotate(256deg)
    }

    80% {
        transform: rotate(288deg)
    }

    90% {
        transform: rotate(310deg)
    }

    100% {
        transform: rotate(360deg);
    }
}

Answer №1

When you use event.target, you are able to access the specific object that was clicked on. By implementing this with a CSS transition modifier, you can achieve a smooth rotation effect on elements.

<body>
    <main>
        <button id="button" style="transition: all 0.5s; background-color: green; width: 100px; height: 50px; border: none; border-radius: 5px; color: lightblue; font-size: 18px" onclick="spin()">click me</button>
    </main>

    <script type = "text/javascript">
        var rotation = 0
        function spin() {
            rotation += 360;
            event.target.style.transform = "rotate(" + rotation + "deg)";
        }
    </script>
</body>

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

Is there a more efficient method for translating arrays between JavaScript and PHP?

Currently, I am in the process of developing a web page that has the capability to read, write, and modify data stored in a MySQL database. My approach involves utilizing PHP with CodeIgniter for handling queries while using JavaScript to dynamically updat ...

Tips for safeguarding AJAX or javascript-based web applications

Utilizing AJAX, this function retrieves information about an image in the database with the ID of 219 when a button is clicked. Any visitor to this webpage has the ability to alter the JavaScript code by inspecting the source code. By modifying the code a ...

Issue: the size of the requested entity is too large

Encountering an error message while using express: Error: request entity too large at module.exports (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/node_modules/raw-body/index.js:16:15) at json (/Users/ ...

Error during deployment on Vercel: Module 'build-rss.js' not found in workpath0 directory

One of my package.json scripts looks like this: { "export": "next export", "build": "next build && npm run export && npm run build:rss", "build:rss": "node ./.next/server/scripts/bui ...

Steps to load a script when the document is ready:

What is the best method to include JavaScript using the following code: <script type="text/javascript" src="http://uads.ir/l.php?s=125125&w=5307cd5c027373e1773c9869"></script> only after the page has fully loaded? $(document).ready(funct ...

Sending Email Form in PHP using JQuery and Ajax for seamless user experience

Looking to create an email form in HTML with PHP, but running into the issue of the page reloading after submitting? Many people turn to jQuery and AJAX to solve this problem. While you may have come across solutions on Stack Overflow, as a non-native Engl ...

CSS Styling Dropdown Menu for Internet Explorer 5 and Internet Explorer 11

I am encountering an issue with a select box that is coded as follows: <html:select property="list_data" size="12" style="width: 350px;" ondblclick="JavaScript:doOK()"> <html:optionsCollection property="list_data" label="codeAndNameLabel" val ...

Top method for organizing and filtering tables in Laravel on the client side?

In my Laravel web application, I have multiple tables with MySQL data stored. I want to provide users with the ability to sort and filter data on any column header dynamically, all processed on the client side. Although Laravel does not come with this feat ...

Issues with Autofocus in AngularJs on Firefox

Check out this straightforward directive to set autofocus: app.directive('autoFocus', function($timeout) { return { restrict: 'AC', link: function(_scope, _element) { $timeout(function(){ ...

Tips for creating a stylish scrollbar in a React Material-Table interface

Currently, I am utilizing the react material-table and looking to implement a more visually appealing scroll-bar instead of the default Pagination option. Although I have experimented with the react-custom-scroll package, it has not produced the desired ...

What is the process for transforming a nested dictionary in JSON into a nested array in AngularJS?

I am looking to create a form that can extract field values from existing JSON data. The JSON I have is nested with dictionary structures, but I would like to convert them into arrays. Is there a way to write a recursive function that can retrieve the key ...

JavaScript combined with a dynamic menu, a customized current link style using CSS, and a site built on PHP

Here's my current website setup: My website is modular and uses dynamic inclusion. The header is a crucial part of the main page, which is responsible for displaying content from specific links on the site. External links to CSS and js files are incl ...

Guide on developing a personalized validation system with Vuetify regulations for verifying the presence of an item

I'm currently working on my first CRUD web app using Vue 2 + Vuetify, but I've hit a roadblock while trying to add validation to a form. Specifically, I need to ensure that no item with the same title already exists in the database. You can view ...

Learn the effective way to customize the primary button text color in Bootstrap 4 using SCSS styling

Looking to customize the .btn-primary text color. I attempted to modify the background color in _variables.scss by adding the following line: $primary: #a1c1b6; However, I was unable to change the text color despite trying various options. // not working ...

Adapting npm scripts with Node.js based on the current context

Can you set up package.json to execute a different npm start script depending on the context? For instance, I want to run DEBUG=http nodemon app.js during development. However, I prefer to run node app.js in production. ...

Leveraging Next.js to interact with secondary backend services while passing a JWT token through server actions

I'm currently facing an issue where I am working with Nextjs and NextAuth, and I need to access another backend by setting the Bearer token in the Authorization header. My concern is not exposing the token to the client, so I aim to retrieve it in a s ...

Is there a way to customize the pagination dots in react-native-swiper-flatlist?

Is it possible to customize the pagination dots style for react-native-swiper-flatlist? <View style={styles.container}> <SwiperFlatList autoplay={false} autoplayLoop={false} index={0} showPagination ...

Developing a trivia game using HTML and JavaScript

I'm in need of some serious assistance with creating a quiz using HTML. My goal is to have a web page where users can visit, take a quiz, and have their responses stored. Unfortunately, I don't have the knowledge or skills required to do this on ...

After logging in successfully, the React app needs a hard refresh to update the

I'm encountering a debugging challenge and would appreciate any assistance from the community. I've been working on my first React app and successfully implemented a Login feature, but after a user logs in, they have to hard refresh their browser ...

Employing the html.validationmessagefor to display a client-side error notification

My text fields have server-side validation messages. The field 'title' is a required field on the server side with a '[Required]' data attribute in the class, but it only seems to be checked on a postback or form submit. I am saving the ...