javascript - Rotating DIV spins slowly on second click - Stack Overflow

I think I overlooked something. This is a very simple spin-the-bottle game.JavascriptjQuery$('.bo

I think I overlooked something. This is a very simple spin-the-bottle game.

Javascript/jQuery

$('.bottle').on('click', function(e) {
    this.removeAttribute('style');
    var deg = 3000 + Math.round(Math.random() * 500);
    var css = '-webkit-transform: rotate(' + deg + 'deg);';

    this.setAttribute(
        'style', css
    );
});

CSS:

.bottle {
    width: 200px;
    height: 200px;
    background-image: url(img/bottle.png);
    -webkit-transition: -webkit-transform 6s ease-out;
}

HTML:

<div class="bottle"></div>

This works perfectly on the first click of the bottle. But starting from the second click, the spin is very very slow?

I think I overlooked something. This is a very simple spin-the-bottle game.

Javascript/jQuery

$('.bottle').on('click', function(e) {
    this.removeAttribute('style');
    var deg = 3000 + Math.round(Math.random() * 500);
    var css = '-webkit-transform: rotate(' + deg + 'deg);';

    this.setAttribute(
        'style', css
    );
});

CSS:

.bottle {
    width: 200px;
    height: 200px;
    background-image: url(img/bottle.png);
    -webkit-transition: -webkit-transform 6s ease-out;
}

HTML:

<div class="bottle"></div>

This works perfectly on the first click of the bottle. But starting from the second click, the spin is very very slow?

Share Improve this question edited Feb 21, 2013 at 5:48 Anujith 9,3706 gold badges35 silver badges48 bronze badges asked Dec 25, 2012 at 9:59 Martin LyderMartin Lyder 1692 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

Try this : http://jsfiddle/sMcAN/

var i = 1;
$('.bottle').on('click', function(e) {
this.removeAttribute('style');
var deg = 3000 + Math.round(Math.random() * 500);
deg = ((-1) ^ i) * deg;

var css = '-webkit-transform: rotate(' + deg + 'deg);';
this.setAttribute('style', css);
i++;
});​

Another update : http://jsfiddle/sMcAN/2/

This is because at first, you are going from 0 to a value over 3000. But then, the value is always within 3000 - so the difference is not big enough and it still takes the 6 seconds you have defined.

One solution would be to make sure that you offset the value and make it different by few thousand each time.

var i = 0, offset = [2000, 4000, 6000, 3000, 5000, 1000];

$('.bottle').on('click', function(e) {
  this.removeAttribute('style');

  var deg = offset[i] + Math.round(Math.random() * 500);
  i++;
  if (i > 5) {
    i = 0;
  }

  var css = '-webkit-transform: rotate(' + deg + 'deg);';

  this.setAttribute(
    'style', css
 );
});​
math.round(math.random() * 1000);

Try that

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745425110a4627140.html

相关推荐

  • javascript - Rotating DIV spins slowly on second click - Stack Overflow

    I think I overlooked something. This is a very simple spin-the-bottle game.JavascriptjQuery$('.bo

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信