3

I want to have a button displayed on screen, next to a single character input field. When the user clicks on the button the character would rotate 1 degree, click it 5 times and it would rotate 5 degrees, hold it down and it would spin.

I would also love for the current degree of rotation to be displayed to the user (this is very important for what I want to build at the end).

Currently I have HTML:

<h2 class="rotate10"> P</h2>

CSS:

.rotate10{

    /* Safari */
    -webkit-transform: rotate(-10deg);

    /* Firefox */
    -moz-transform: rotate(-10deg);

    /* IE */
    -ms-transform: rotate(-10deg);

    /* Opera */
    -o-transform: rotate(-10deg);

    /* Internet Explorer */
    filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
}
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
Kōdo no musō-ka
  • 769
  • 1
  • 8
  • 23

4 Answers4

5

you need to assign transform css property on click

http://jsfiddle.net/NourSammour/pu58xsmg/6/

var angle = 10;    

$('#line').click(function() {
    $(this).find('span').css ({
        '-webkit-transform': 'rotate(' + angle + 'deg)',
           '-moz-transform': 'rotate(' + angle + 'deg)',
             '-o-transform': 'rotate(' + angle + 'deg)',
            '-ms-transform': 'rotate(' + angle + 'deg)'
    });
    angle+=10;
});

to display the current angle/degree you can display angle variable

Nour Sammour
  • 2,822
  • 1
  • 20
  • 19
5

Try something like this

var count = 0;
var target = $('h2');

$('button').mousedown(function(){
    target = setInterval(function(){
       count++;
       $('h2').css('transform', 'rotate('+count+'deg)');
       $('p').text('Rotated ' + count + 'deg');
    }, 100);
    return false;
});

$(document).mouseup(function(){
    clearInterval(target);
    return false;
});
button {
  display: block;
}

p {
  margin: 0;
}

h2 {
  display: inline-block;
  transform-origin: center;
  font-size: 50px;
  margin-top: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Rotate</button>
<p>Rotated 0deg</p>
<h2 class="rotate10">P</h2>
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
3
<button id="ex" style="width:40px">
  hello
</button>

js

 var count=1
    $("#ex").on("click",function(){
        $("#ex").css({"-ms-transform":" rotate("+10*count+"deg)"});
      $("#ex").css({"-webkit-transform":" rotate("+10*count+"deg)"});
      $("#ex").css({"transform":"rotate("+10*count+"deg)"});
      count++;
    });
mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54
1

Combining Nour's solution (which only listened to onClick) and the answer from this thread I created this fiddle: http://jsfiddle.net/ea6zycpk/

var angle = 1;    
var timeoutId = 0;
var timeoutId2 = 0;

$('#line').mousedown(function() {
    timeoutId = setInterval(rotate, 20);
    timeoutId2 = setInterval(update, 20);
}).on('mouseup mouseleave', function() {
    clearInterval(timeoutId);
    clearInterval(timeoutId2);
});

function rotate() {
    $('#line').find('span').css ({
        '-webkit-transform': 'rotate(' + angle + 'deg)',
           '-moz-transform': 'rotate(' + angle + 'deg)',
             '-o-transform': 'rotate(' + angle + 'deg)',
            '-ms-transform': 'rotate(' + angle + 'deg)'
    });
    angle+=1;
};

function update() {
  $('#angle').text(angle);
}
Community
  • 1
  • 1
dasjanik
  • 326
  • 3
  • 12