javascript: how to rotate text using javascript?


Dexing

I have the following code:

<img src="http://www.w3schools.com/html/html5.gif" id="imgid">
<h1 id='header'>Example</h1>
<!--This is an example html doc not the real thing!-->
<button onclick="spin(document.getElementById('header'));spin(document.getElementById('imgid'));">Spin!</button>
<script>
  function spin(object) {
    setInterval(function() {
      object.style.transform += "rotate(10deg)";
    }, 1);
  }
</script>

How can I make the text rotate correctly? It would be great if you could explain how it works. If you need any reference to my sources then:

"Correct" means centered rotation. (Not the whole page).

Thanks!

Tundra Fitz

Try this. The main problem is that the width stretches all the way to the whole page, which messes up the rotation, so adding display:inline-block makes the width match the content of the div.

<style>
#imgid{display:inline-block;}
#myDIV{display:inline-block;}
</style>

<html>
    <head>
        <title>Example</title>
    </head>

    <body>
        <div><img src="http://www.w3schools.com/html/html5.gif" id="imgid"></div>
        <div><h1 id='myDIV'>Example</h1></div>
        <div><button onclick="spin(document.getElementById('imgid'));spin(document.getElementById('myDIV'));">xdfdsf</button></div>
    </body>
</html>

<script>
function spin(object)
{
    setInterval(function()
    {
        object.style.transform += "rotate(10deg)";
    }, 1);
}
</script>

Related


javascript: how to rotate text using javascript?

Dexing I have the following code: <img src="http://www.w3schools.com/html/html5.gif" id="imgid"> <h1 id='header'>Example</h1> <!--This is an example html doc not the real thing!--> <button onclick="spin(document.getElementById('header'));spin(document.getEleme

javascript: how to rotate text using javascript?

ten stars I have the following code: <img src="http://www.w3schools.com/html/html5.gif" id="imgid"> <h1 id='header'>Example</h1> <!--This is an example html doc not the real thing!--> <button onclick="spin(document.getElementById('header'));spin(document.getEl

javascript: how to rotate text using javascript?

Dexing I have the following code: <img src="http://www.w3schools.com/html/html5.gif" id="imgid"> <h1 id='header'>Example</h1> <!--This is an example html doc not the real thing!--> <button onclick="spin(document.getElementById('header'));spin(document.getEleme

javascript: how to rotate text using javascript?

Dexing I have the following code: <img src="http://www.w3schools.com/html/html5.gif" id="imgid"> <h1 id='header'>Example</h1> <!--This is an example html doc not the real thing!--> <button onclick="spin(document.getElementById('header'));spin(document.getEleme

Rotate text with Javascript

Cole Roberts I want to iterate through a sequence of words to create a text rotation effect. Most of my work works as expected. Is there any way to use css transitions on the length of the p element? The movement is not smooth when going from an object with ch

Rotate text in canvas with JavaScript

Jason Samuels In the code below I want to rotate the text of each element of an array in javascript. If I use eg ctx.rotate(Math.PI/10)before filling the text , it will rotate all elements. The position of the text also changes, ctx.rotate(Math.PI/10)if I use

Rotate text in canvas with JavaScript

Jason Samuels In the code below I want to rotate the text of each element of an array in javascript. If I use eg ctx.rotate(Math.PI/10)before filling the text , it will rotate all elements. The position of the text also changes, ctx.rotate(Math.PI/10)if I use

How to Rotate an Image Using CSS or JavaScript

Guillermo Nahuel Varelli I would like to know if there is a way to rotate an image without using fotoshop, I want to use CSS or JavaScript The result I am looking for is here The following solution works, but I want to rotate the image without the hover proper

How to Rotate an Image Using CSS and JavaScript

new bike I want to simulate a spinning moon in the top right corner of this website If I already have the material "picture" Give me some simple samples or hints and I'll look into them. Adrian Enriquez The site uses CSS transitions. Please check out the demo

