jangle – the jQuery rotation plugin
Welcome to the jangle project page! jangle is a jQuery plugin that allows you to rotate an image or any other HTML element by simply applying jangle to the jQuery selector of your choice! Rotate away!
Available Options
The jangle plugin has the following options to create various rotation effects:
continuous: true or false, // account for previous rotations? (default is false)
degrees: 0, // degrees element should be rotated
duration: 0, // length of time (in m/s) the animation takes
interval: 100 // length of time (in m/s) between each rotation occurs
NOTE: The interval should always be larger than the duration option.
Requirements
To use jangle, you must be using jQuery version 1.6+. It has been fully tested on IE6+ and the latest versions of Chrome, Firefox and Safari.
**NOTE:** While this does work in IE6/IE7/IE8, due to the way position: relative; works, layout behavior may not be consistent.
Setup
Reference the jQuery library followed the jangle JavaScript file:
<!-- load jQuery library --> <script src="/common/js/libs/jquery-1.5.2.min.js"></script> <!-- load jangle plugin --> <script src="jquery-jangle-0.3.js"></script>
Examples
Now you are ready to use jangle! Here is some example code on the how you can use it.
Page Load
// rotate a DIV 90 degrees
$('div').jangle(90);
Event Driven
// rotate a DIV 90 degrees upon clicking a link
$('a').click(function() {
$('div').jangle(90);
});
Input Driven
// see jangle-example.html for full code sample
// rotate an image based on input value
$('button').click(function() {
var angle = parseInt($('input[type=text]').val()) || 0;
$('img').jangle(angle);
});
Slider Driven
// see jangle-example.html for full code sample
// rotate an image based on slider position
$('.slider').slider({
min: 0, // degrees
max: 360, // degrees
slide: function(e, ui) {
$('img').jangle(ui.value);
},
stop: function(e, ui) { // in case you out slide the slider
$('img').jangle(ui.value);
}
})

{ 23 comments… read them below or add one }
I know I sound like an amateur, but I need help here. I have all the script and code in the right places but I can’t figure out how to get an image to rotate. I wanted to CLICK on an image to make it rotate 90 degrees. After reading this article it sounds like it will do what I want. I just don’t see any examples of how to use the code in relation to an image to make it clickable to rotate.
Please help! Thank you so much for this code! I have been scouring the internet in search of something like this. Thanks!
Continuous rotation has been added and is in the latest version on GitHub.
@Jonathan-
try this:
$(‘img’).click(function() {
$(this).jangle(90);
});
Here’s a working example:
Your question did get me thinking though…I’m going to add to the plugin the ability to do incremental rotation. Busy at the moment but will hopefully add in the near future. However, here’s another version using jQuery’s toggle method:
Great plugin, works great.
I really like the rotation toggle-effect. However, I wonder if there’s a way to make the rotation-effect a bit “smoother”, maybe with parameters like “durability of rotation” or something like that…
Cheers
Using the slider somewhat accomplishes this. Using jQuery’s ‘animation’ method, this could probably be accomplished as well. If I get time, I’ll take a look.
I believe you were initially referring to ‘duration’ instead of ‘durability’ in your post. This has been added in the latest version, which you can get form GitHub. Will update this page soon with example.
Great and easy to use. But I have one issue.
If we rotate an image having less height and more width, the image is cutting .
I have tested this with jqueryrotate.js also. The same is happening there.
Hmm, not exactly sure what you are seeing. Based on what you’re describing seems to fit the example that I have though. The image height is less than the width, but no clipping occurs. I would check the parent container styles and see if you have set up any overflows, which could indeed introduce some clipping. Feel free to produce a simplified test case using a site like jsFiddle and post it here. I will happily take a look.
Great plugin!
Three requests:
1. Add a “rotatable” option, when true it adds a handle to the element which lets you rotate the object manually.
2. Add a method that returns the current angle of a element.
3. Add a “rotatable” class to the element
So pretty much like jqueryUI.resizeable but rotatable instead.
Cheers
@Dave
Some nice ideas! I’ll definitely look into it. #2 & #3 should be be relatively simple. For #1 I have actually experimented with this with varying degrees of success. Might dig it back up since you’re interested in it. Stay tuned.
Hi, i look for a way to display the angle in an input text, while i use the slider. Is that possible?
ty
@parmenion you could do something similar to the following. I haven’t tested but it should be something close to this:
$('.selector' ).slider({
change: function(event, ui) {
$('input[type=text]').val(ui.value);
}
});
yay, that’s work. Thank you!
Cory,
After looking at several options to rotate the text I concluded this is the best one. Easy to implement! However, I ran into an issue with IE browser. I tested your demo and it works great, but my page is having a problem… Please see BBBB label at http://k8r.me/tsO13R
It works great in FF, Safari and Chrome, but IE…
What am I missing here?
Nevermind. I had something overwriting position value. By adding !important I fixed it. Thanks for the great plugin.
Hey, great plugin!
I’m having an issue though, where I’m getting this error :
TypeError: ‘undefined’ is not an object (evaluating ‘this[0].style’)
I get it only on pages that do not have the element the selector is looking for.
It refers me to this line: `elStyle = this[0].style;`
I have the code in the footer of my site. I can add conditional statements to not show on certain pages, but that will get very tedious.
Thanks for your help!
Thanks for the heads up. Issue resolved. Download the latest source from GitHub.
Great plugin, Cory! Just thought i’d post a note to let you know of an issue I ran into.
I had some trouble with the rotation seemingly not working in IE8. It wasn’t until I added a background color that I realised the DIV was actually rotating, it was the elements inside that weren’t.
After a great deal of stress i finally realised it’s because I had position: relative applied in my stylesheet to all tags and for some reason this was messing up the filter rendering in IE8. Removing it fixed the problem
^ that should say <p> elements and <p> tags above (I didn’t escape them when typing in lol!)
good to know, especially for anyone who runs into similar issues. will take a look when i get a chance. thanks.
Great plugin Cory. Gave me a great idea for this time based slider application we have to build.