Michael
Michael

Reputation: 13636

How to rotate an image on click

I have an image and each time I click on it I want to make it rotate 180 degrees. This is the code I tried:

<img id="showLayers" class="miniToolbarContant" src="../stdicons/GeomindIcons/slide.png"/>
$('#showLayers').click( function() { 
    $('#showLayers img').animate({ rotate: 180 });
});

This code does not work for some reason. Any ideas why the above code does not work?

Upvotes: 2

Views: 4362

Answers (7)

Sandy
Sandy

Reputation: 6353

There are many ways to do this. Below is one of the way:

var inti = 180;

$('img').click(function(){     
     var rotate = "rotate(" + inti + "deg)";
            var trans = "all 0.3s ease-out";
    $(this).css({
         "-webkit-transform": rotate,
                "-moz-transform": rotate,
                "-o-transform": rotate,
                "msTransform": rotate,
                "transform": rotate,
                "-webkit-transition": trans,
                "-moz-transition": trans,
                "-o-transition": trans,
                "transition": trans
    });
    inti += 180;
});

DEMO: https://jsfiddle.net/1464bgn2/

Upvotes: 0

Dev Man
Dev Man

Reputation: 2137

the .animate() method in jQuery accepts only integers as a value to a property so "180deg" is invalid instead create a class and toggle it

$(document).on('click', ".a", function() {
  $(".a").toggleClass('a_rotated');
})
.a {
  width: 100px;
  height: 100px;
  background-color: red;
  transition: all 0.2s ease-in-out;
}
.a_rotated {
  transform: rotate(180deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="a"></div>

Upvotes: 1

P. Frank
P. Frank

Reputation: 5745

Ok you can use the jquery css methods with rotate. With a variable you increm, your pictures rotate at 360 deg (180 by 180).

Please try:

var angle = 0;
$('#showLayers').click(function() {
  angle += 180
  $(this).css('-webkit-transform','rotate('+angle+'deg)'); 
});
#showLayers {
  transition: transform 0.3s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id="showLayers" class="miniToolbarContant" src="https://i.imgur.com/J5YLlJvl.png" width="250" />

Upvotes: 1

ramgopal chaudhary
ramgopal chaudhary

Reputation: 224

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
    var rot =180;
    function rotateImage(test){
        test.style.webkitTransform="rotate("+rot+"deg)";

    }
</script>
</head>
<body>

<div id="imgWrapper">
    <img src="test.png" id="image" onclick="rotateImage(this)">  
</div>

</body>
</html>

Upvotes: 1

Nour Alhammady
Nour Alhammady

Reputation: 9

Try this code :

$('#showLayers').click( function() {

    $('#showLayers img').css('transform', 'rotate(180deg)');

});

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337713

Firstly note that your issue is because the selector is incorrect. It should be $('#showLayers') not $('#showLayers img') - or even just $(this) as you're in the scope of the click handler.

Secondly, note that you can improve the logic by using CSS for the animation and simply toggling the class in JS as needed:

$('#showLayers').click(function() {
  $(this).toggleClass('rotate');
});
#showLayers {
  transition: transform 0.3s;
}

.rotate {
  -webkit-transform: rotate(180deg);
  -moz-transform: rotate(180deg);
  -o-transform: rotate(180deg);
  -ms-transform: rotate(180deg);
  transform: rotate(180deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id="showLayers" class="miniToolbarContant" src="https://i.imgur.com/J5YLlJvl.png" width="250" />

Upvotes: 4

Brandon
Brandon

Reputation: 1625

Change showLayers img to #showLayers.

Also rotate: 180 isn't valid styling. See this answer for how to rotate a div.

Upvotes: -1

Related Questions