51

Is it possible to rotate a div using jQuery? I can rotate an image but can't rotate a div; is there any solution for this?

Rubens
  • 14,478
  • 11
  • 63
  • 92
DEVOPS
  • 18,190
  • 34
  • 95
  • 118
  • 1
    possible duplicate of [Rotating a Div Element in jQuery](http://stackoverflow.com/questions/382591/rotating-a-div-element-in-jquery) – Bobby Jun 11 '10 at 07:28
  • 1
    @Sjoerd: Please read the FAQ: http://meta.stackexchange.com/questions/8724/how-to-deal-with-google-questions – Bobby Jun 11 '10 at 07:28
  • 4
    I dont get those rules for closing questions. People are trying to find solutions for their problems (not just the one who asked the question). I found this and I knew there is better solution, so I kept searching. But I couldnt come back and place better answer here because "he didnt try hard enough to search his own solution". Wasn't the point of this site to actually provide answers for ppl in as short time as possible? When hes not trying on his own hes "cheating" himself - HE wont become good programmer but its his fight not our or yours. – Srneczek Jun 23 '15 at 09:58
  • 1
    @user1096901, no, this is a Q&A site, not just an A site. It is important that the questions are worded properly. A lot of effort goes into trying to get people to ask proper questions that can be answered. You can find more on this in the [help/on-topic]. – Abel Sep 26 '15 at 02:46
  • It's 2021, I googled and this came up. It's the answer I was looking for. – Motionharvest Dec 25 '21 at 20:29

1 Answers1

85

EDIT: Updated for jQuery 1.8

Since jQuery 1.8 browser specific transformations will be added automatically. jsFiddle Demo

var rotation = 0;

jQuery.fn.rotate = function(degrees) {
    $(this).css({'transform' : 'rotate('+ degrees +'deg)'});
    return $(this);
};

$('.rotate').click(function() {
    rotation += 5;
    $(this).rotate(rotation);
});

EDIT: Added code to make it a jQuery function.

For those of you who don't want to read any further, here you go. For more details and examples, read on. jsFiddle Demo.

var rotation = 0;

jQuery.fn.rotate = function(degrees) {
    $(this).css({'-webkit-transform' : 'rotate('+ degrees +'deg)',
                 '-moz-transform' : 'rotate('+ degrees +'deg)',
                 '-ms-transform' : 'rotate('+ degrees +'deg)',
                 'transform' : 'rotate('+ degrees +'deg)'});
    return $(this);
};

$('.rotate').click(function() {
    rotation += 5;
    $(this).rotate(rotation);
});

EDIT: One of the comments on this post mentioned jQuery Multirotation. This plugin for jQuery essentially performs the above function with support for IE8. It may be worth using if you want maximum compatibility or more options. But for minimal overhead, I suggest the above function. It will work IE9+, Chrome, Firefox, Opera, and many others.


Bobby... This is for the people who actually want to do it in the javascript. This may be required for rotating on a javascript callback.

Here is a jsFiddle.

If you would like to rotate at custom intervals, you can use jQuery to manually set the css instead of adding a class. Like this! I have included both jQuery options at the bottom of the answer.

HTML

<div class="rotate">
    <h1>Rotatey text</h1>
</div>

CSS

/* Totally for style */
.rotate {
    background: #F02311;
    color: #FFF;
    width: 200px;
    height: 200px;
    text-align: center;
    font: normal 1em Arial;
    position: relative;
    top: 50px;
    left: 50px;
}

/* The real code */
.rotated {
    -webkit-transform: rotate(45deg);  /* Chrome, Safari 3.1+ */
    -moz-transform: rotate(45deg);  /* Firefox 3.5-15 */
    -ms-transform: rotate(45deg);  /* IE 9 */
    -o-transform: rotate(45deg);  /* Opera 10.50-12.00 */
    transform: rotate(45deg);  /* Firefox 16+, IE 10+, Opera 12.10+ */
}

jQuery

Make sure these are wrapped in $(document).ready

$('.rotate').click(function() {
    $(this).toggleClass('rotated');
});

Custom intervals

var rotation = 0;
$('.rotate').click(function() {
    rotation += 5;
    $(this).css({'-webkit-transform' : 'rotate('+ rotation +'deg)',
                 '-moz-transform' : 'rotate('+ rotation +'deg)',
                 '-ms-transform' : 'rotate('+ rotation +'deg)',
                 'transform' : 'rotate('+ rotation +'deg)'});
});
Jacob van Lingen
  • 8,989
  • 7
  • 48
  • 78
Dan Grahn
  • 9,044
  • 4
  • 37
  • 74
  • that rotation is better? css or jquery, http://plugins.jquery.com/multirotation/. thanks. – A. M. Mérida Jun 28 '13 at 11:10
  • Added info in the answer about this plugin. Take a look at the source code for the plugin. It has support for IE8 and a few more features, but other than that it does exactly what this does + overhead. – Dan Grahn Jun 28 '13 at 11:25
  • Simple and works perfectly. Thanks for making it a jQuery function. You can replace the definition `jQuery.fn.rotate` with `$.fn.rotate` - does the same thing but more concise. – clearlight Dec 15 '15 at 02:53
  • Since jQuey 1.8 the browser prefixes are no longer needed, see also: http://stackoverflow.com/a/17487728/2049986. – Jacob van Lingen Jun 20 '16 at 09:55
  • @JacobvanLingen Thanks! I've updated the answer. – Dan Grahn Jun 28 '16 at 11:44