2

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?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Michael
  • 13,950
  • 57
  • 145
  • 288
  • Go through this stack overflow [link](http://stackoverflow.com/questions/14396614/rotate-image-with-onclick) may be it can help you. – Shubham Baranwal Dec 06 '16 at 09:06
  • 1
    Possible duplicate of [CSS rotation cross browser with jquery.animate()](http://stackoverflow.com/questions/15191058/css-rotation-cross-browser-with-jquery-animate) – Justinas Dec 06 '16 at 09:10

7 Answers7

4

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" />
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

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>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Dev Man
  • 2,114
  • 3
  • 23
  • 38
1
<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>
1

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" />
P. Frank
  • 5,691
  • 6
  • 22
  • 50
0

Try this code :

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

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

});
0

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/

Sandy
  • 6,285
  • 15
  • 65
  • 93
-1

Change showLayers img to #showLayers.

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

Community
  • 1
  • 1
Brandon
  • 1,336
  • 3
  • 10
  • 38