Michael
Michael

Reputation: 168

Stop start function on button click

I have function that rotate div inside another div. How can i start and stop rotating the div on button click. One button to start rotating another to stop it. And is there a way to rotate div only when i slide slider right or left not like it is now on mouse move? Please help. Function for rotating div :

var img = $(".inner");

if (img.length > 0) {
  var offset = img.offset();

  function mouse(evt) {
    var center_x = (offset.left) + (img.width() / 2);
    var center_y = (offset.top) + (img.height() / 2);
    var mouse_x = evt.pageX;
    var mouse_y = evt.pageY;
    var radians = Math.atan2(mouse_x - center_x, mouse_y - center_y);
    var degree = (radians * (180 / Math.PI) * -1) + 90;
    img.css('-moz-transform', 'rotate(' + degree + 'deg)');
    img.css('-webkit-transform', 'rotate(' + degree + 'deg)');
    img.css('-o-transform', 'rotate(' + degree + 'deg)');
    img.css('-ms-transform', 'rotate(' + degree + 'deg)');
  }
  $(".inner").mousemove(mouse);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="inner" style="width:100px; height:100px; background-color:black; display:block; ">

Upvotes: 4

Views: 1290

Answers (2)

Pedram
Pedram

Reputation: 16575

You can set class to detect if img has clicked or not:

Self Click Version:

var img = $(".inner");
var offset = img.offset();

function mouse(evt) {
  var center_x = (offset.left) + (img.width() / 2);
  var center_y = (offset.top) + (img.height() / 2);
  var mouse_x = evt.pageX;
  var mouse_y = evt.pageY;
  var radians = Math.atan2(mouse_x - center_x, mouse_y - center_y);
  var degree = (radians * (180 / Math.PI) * -1) + 90;
  img.css('-moz-transform', 'rotate(' + degree + 'deg)');
  img.css('-webkit-transform', 'rotate(' + degree + 'deg)');
  img.css('-o-transform', 'rotate(' + degree + 'deg)');
  img.css('-ms-transform', 'rotate(' + degree + 'deg)');
}

img.on('click', function(e) {

  $(this).toggleClass('clicked');

});

img.on('mousemove', function(evt) {
  if (!img.hasClass('clicked')) {
    mouse(evt);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="inner" style="width:100px; height:100px; background-color:black; display:block; ">

Button Click Version:

var img = $(".inner");
var offset = img.offset();

function mouse(evt) {
  var center_x = (offset.left) + (img.width() / 2);
  var center_y = (offset.top) + (img.height() / 2);
  var mouse_x = evt.pageX;
  var mouse_y = evt.pageY;
  var radians = Math.atan2(mouse_x - center_x, mouse_y - center_y);
  var degree = (radians * (180 / Math.PI) * -1) + 90;
  img.css('-moz-transform', 'rotate(' + degree + 'deg)');
  img.css('-webkit-transform', 'rotate(' + degree + 'deg)');
  img.css('-o-transform', 'rotate(' + degree + 'deg)');
  img.css('-ms-transform', 'rotate(' + degree + 'deg)');
}

$('#Stop').on('click', function(e) {
  img.addClass('clicked');
});

$('#Start').on('click', function(e) {
  img.removeClass('clicked');
});

img.on('mousemove', function(evt) {
  if (!img.hasClass('clicked')) {
    mouse(evt);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="inner clicked" style="width:100px; height:100px; background-color:black; display:block; ">
</div>
<button id="Start">Start</button>
<button id="Stop">Stop</button>

Upvotes: 1

Bj&#246;rn C
Bj&#246;rn C

Reputation: 4008

With Jquery you can use:

$('selector').on('click', function(){

}

To rotate you can use Jquery animate: http://api.jquery.com/animate/

Upvotes: 0

Related Questions