Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery rotate image onclick

Tags:

jquery

I am trying to achieve this (90 degree rotation), but it fails without errors. This image is within a <a></a> TAG where it has already a toggle jquery function.

<?php echo "<img src='down_arrow.png' onclick='$(this).animate({rotate: \"+=90deg\"});' />"; ?>
like image 269
Art Planteur Avatar asked Jan 16 '13 18:01

Art Planteur


People also ask

How to rotate image on click in jQuery?

<? php echo "<img src='down_arrow. png' onclick='$(this). animate({rotate: \"+=90deg\"});' />"; ?>

How to use transform rotate in jQuery?

$(function() { var $elie = $("#bkgimg"); rotate(45); function rotate(degree) { // For webkit browsers: e.g. Chrome $elie. css({ WebkitTransform: 'rotate(' + degree + 'deg)'}); // For Mozilla browser: e.g. Firefox $elie. css({ '-moz-transform': 'rotate(' + degree + 'deg)'}); } });

How to rotate a div in jQuery?

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


2 Answers

Depending on which browser versions you need to support, you could try CSS tranforms.

First, define a CSS class like this:

.rotated {
  transform: rotate(90deg);
  -ms-transform: rotate(90deg); /* IE 9 */
  -moz-transform: rotate(90deg); /* Firefox */
  -webkit-transform: rotate(90deg); /* Safari and Chrome */
  -o-transform: rotate(90deg); /* Opera */
}

And then you can use jQuery to add the class when clicking the link:

<img src="down_arrow.png" onclick="$(this).addClass('rotated');" />
like image 55
Tom van Enckevort Avatar answered Oct 07 '22 10:10

Tom van Enckevort


Use a combination of css rules -webkit-transform and -moz-transform on image click.For example to rotate image by 90 degree apply following css rules on click

$('img').click(function(){
    $(this).css({
        "-webkit-transform": "rotate(90deg)",
        "-moz-transform": "rotate(90deg)",
        "transform": "rotate(90deg)" /* For modern browsers(CSS3)  */
    });
});
like image 23
Varinder Singh Avatar answered Oct 07 '22 09:10

Varinder Singh