0

I want to rotate my element using javascript. My method for rotation:

function AnimateSidebar(sidebar) {
        sidebar.style.width = "20%";

        let arrow = document.getElementById("arrow");
        arrow.style.transform = "rotate(60deg);";
        arrow.style = "tranform(rotate(60deg));";

        arrow.style.mozTransform = "rotate(60deg);";
        arrow.style.msTransform = "rotate(60deg);";
        arrow.style.oTransform = "rotate(60deg);";
        arrow.style.webkitTransform = "rotate(60deg);";
        
        arrow.style = "margin-left:85%;margin-right:0;";
    }

I tried to rotate it in several ways, as you can see above, but nothing works. The last line of code to change the margin of the arrow element works fine.

My css for the arrow element: (the initial rotation in css file works ok)

#arrow{
    transform:rotate(20deg);
    transition:0.8s;
}

My browser is Microsoft Edge.

EDIT: Solved. I had to put the margin property above the tranform property.

2 Answers 2

1

The bug appears to be that you have a semicolon inside the string value of your transforms.

Element.style.transform expects the exact CSS, so writing -

arrow.style.transform = "rotate(60deg);";

is roughly interpreted as -

#arrow {
   transform: rotate(60deg);;
}

Notice the double semicolon, which is invalid CSS.

let arrow = document.getElementById("arrow");
arrow.style.transform = "rotate(60deg)";
#arrow {
  width: min-content;
}
<div id="arrow">→</div>

Sign up to request clarification or add additional context in comments.

2 Comments

This does not solve the problem. As I said, the last line of JS works fine, despite containing the semicolon in the string.
Ah yes that last line was overriding all of the statements above. The semicolon works in that last line as you are setting the complete style and not just a single value.
1

Leave out the arrow.style = "tranform(rotate(60deg));";

Try to first change the margin and then transform the arrow afterwards:

arrow.style = "margin-left:85%;margin-right:0;";
arrow.style.transform = "rotate(60deg)";  //without the semicolon!

Or directly set the margin-left using the style margin property:

arrow.style.marginLeft = "85%";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.