Skip to content

Instantly share code, notes, and snippets.

@jgphilpott
Forked from hoandang/get_rotate.js
Last active August 20, 2023 20:19
Show Gist options
  • Save jgphilpott/1bc17b82063f14fabb8f3e38825f6f10 to your computer and use it in GitHub Desktop.
Save jgphilpott/1bc17b82063f14fabb8f3e38825f6f10 to your computer and use it in GitHub Desktop.
Rotate any jQuery selected element.
# Rotate any jQuery selected element.
# Credit: https://gist.github.com/hoandang/5989980
# Credit: https://stackoverflow.com/a/15191130/1544937
$.fn.rotate = (degree = 0, duration = 1000) ->
element = $(this)
rotation = ->
matrix = element.css "-webkit-transform" or
element.css "-moz-transform" or
element.css "-ms-transform" or
element.css "-o-transform" or
element.css "transform"
if matrix isnt "none"
matrix = matrix.split("(")[1].split(")")[0].split(",")
return Math.round Math.atan2(matrix[1], matrix[0]) * (180 / Math.PI)
else
return 0
rotation = rotation()
if rotation isnt degree
$("deg": rotation).animate "deg": degree,
"duration": duration
"step": (now) =>
element.css "transform": "rotate(" + now + "deg)"
// Rotate any jQuery selected element.
// Credit: https://gist.github.com/hoandang/5989980
// Credit: https://stackoverflow.com/a/15191130/1544937
$.fn.rotate = function(degree = 0, duration = 1000) {
var element, rotation;
element = $(this);
rotation = function() {
var matrix;
matrix = element.css("-webkit-transform" || element.css("-moz-transform" || element.css("-ms-transform" || element.css("-o-transform" || element.css("transform")))));
if (matrix !== "none") {
matrix = matrix.split("(")[1].split(")")[0].split(",");
return Math.round(Math.atan2(matrix[1], matrix[0]) * (180 / Math.PI));
} else {
return 0;
}
};
rotation = rotation();
if (rotation !== degree) {
return $({
"deg": rotation
}).animate({
"deg": degree
}, {
"duration": duration,
"step": (now) => {
return element.css({
"transform": "rotate(" + now + "deg)"
});
}
});
}
};
@jgphilpott
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment