Guide to incorporating Circular Animation within a Div Container

Currently, I am making changes to this jsfiddle. However, the issue I'm facing is that I can't get it to circle around in a specific direction.

  .dice-wrapper {
    position: absolute;
    top: 209px;
    right: -9px;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-flex-wrap: wrap;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    -webkit-transform: translate3d(-50%, -50%, 0);
    transform: translate3d(-50%, -50%, 0);
    font-family: 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif;
}

@keyframes myOrbit {
    from { transform: rotate(0deg) translateX(150px) rotate(0deg); }
    to   { transform: rotate(360deg) translateX(150px) rotate(-300deg); }
}

This illustration demonstrates the desired movement: https://i.sstatic.net/I1OIc.png

To clarify, the animation should only occur when the user clicks the button, not continuously.

Answer №1

WHEN ANIMATION SHOULD CEASE AFTER BEING RELEASED

To ensure the animation begins in an active state, it should be initiated during interaction. Your current code activates it upon page load, preventing activation via another button press.

To rectify this issue, consider incorporating the following adjustments:

.dice-wrapper {
    position: absolute;
    /* top: 50%; */
    top: 209px;
    right: -9px;
    /* left: 50%; */
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-flex-wrap: wrap;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    -webkit-transform: translate3d(-50%, -50%, 0);
    transform: translate3d(-50%, -50%, 0);
    font-family: 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif;
}
.dice-wrapper:active { // :active is the pseudo class when mouse is down
    -webkit-animation: myOrbit 4s linear infinite; /* Chrome, Safari 5 */
       -moz-animation: myOrbit 4s linear; /* Firefox 5-15 */
         -o-animation: myOrbit 4s linear; /* Opera 12+ */
            animation: myOrbit 4s linear; /* Chrome, Firefox 16+, 
                                                  IE 10+, Safari 5 */
}
// Keyframes...

Implementing this adjustment will allow the animation to trigger on button press and halt upon release.

WHEN ANIMATION SHOULD NOT HALT AFTER BEING RELEASED

If avoiding JavaScript is necessary, achieving this behavior becomes unattainable.

With JavaScript, make the following CSS modification:

.dice-wrapper {
    position: absolute;
    /* top: 50%; */
    top: 209px;
    right: -9px;
    /* left: 50%; */
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-flex-wrap: wrap;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    -webkit-transform: translate3d(-50%, -50%, 0);
    transform: translate3d(-50%, -50%, 0);
    font-family: 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif;
}
.dice-wrapper.pressed { // .pressed class will be added in JavaScript
    -webkit-animation: myOrbit 4s linear infinite; /* Chrome, Safari 5 */
       -moz-animation: myOrbit 4s linear; /* Firefox 5-15 */
         -o-animation: myOrbit 4s linear; /* Opera 12+ */
            animation: myOrbit 4s linear; /* Chrome, Firefox 16+, 
                                                  IE 10+, Safari 5 */
}
// Keyframes...

Note that :active has been replaced by .pressed

Incorporate the pressed class using JavaScript (using jQuery):

$(".dice-wrapper").click(function(){
    $(this).addClass("pressed"); // Activate the animation
})

Swap out click for mousedown to initiate the animation on button press. Utilize the setTimeout function to cease the animation:

$("#button").click(function(){
    $(this).addClass("pressed");
})
setTimeout(function(){
    $(this).removeClass("pressed")
}, 2000); // 2 seconds = 2000 milliseconds

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

What could be the reason for the clone function not duplicating the data within the table

Recently, I utilized jQuery's clone method to duplicate and add an HTML table. The rows and cells of the table were added using jQuery. However, despite using clone, it only replicated the headers and CSS, not the appended data. This raises the ques ...

Can you guide me on how to export a named function using module.exports?

I have a script file for my discord bot that includes a specific command and a function with parsing logic that I want to reuse in my main index.js // File: ./commands/scrumPrompt.js // The function const extractDeets = function (f, scrum) { let items ...

What is the best way to craft an if/else statement for a situation when a twig variable is undefined?

When utilizing twig to declare a variable called user: <script type="text/javascript> {% if user is defined %} var user = { example: {{ userjson | raw }} }; {% endif %} </script> If a user is not logged in, an error message a ...

Tips on using jQuery to check if two cards have matched

I have been working on a card matching game using jQuery, and I've encountered an issue. Even though I have written the code to compare two sets of cards, it always goes to the "card match" condition, even when the cards do not actually match. If you ...

