Is it possible to utilize the addition assignment operator (`+=`) to modify the `transform:rotate('x'deg);` CSS property using

This is the code I have been working on:

#move{
height:70px;
width:70px;
border:2px solid black;
    border-radius:15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<input type="button" value="Move Right" onclick="
$('#move').css({'background-color':'aqua'});
$('#move').css({'position':'absolute',
                               'transition':'500ms ease-in-out',
                               'right':'-=50px',
                               'transform':'rotate(+=5deg)'});
"></input>
        
        <br>
        OBJECT
        <br>
<div id="move"></div>

My query arises here:

Can I achieve both movement and rotation of my div element by making it rotate +5 degrees every time the "MOVE RIGHT" button is clicked? Can an operator like += be added to the rotate value in the CSS property, for example:

'transform': 'rotate(+=5deg)'

Answer №1

It's important to note that the transform property can have varied values, and they are not commutative. For instance, check out these examples:

transform: translateX(100px) rotate(90deg); /* Outcome differs from... */
transform: rotate(90deg) translateX(100px); /* ... this result!       */

.box {
  height: 100px;
  width: 100px;
  background: red;
}
#box1 {
  transform: translateX(100px) rotate(90deg);
}
#box2 {
  transform: rotate(90deg) translateX(100px);
}
<div class="box" id="box1"></div>
<div class="box" id="box2"></div>

One might assume that adding a rotation of 45deg would be

transform: translateX(100px) rotate(135deg); /* Accepted modification    */
transform: rotate(135deg) translateX(100px); /* Not recommended option!   */

.box {
  height: 100px;
  width: 100px;
  background: red;
}
#box1 {
  transform: translateX(100px) rotate(135deg);
}
#box2 {
  transform: rotate(135deg) translateX(100px);
}
<div class="box" id="box1"></div>
<div class="box" id="box2"></div>

However, this approach only works for the initial example, where rotate was the final transformation function. Typically, the correct sequence should be

transform: translateX(100px) rotate(90deg) rotate(45deg); /* Right way to do it */
transform: rotate(90deg) translateX(100px) rotate(45deg); /* Correct order as well */

.box {
  height: 100px;
  width: 100px;
  background: red;
}
#box1 {
  transform: translateX(100px) rotate(90deg) rotate(45deg);
}
#box2 {
  transform: rotate(90deg) translateX(100px) rotate(45deg);
}
<div class="box" id="box1"></div>
<div class="box" id="box2"></div>

You can also apply this custom method:

$.fn.addTransform = function(val) {
  return this.each(function() {
    var tr = $(this).css('transform');
    if(tr === 'none') tr = '';
    $(this).css('transform', tr + ' ' + val);
  });
};

Utilize it like so

$('#move').addTransform('rotate(5deg)');

$.fn.addTransform = function(val) {
  return this.each(function() {
    var tr = $(this).css('transform');
    if(tr === 'none') tr = '';
    $(this).css('transform', tr + ' ' + val);
  });
};
$('input').click(function() {
  $('#move').css({
    backgroundColor: 'aqua',
    position: 'absolute',
    transition: '500ms ease-in-out',
    right: '-=50px'
  }).addTransform('rotate(5deg)');
});
#move {
  height: 70px;
  width: 70px;
  border: 2px solid black;
  border-radius: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="button" value="Move Right" />
<div id="move"></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

Angular UI Bootstrap collapse directive fails to trigger expandDone() function

I am currently utilizing UI Bootstrap for Angular in one of my projects, and I have developed a directive that encapsulates the collapse functionality from UI Bootstrap. Here is how it looks: app.directive( 'arSection', ['$timeout', fu ...

Interacting with PHP through JavaScript function calls

I am facing some frustration with my website due to an issue involving PHP. When I retrieve data from a PHP file, it returns a JSON list like this: {"Data":{"Recipes":{"Recipe_5":{"ID":"5","TITLE":"Spaghetti Bolognese"},"Recipe_7":{"ID":"7","TITLE":"Wurst ...

Using Vue.js to loop through data objects when a button is clicked

I'm currently working on building a quiz functionality using vue.js, but I've hit a roadblock in trying to make the "Next" button cycle through my data. The goal is to have the screen display the object containing the question and answers (e.g. q ...

Typescript's Nested Type Assignments

Simply put, I'm making an API call and receiving the following data: { getUserInfo: { country: 'DE', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c48594f487c59445d514c5059125f5351">[e ...

Is it advisable to include cursor:pointer in the pseudo class :hover?

Which option is better to use? .myclass { cursor: pointer; } or .myclass:hover { cursor: pointer; } Is there a notable difference between the two? ...

How can we sort an array based on the inner HTML values of its children elements in JavaScript?

Can you arrange a JavaScript array based on the innerText values of its HTML elements? I am generating a div element (class="inbox" id="inbox${var}") with a number as its innerText, then adding all these divs to an array (numArr). I wan ...

Craft an engaging and dynamic Image map with SVG technology to elevate responsiveness and interactivity

I'm currently working on a website and I need to create two clickable sections on the home page. Each section will lead to a different specialization of the company. To achieve this, I decided to use a square image split into two right-angled triangle ...

Necessary workaround needed for HTML time limit

Our HTML page has been designed to automatically timeout after 10 minutes of inactivity. This functionality is achieved through the following code: function CheckExpire() { $.ajax({ type:'GET', url:self.location.pathname+"?ch ...

Using jQuery to create radial-gradients with the background-css feature

I'm a newcomer to this online community and English is not my first language. Despite that, I will do my utmost to clearly explain the issue at hand. Recently, I utilized the .css function in jQuery to create a design element successfully. However, I ...

Learn how to establish a state using an array and effectively utilize the setState() method in React

For my latest project, which is API based, I am working with arrays that contain sub-arrays. How can I define a state with an array and utilize the setState() method to work with the entire array? ...

How to Incorporate LessCSS into the Blueprint Framework?

Can LessCSS be integrated with Blueprint framework? What are the prerequisites for starting? ...

Utilizing jQuery to determine the placement of a tooltip within a slider

I am currently utilizing both a jQuery Tooltip and a jQuery Slider (jQueryUI) simultaneously. While the slider is functioning correctly, I am encountering an issue where tooltips are being displayed in the wrong position after scrolling (view screenshot or ...

Create an Array of Objects by Sharing Your Post

Can someone guide me on the correct way to post my data here? I have been encountering errors while trying to insert data into user.SavedMachines.Id and user.SavedMachines.Date. I attempted using user.SavedMachines.Id = req.body.SavedMachinesId and user. ...

The behavior of CSS position: sticky varies depending on whether the user is scrolling up or scrolling down

I am experiencing an issue in my Vue CLI app where a component with the position: sticky CSS property is being partially hidden under the top of the browser when scrolling down, but works correctly when scrolling up. This behavior is also observed on my Ga ...

Enhance CSS delivery for the items listed below

Reduce the delay caused by rendering JavaScript and CSS above-the-fold. There are 16 CSS resources currently blocking the rendering of your page. This delay could be affecting the loading time of your content. To improve this issue, consider deferring or ...

I have a JavaScript code stored as a string that I need to transform into plain JavaScript

If I have a variable in string format, for example suppose there is a JavaScript code inside it: var string="function myFunction(a,b){return a*b;}"; I want to convert this into pure JavaScript code format like so: function myFunction(a, b) { return ...

Creating a tool to display mouse coordinates as a pointer on an HTML5 canvas

I'm struggling with mouse coordinates while working on canvas in HTML5. My approach involves using JavaScript for drawing on the canvas. Below is my current code to track the mouse pointer within the canvas: $('#canvasID').mousedown(functi ...

What is the best way to synchronize the image dimensions and source when dynamically loading images?

I am facing an issue with a function that updates images by altering the src and css (width/height) of an IMG tag within the document. Below is a simplified example to demonstrate the problem: updateImage = function(src, width, height) { $("#changeMe"). ...

This route does not allow the use of the POST method. Only the GET and HEAD methods are supported. This limitation is specific to Laravel

I am encountering an issue while attempting to submit an image via Ajax, receiving the following error message: The POST method is not supported for this route. Supported methods: GET, HEAD. Here is the Javascript code: $("form[name='submitProfi ...

Implementing relative path 'fetch' in NextJs App Router

When attempting to retrieve data using fetch("/api/status"), the URL is only detected when utilizing the absolute path: fetch("http://localhost:3000/api/status"). This is happening without storing the path in a variable. ...