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!

jangle Demojangle - GitHub Source Code

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 }

Jonathan June 23, 2011 at 11:34 AM

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! :)

Reply

Cory September 15, 2011 at 1:36 PM

Continuous rotation has been added and is in the latest version on GitHub.

Reply

Cory June 24, 2011 at 10:44 AM

@Jonathan-
try this:

$(‘img’).click(function() {
$(this).jangle(90);
});

Reply

Cory June 24, 2011 at 11:29 AM

Here’s a working example:

Reply

Cory June 24, 2011 at 11:40 AM

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:

Reply

Manu July 21, 2011 at 5:15 AM

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

Reply

Cory July 29, 2011 at 2:34 PM

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. :)

Reply

Cory September 15, 2011 at 1:34 PM

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.

Kapil July 23, 2011 at 9:55 AM

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.

Reply

Cory July 29, 2011 at 12:42 PM

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.

Reply

Dave September 15, 2011 at 9:48 AM

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

Reply

Cory September 15, 2011 at 1:33 PM

@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.

Reply

parmenion September 23, 2011 at 6:10 AM

Hi, i look for a way to display the angle in an input text, while i use the slider. Is that possible?

ty

Reply

Cory September 23, 2011 at 11:29 PM

@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);
}
});

Reply

parmenion October 3, 2011 at 5:46 AM

yay, that’s work. Thank you!

Reply

Sergey February 5, 2012 at 9:51 PM

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?

Reply

Sergey February 6, 2012 at 10:42 AM

Nevermind. I had something overwriting position value. By adding !important I fixed it. Thanks for the great plugin.

Reply

Jeremy February 23, 2012 at 12:03 AM

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!

Reply

Cory February 27, 2012 at 4:11 PM

Thanks for the heads up. Issue resolved. Download the latest source from GitHub.

Reply

Ben March 8, 2012 at 10:28 AM

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 :)

Reply

Ben March 8, 2012 at 10:30 AM

^ that should say <p> elements and <p> tags above (I didn’t escape them when typing in lol!)

Reply

Cory March 8, 2012 at 3:20 PM

good to know, especially for anyone who runs into similar issues. will take a look when i get a chance. thanks.

Reply

Joe Trusty March 12, 2012 at 1:02 PM

Great plugin Cory. Gave me a great idea for this time based slider application we have to build.

Reply

Leave a Comment