0

Hi guys at the moment i am trying to figure out how to rotate a div and make it stop at a loctation. I have had some help on this however iam not sure what iam doing wrong. Any help with the code would be great.

<script type="text/javascript">
var $elie = $("#super");
rotate(1);

function rotate(degree) {
$elie.css({
    WebkitTransform: 'rotate(' + degree + 'deg)'
});
$elie.css({
    '-moz-transform': 'rotate(' + degree + 'deg)'
});

if (degree < 1196) {
    timer = setTimeout(function() {
        rotate(++degree);
    }, 1);
}
}
4
  • You don't assign to the unprefixed version? The unprefixed version is used by IE, Firefox, and Opera. Commented Nov 29, 2012 at 0:41
  • i dont understand.. Sorry iam not the best with jquery Commented Nov 29, 2012 at 0:42
  • The unprefixed version of that CSS property is transform. Commented Nov 29, 2012 at 0:43
  • Sorry still dont get u. Maybe if u give me an example i could try it out and see what u mean Commented Nov 29, 2012 at 0:47

2 Answers 2

2

Here is a reusable version that I got working. Hope it helps you out.

$(function () {

    var rotateAnimation = function (props) {
        // init animation props
        var rotateEl = props.el,
            curAngle = props.startAngle,
            endAngle = props.endAngle;
        // scope angle to parent function
        var angleValue = 'rotate(' + curAngle + 'deg)';

        // define worker function
        var rotate = function () {
            // increment the angle
            curAngle += props.increment;

            // see if we are done animating
            if (curAngle >= endAngle) {
                curAngle = endAngle;
                clearInterval(timer);//stop looping

            }
            // create css value 
            angleValue = 'rotate(' + curAngle + 'deg)';
            rotateEl.css({
                '-moz-transform': angleValue,
                '-webkit-transform': angleValue,
                '-o-transform': angleValue,
                '-ms-transform':angleValue
            });
        };

        var timer = setInterval(rotate, props.delay);//let the fun begin
    };

    $('#super').on('click', function () {
        rotateAnimation({
            el: $(this),
            startAngle: 0,
            endAngle: 1234,
            delay: 1,
            increment: 3
        });
    });

});
Sign up to request clarification or add additional context in comments.

Comments

1

It looks like it is rotating and stopping, but the value of 1196 takes a while to get to. Here's a jsfiddle with what's going on. http://jsfiddle.net/2x3Us/

Html:

<div id="super" style="background-color: red">hello</div>​

Javascript:

var $elie = $("#super");
rotate(1);

function rotate(degree) {
    $elie.css({
        '-ms-transform': 'rotate(' + degree + 'deg)',
        '-webkit-transform': 'rotate(' + degree + 'deg)',
        '-o-transform': 'rotate(' + degree + 'deg)',
        '-moz-transform': 'rotate(' + degree + 'deg)'
    });

    console.log(degree);
    if (degree < 360) {
        timer = setTimeout(function() {
            rotate(++degree);
        }, 1);
    }
}​

8 Comments

still dose not work. Can i ask what libarys i need 2 use to use this function?
do you not see the div being rotated? Try it now... I added a background color to make it clear.
The div i got dose not even rotate those. Thats the problem
Did you look at the link that I put in the answer? In regard to a library, you need to have jquery included.
what browser are you using?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.