Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  101] [ 2]  / answers: 1 / hits: 30383  / 12 Years ago, fri, january 18, 2013, 12:00:00

I try to make a toggle button where my image (an arrow) will be rotated by 180 degrees each click:



<a href='#' onclick='$(#divToggle).slideToggle(slow);'>
<img src='blue_down_arrow.png' onclick='$(this).rotate(180);' /></a>

<div id='divToggle' style='display:none;'>...CONTENT...</div>;


This code toggle my div but the image rotates only for the first click and then stays up. I am using this : http://code.google.com/p/jqueryrotate/


More From » jquery

 Answers
6

This is because the value for rotate angle is absolute, not based on the last rotate.



Working code:



jQuery



var rotate_factor = 0;

function rotateMe(e) {
rotate_factor += 1;
var rotate_angle = (180 * rotate_factor) % 360;
$(e).rotate({angle:rotate_angle});
}


HTML



<img src='blue_down_arrow.png' onclick='rotateMe(this);' /></a>


Update: Shorter code



var rotate_angle = 0;

<img src='blue_down_arrow.png' onclick='rotate_angle = (rotate_angle + 180) % 360; $(this).rotate(rotate_angle);' /></a>

[#80782] Thursday, January 17, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
theron

Total Points: 168
Total Questions: 93
Total Answers: 94

Location: South Georgia
Member since Fri, Nov 13, 2020
4 Years ago
;