Change the duration of a CSS animation rotation using jQuery and an input range control

I'm trying to rotate an element using the -webkit-animation properties, but I want to be able to control the duration using jQuery and an input range slider.

Unfortunately, I'm having trouble getting it to work and I can't figure out what's causing the issue.

$('#speed').change(function() {
    $('.rotate').css({'-webkit-animation': 'rotation '+this.value+'s linear infinite'});
});

Check out the Fiddle here

Answer №1

Utilizing the requestAnimationFrame method is a more efficient way to control animations compared to manually adding and removing keyframes. Although adjusting and creating CSS animations can be challenging, requestAnimationFrame behaves similarly to setInterval but optimizes CPU usage by only rendering when necessary. Learn more about requestAnimationFrame here.

var spd=0, r=0, box = $('.rotate');

$('#speed').on('change', function () {
    spd = +(this.value);
});

function start() {
    r += spd;
    box.css('transform', 'rotate(' + r + 'deg)');
    requestAnimationFrame(start);
}
start();

Answer №2

It's a bit unusual, but an interesting approach is to recreate the animation when the speed changes. This will effectively restart the animation with the updated speed.

Check out this fiddle for a demo.

$('#speed').on('change', function () {

    var element = $(".rotate"),
    n = element.clone(true);

    element.before(n);

    $(".rotate:last").remove();
    $('.rotate').css("-webkit-animation", "rotate " + (30/this.value) + "s linear infinite");
});

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

Exploring Checkbox Limiting with jQuery

Is there a more efficient approach to restrict the selection of checkboxes? I want the script to be adaptable depending on the applied class, which will always indicate the maximum allowed value (e.g., "limit_1" or "limit_2"). Currently, I'm creatin ...

A guide on showcasing a blob type in a struts2 jquery grid sourced from a mysql database

I managed to successfully save an image as a blob type in MySQL and now I'm looking to showcase it using the Struts2 jQuery grid widget. However, when I tried using edittype="image", it only displayed some random string instead of the actual image. Ca ...

Problem with the `file` input not working properly

Currently, I am working on creating a form with two input fields - one for text and the other for file upload. However, the issue I am facing is that the Browse button for the file input is being displayed inside the input box. My goal is to have the butto ...

Trigger an event when the menu is outside the viewport and then lock the menu to the top of the viewport

I am trying to create a menu element that remains fixed at the top of the browser viewport as the user scrolls, ensuring it is always visible. In my webpage layout, the menu sits below some text in the header initially. Once the user scrolls past the heade ...

Choose gets stuck while loading an abundance of data using JQuery's .html() and JSON

Here's the code snippet I'm currently using: $.getJSON("registro/backend.php?action=list&id="+sel.value, function(data){ var options = []; for (var i=0; i<data.rows.length; i++) { options += &apos ...

Looking to invoke a jQuery ajax page method

Just diving into jQuery and looking to execute a method at specified intervals. $(document).ready(function pageLoad() { setTimeout('runMethod()', 10000); }); (function runMethod() { $.ajax({ type: "POST", url ...

What are your thoughts on this method for preventing scrolling?

Is there a way to prevent scrolling of the underlying content on a webpage when a modal is displayed? I found this code snippet that achieves that. (http://jsfiddle.net/77P2e/) var $window = $(window), previousScrollTop = 0, scrollLock = false; $window.s ...

Utilizing Images with 'General Drawing' in Highcharts

I'm currently attempting to create a task diagram using Highcharts. I had the idea of incorporating images using the <img> tag ren.label('<img src="/images/test.jepg', 10, 82) .attr({ ...

Executing a save function in the controller when the TinyMCE save button is clicked through an Angular directive

For the Rich text editing in my angularjs application, I am using the TinyMce angular directive. The directive is working perfectly as expected. However, I wanted to include the save plugin to enable custom save functions. To integrate the save plugin, I ...

The alignment of two responsive divs is off

My goal is to create two responsive columns of divs that stack on top of each other as the screen size decreases. The challenge I am facing is getting these divs to always be centered and sit next to each other, using only inline CSS. <div id="containe ...

The setting page yields no discernible impact

Currently, I have integrated jQuery DataTables into my project with the following initialization code: $('#admin_users_table').DataTable({ "paging": true, "columnDefs": [ { "render": function(data, type, row) { switch ...

Tips for integrating File Upload with an AJAX form in MVC 4

I am looking to incorporate a File Upload feature in MVC 4 that is compatible with all browsers including IE 8 or higher, Chrome, and FF. The key requirement is to have AJAX support, as I intend to integrate it within an AJAX.BeginForm. While I have foun ...

Conceal the standard style class of the p:selectOneRadio element

On my xhtml page, I've implemented p:selectOneRadio. However, I'm struggling to remove the default style class .ui-helper-hidden-accessible, resulting in the radio button icons not being visible. Here's a snippet of my code: <p:selectOne ...

InvalidSelectorError: The specified selector is not valid //button[contains(., 'buttonText')] while running JavaScript code

Here's an example of HTML code that I am working with: <button id="toolbar-item-17-update-id" type="submit" name="action" value="update" class="button-action-update btn btn-secondary"> Ed ...

Guide on implementing a looping settimeout function on a sliding page to dynamically load an HTML template within the Framework 7 framework

My current setup involves using Framework7 (framework7.io). The code below functions correctly on the main view page, utilizing a looping settimeout to refresh signups.html every second: <script type=“text/javascript” src=“code.jquery.com/jquery- ...

What is the best 'event' to pair with an <input/> element in iOS/Android development?

Looking for a way to toggle results when a user starts typing in a search field? Here are some event options: mousedown / mouseup touchstart / touchend focus You could also consider using the "change" event instead of "click" to check for text input an ...

Problem with the spacing in the Bootstrap website after the breadcrumbs due to flexbox issues

I'm facing a challenge with one of my Bootstrap-based pages that I can't quite figure out. We recently implemented breadcrumbs for navigation, which is also built with Bootstrap. However, some pages are displaying a significant gap between the br ...

What is the best way to add multiple classes to an HTML element?

Can a single HTML container have more than one class assigned to it? For example, would the following code work: <article class="column wrapper"> ...

Autocomplete alert in jQuery: Get the selected value from the suggestions upon selection

I'm using the jQuery autocomplete plugin for a movie search feature. Each movie has an IMDb ID which is enclosed in a span with the class "imdbID" when parsed by PHP. Here's an example of how it looks: <li>Movie title 1<span class="imdb ...

How to use jQuery and AJAX to dynamically fill a list?

After creating a jQuery & AJAX script that utilizes a PHP function to retrieve data from an API, I encountered a problem. Each time I search for a new book, it overwrites the previous search results. My goal is to assign a book ID, conduct a search, displ ...