What is the reason for XMLHttpRequest overwriting preexisting events?

After clicking a button, I trigger an XMLHttpRequest event. However, I noticed that the original event becomes null once the request is sent. Why does this occur? Is there a way to maintain the original event? document.getElementById('start') ...

Enabling or disabling dropdown lists within a gridview based on the selected value in a different dropdown list within the same gridview

Within my GridView, there are two dropdown lists: ddOnsiteOffsite and ddHours. My goal is to disable the ddHours dropdown if the selected text in ddOnsiteOffsite is "onsite". I've attempted to implement the following code snippet, but unfortunately, i ...

Is it possible for me to use @import to selectively choose what I need and disregard the rest?

If I am utilizing SASS and wish to incorporate a significant portion of another existing stylesheet, whether it is in SASS or CSS format, I have the option to import that particular sheet using @import, then employ @extend to apply some of its rules. Is ...

Error: The XML parsing in ASP failed to find a root element at the specified location

When clicking the button, I have jQuery/Ajax code that is supposed to pass the value of a selected radio button to a controller action and open a detail page. However, I am encountering an error. When using Mozilla Firefox, the console displays: XML Par ...

Guide on setting up JSME, a chemistry portal integrated with JavaScript, within vuejs

I am looking to integrate the JSME chemistry portal into my Vuejs application. Upon the initial page load, an error is displayed: JSME initialization error: HTML id jsme_container not found. Strangely, upon refreshing the page, the error disappears. How ...

Implement a function that runs upon Page Load using Javascript

Here is some Javascript code that loads a map with different regions. When you hover or click on a country, additional information about that country is displayed on the right side of the map. I would like to have a random country already displaying infor ...

Uploading a File to a Remote WCF Service via jQuery AJAX on Different Domains

I am currently facing some difficulties in achieving the following task and would really appreciate any guidance you can provide. My goal is to upload a file from my HTML5 site to a cross domain WCF service hosted in IIS 7.5. Additionally, I need to send ...

Encountering an error while using $state.go function in Angular JS testing

Below is the code snippet for a Controller in Angular JS: describe('Controller: homeCtrl', function () { beforeEach(module('incident')); var homeCtrl, $state; beforeEach(inject(function ($controller, _$state_) { $state = _ ...

What is the best way to create a JavaScript Up/Down Numeric input box using jQuery?

So, I have this Numeric input box with Up/Down buttons: HTML Markup: <div class="rotatortextbox"> <asp:TextBox ID="txtrunningtimeforfirstlot" ClientIDMode="Static" runat="server">0</asp:TextBox> (In mins) </div> <div cl ...

After invoking call(), the object becomes 'this' on a worldwide scope

I have a list containing the names of different methods. var methodList = ['method1', 'method2', 'method3', 'method4']; I dynamically select a method based on certain criteria. These methods are part of a larger cl ...

Prevent jQuery AJAX from stripping away the # character

Is it possible for the jQuery ajax function to preserve anchors in URLs, such as Something.html#anchor? What happens if we pass the # symbol as a parameter for a script? Are there ways to stop jQuery from removing the anchor in this scenario? ...

JavaScript ECMAScript 6 - WARNING: "Decorators can only be applied to a class when exporting"

In ECMAScript 6, I am attempting to export a function so that I can import it and utilize it in other files for the sake of writing DRY code. However, an error message is appearing: You can only use decorators on an export when exporting a class (16:0) ...

What steps should I take to ensure that clicking this button triggers the API request and returns the data in JSON format?

I'm attempting to have the button with id 'testp' return an api request in json format, however it seems to not be functioning properly. You can find the HTML code link here: https://github.com/atullu1234/REST-API-Developer-1/blob/main/js-bu ...

AngularJS enhanced dropdown navigation bar built using Bootstrap styling

I'm currently learning how to implement Bootstrap, but I've run into an issue. I created a dropdown in the navigation bar, but when I clicked on it, the dropdown didn't collapse as expected. The class="dropdown" didn't change to class= ...

Tips for concealing content within table cells

I am facing an issue with my form that contains a table. When the user clicks on the radio button labeled "No", I want the content of the subsequent cells in that row to become visible. However, when they click on "Yes", the content should be hidden again. ...

It appears that the JavaScript global dynamic variable is undefined as it changes from function to function, suggesting it might be

I'm encountering an issue with the way these two functions interact when used in onclick calls of elements. Due to external circumstances beyond my control, I need to keep track of when and how elements are hidden. Everything works perfectly as inten ...