How to rotate GIFs in the background using JavaScript?

Cod 360 I'm having a hard time playing a gif in the body background using javascript. I have set the background in the css body and a variable in the root in style.css to access this variable in javascript. It doesn't work this way. My javascript code is perfe

How to rotate an image back and forth using JavaScript

alex 96 I'm trying to create a website where I want the faces to be rotated to a certain point and then rotated back in the opposite direction until a certain point, if they can go on and on I'd like to do this, but I only have Can this do a full rotation fore

How to Rotate an Image Using CSS or JavaScript

Guillermo Nahuel Varelli I would like to know if there is a way to rotate an image without using fotoshop, I want to use CSS or JavaScript The result I am looking for is here The following solution works, but I want to rotate the image without the hover proper

How to Rotate an Image Using CSS and JavaScript

new bike I want to simulate a spinning moon in the top right corner of this website If I already have the material "picture" Give me some simple samples or hints and I'll look into them. Adrian Enriquez The site uses CSS transitions. Please check out the demo

How to rotate an image back and forth using JavaScript

alex 96 I'm trying to create a website where I want the faces to be rotated to a certain point and then rotated back in the opposite direction until a certain point, if they can go on and on I'd like to do this, but I only have Can this do a full rotation fore

rotate div using javascript

user2078845: I want to click on one div and rotate another div, then when the first div is clicked again, the other div rotates back to its original position. I can reference this library if I need http://ricostacruz.com/jquery.transit . adenet; To rotate the

rotate div using javascript

username I want to click on one div and rotate another div, then when the first div is clicked again, the other div rotates back to its original position. I can reference this library if I need http://ricostacruz.com/jquery.transit . Adene To rotate the DIV, w

rotate div using javascript

user2078845: I want to click on one div and rotate another div, then when the first div is clicked again, the other div rotates back to its original position. I can reference this library if I need http://ricostacruz.com/jquery.transit . adenet; To rotate the

How to Rotate Hue in JavaScript

Lance Pollard I saw a function from to here , which doesn't make sense to me, I thought it would start from 0-360.spin-360360 I want to extend the concept of complementary colors, triples and quads (which are built into another library) and generate arbitrary

How to Rotate Hue in JavaScript

Lance Pollard I saw a function from to here , which doesn't make sense to me, I thought it would start from 0-360.spin-360360 I want to extend the concept of complementary colors, triples and quads (which are built into another library) and generate arbitrary

InDesign JavaScript: How to rotate an image inside a text frame?

willow This is my first question here and I hope my question is well explained below. When new to javascript, I'm writing a script to place (hundreds of) images in an InDesign document. Now I am stuck due to the following problem. After placing the images into

InDesign JavaScript: How to rotate an image inside a text frame?

willow This is my first question here and I hope my question is well explained below. When new to javascript, I'm writing a script to place (hundreds of) images in an InDesign document. Now I am stuck due to the following problem. After placing the images into

InDesign JavaScript: How to rotate an image inside a text frame?

willow This is my first question here and I hope my question is well explained below. When new to javascript, I'm writing a script to place (hundreds of) images in an InDesign document. Now I am stuck due to the following problem. After placing the images into

InDesign JavaScript: How to rotate an image inside a text frame?

willow This is my first question here and I hope my question is well explained below. When new to javascript, I'm writing a script to place (hundreds of) images in an InDesign document. Now I am stuck due to the following problem. After placing the images into

InDesign JavaScript: How to rotate an image inside a text frame?

willow This is my first question here and I hope my question is well explained below. When new to javascript, I'm writing a script to place (hundreds of) images in an InDesign document. Now I am stuck due to the following problem. After placing the images into

InDesign JavaScript: How to rotate an image inside a text frame?

willow This is my first question here and I hope my question is well explained below. When new to javascript, I'm writing a script to place (hundreds of) images in an InDesign document. Now I am stuck due to the following problem. After placing the images into