JQUERY animate не работает несколько раз подряд

216
06 декабря 2017, 23:37

И так, вот код ниже, он крутит тюленюшку по кнопке, делает 2 оборота + случайные 100-300 градусов. Так вот если нажать кнопку, подождать завершения кручения, и нажать ее ещё раз, то он начнет анимацию с того места где он закончил (rotate). Мне нужно чтобы он каждый раз заново от 0 deg крутил тюленюшку. Я задавал до анимации css transform rotate 0deg, не помогало.

jQuery.easing['jswing'] = jQuery.easing['swing']; 
 
jQuery.extend( jQuery.easing, 
{ 
	def: 'easeOutQuad', 
	swing: function (x, t, b, c, d) { 
		//alert(jQuery.easing.default); 
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d); 
	}, 
	easeInQuad: function (x, t, b, c, d) { 
		return c*(t/=d)*t + b; 
	}, 
	easeOutQuad: function (x, t, b, c, d) { 
		return -c *(t/=d)*(t-2) + b; 
	}, 
	easeInOutQuad: function (x, t, b, c, d) { 
		if ((t/=d/2) < 1) return c/2*t*t + b; 
		return -c/2 * ((--t)*(t-2) - 1) + b; 
	}, 
	easeInCubic: function (x, t, b, c, d) { 
		return c*(t/=d)*t*t + b; 
	}, 
	easeOutCubic: function (x, t, b, c, d) { 
		return c*((t=t/d-1)*t*t + 1) + b; 
	}, 
	easeInOutCubic: function (x, t, b, c, d) { 
		if ((t/=d/2) < 1) return c/2*t*t*t + b; 
		return c/2*((t-=2)*t*t + 2) + b; 
	}, 
	easeInQuart: function (x, t, b, c, d) { 
		return c*(t/=d)*t*t*t + b; 
	}, 
	easeOutQuart: function (x, t, b, c, d) { 
		return -c * ((t=t/d-1)*t*t*t - 1) + b; 
	}, 
	easeInOutQuart: function (x, t, b, c, d) { 
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b; 
		return -c/2 * ((t-=2)*t*t*t - 2) + b; 
	}, 
	easeInQuint: function (x, t, b, c, d) { 
		return c*(t/=d)*t*t*t*t + b; 
	}, 
	easeOutQuint: function (x, t, b, c, d) { 
		return c*((t=t/d-1)*t*t*t*t + 1) + b; 
	}, 
	easeInOutQuint: function (x, t, b, c, d) { 
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b; 
		return c/2*((t-=2)*t*t*t*t + 2) + b; 
	}, 
	easeInSine: function (x, t, b, c, d) { 
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b; 
	}, 
	easeOutSine: function (x, t, b, c, d) { 
		return c * Math.sin(t/d * (Math.PI/2)) + b; 
	}, 
	easeInOutSine: function (x, t, b, c, d) { 
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b; 
	}, 
	easeInExpo: function (x, t, b, c, d) { 
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b; 
	}, 
	easeOutExpo: function (x, t, b, c, d) { 
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b; 
	}, 
	easeInOutExpo: function (x, t, b, c, d) { 
		if (t==0) return b; 
		if (t==d) return b+c; 
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b; 
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b; 
	}, 
	easeInCirc: function (x, t, b, c, d) { 
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b; 
	}, 
	easeOutCirc: function (x, t, b, c, d) { 
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b; 
	}, 
	easeInOutCirc: function (x, t, b, c, d) { 
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b; 
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b; 
	}, 
	easeInElastic: function (x, t, b, c, d) { 
		var s=1.70158;var p=0;var a=c; 
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3; 
		if (a < Math.abs(c)) { a=c; var s=p/4; } 
		else var s = p/(2*Math.PI) * Math.asin (c/a); 
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b; 
	}, 
	easeOutElastic: function (x, t, b, c, d) { 
		var s=1.70158;var p=0;var a=c; 
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3; 
		if (a < Math.abs(c)) { a=c; var s=p/4; } 
		else var s = p/(2*Math.PI) * Math.asin (c/a); 
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b; 
	}, 
	easeInOutElastic: function (x, t, b, c, d) { 
		var s=1.70158;var p=0;var a=c; 
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5); 
		if (a < Math.abs(c)) { a=c; var s=p/4; } 
		else var s = p/(2*Math.PI) * Math.asin (c/a); 
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b; 
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b; 
	}, 
	easeInBack: function (x, t, b, c, d, s) { 
		if (s == undefined) s = 1.70158; 
		return c*(t/=d)*t*((s+1)*t - s) + b; 
	}, 
	easeOutBack: function (x, t, b, c, d, s) { 
		if (s == undefined) s = 1.70158; 
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b; 
	}, 
	easeInOutBack: function (x, t, b, c, d, s) { 
		if (s == undefined) s = 1.70158;  
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; 
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b; 
	}, 
	easeInBounce: function (x, t, b, c, d) { 
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b; 
	}, 
	easeOutBounce: function (x, t, b, c, d) { 
		if ((t/=d) < (1/2.75)) { 
			return c*(7.5625*t*t) + b; 
		} else if (t < (2/2.75)) { 
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b; 
		} else if (t < (2.5/2.75)) { 
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b; 
		} else { 
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b; 
		} 
	}, 
	easeInOutBounce: function (x, t, b, c, d) { 
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b; 
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b; 
	} 
}); 
 
 
function getRandomArbitary(min, max) 
{ 
  return Math.random() * (max - min) + min; 
} 
 
function test(){ 
 
$("#content-wheel-canvas").animate({rotation: 720 + getRandomArbitary(100,300)}, 
    { 
       duration: 8000, 
	   easing: "easeOutCubic", 
       step: function(now, fx) { 
            
            $(this).css({"transform": "rotate("+now+"deg)"}); 
       } 
    });  
    } 
    
    
$( "#test" ).click(function() { 
 test(); 
}); 
     
    
#content-wheel { 
    padding-top: 60px; 
    padding-bottom: 70px; 
    position: relative 
} 
#content-wheel-pointer { 
    position: absolute; 
    width: 100%; 
    transform: rotate(180deg); 
    bottom: 85px 
} 
#content-wrapper { 
    background-color: #26262c; 
    overflow-y: scroll; 
    overflow-x: scroll; 
    padding-bottom: 30px 
} 
#wheel-pointer { 
    display: block; 
    margin: auto; 
    height: 24px; 
    width: 24px; 
    padding-top: 16px; 
    -webkit-transition: .1s ease; 
    -moz-transition: .1s ease; 
    -ms-transition: .1s ease; 
    -o-transition: .1s ease; 
    transition: .1s ease; 
    color: transparent; 
    opacity: 0 
} 
.duel-wheel-pointer { 
    position: absolute; 
    width: 100%; 
    transform: rotate(180deg); 
    text-align: center; 
    top: 145px; 
    color: #333 
} 
#content-wheel-canvas { 
    height: 100%; 
    width: 100% 
} 
#content-wheel-canvas-wrapper { 
    margin: auto; 
    height: 400px; 
    width: 400px; 
    transform: rotate(180deg) 
} 
#wheel { 
    display: block; 
    margin: auto; 
    margin-top: 0; 
    width: 100%; 
    height: 100%; 
    opacity: 0; 
    background: url(https://image.ibb.co/d7xykG/image_big_0014489.png) no-repeat center; 
    background-size: 100% 
} 
#content-wheel-balance { 
    position: absolute; 
    left: 40px; 
    right: 0; 
    bottom: 60px; 
    z-index: 1 
} 
#content-wheel-autobetter { 
    position: absolute; 
    left: 40px; 
    right: 0; 
    top: 250px; 
    z-index: 1; 
    display: none 
} 
#content-wheel-timer { 
    position: absolute; 
    bottom: 125px; 
    width: 100% 
} 
#wheel-timer { 
    display: none; 
    margin: auto; 
    width: 400px; 
    opacity: 1; 
    font-size: 30px; 
    color: #444; 
    text-align: center; 
    font-weight: 300 
} 
#content-wheel { 
    padding-top: 60px; 
    padding-bottom: 70px; 
    position: relative 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<button style="width: 200px; height: 30px;" id="test" >ЖМИ</button> 
 </button> 
 <div id="content-wrapper"> 
<div id="content-wheel"> 
               <div id="content-wheel-canvas-wrapper"> 
                  <div id="content-wheel-canvas" class="noselect" style="transform: rotate(1060.07deg);"> 
                     <div id="wheel" style="opacity: 1;"></div> 
                  </div> 
               </div> 
            </div> 
              </div>

Answer 1

или вообще убрать эти значения от предыдущей анимации (продолжая идею которую предложил @Дмитрий Полянин ). добавив код

 $("#content-wheel-canvas").css("rotation", "");

получается, например, так:

jQuery.easing['jswing'] = jQuery.easing['swing']; 
 
jQuery.extend(jQuery.easing, { 
  def: 'easeOutQuad', 
  swing: function(x, t, b, c, d) { 
    //alert(jQuery.easing.default); 
    return jQuery.easing[jQuery.easing.def](x, t, b, c, d); 
  }, 
  easeInQuad: function(x, t, b, c, d) { 
    return c * (t /= d) * t + b; 
  }, 
  easeOutQuad: function(x, t, b, c, d) { 
    return -c * (t /= d) * (t - 2) + b; 
  }, 
  easeInOutQuad: function(x, t, b, c, d) { 
    if ((t /= d / 2) < 1) return c / 2 * t * t + b; 
    return -c / 2 * ((--t) * (t - 2) - 1) + b; 
  }, 
  easeInCubic: function(x, t, b, c, d) { 
    return c * (t /= d) * t * t + b; 
  }, 
  easeOutCubic: function(x, t, b, c, d) { 
    return c * ((t = t / d - 1) * t * t + 1) + b; 
  }, 
  easeInOutCubic: function(x, t, b, c, d) { 
    if ((t /= d / 2) < 1) return c / 2 * t * t * t + b; 
    return c / 2 * ((t -= 2) * t * t + 2) + b; 
  }, 
  easeInQuart: function(x, t, b, c, d) { 
    return c * (t /= d) * t * t * t + b; 
  }, 
  easeOutQuart: function(x, t, b, c, d) { 
    return -c * ((t = t / d - 1) * t * t * t - 1) + b; 
  }, 
  easeInOutQuart: function(x, t, b, c, d) { 
    if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b; 
    return -c / 2 * ((t -= 2) * t * t * t - 2) + b; 
  }, 
  easeInQuint: function(x, t, b, c, d) { 
    return c * (t /= d) * t * t * t * t + b; 
  }, 
  easeOutQuint: function(x, t, b, c, d) { 
    return c * ((t = t / d - 1) * t * t * t * t + 1) + b; 
  }, 
  easeInOutQuint: function(x, t, b, c, d) { 
    if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b; 
    return c / 2 * ((t -= 2) * t * t * t * t + 2) + b; 
  }, 
  easeInSine: function(x, t, b, c, d) { 
    return -c * Math.cos(t / d * (Math.PI / 2)) + c + b; 
  }, 
  easeOutSine: function(x, t, b, c, d) { 
    return c * Math.sin(t / d * (Math.PI / 2)) + b; 
  }, 
  easeInOutSine: function(x, t, b, c, d) { 
    return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b; 
  }, 
  easeInExpo: function(x, t, b, c, d) { 
    return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b; 
  }, 
  easeOutExpo: function(x, t, b, c, d) { 
    return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b; 
  }, 
  easeInOutExpo: function(x, t, b, c, d) { 
    if (t == 0) return b; 
    if (t == d) return b + c; 
    if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b; 
    return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b; 
  }, 
  easeInCirc: function(x, t, b, c, d) { 
    return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b; 
  }, 
  easeOutCirc: function(x, t, b, c, d) { 
    return c * Math.sqrt(1 - (t = t / d - 1) * t) + b; 
  }, 
  easeInOutCirc: function(x, t, b, c, d) { 
    if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b; 
    return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b; 
  }, 
  easeInElastic: function(x, t, b, c, d) { 
    var s = 1.70158; 
    var p = 0; 
    var a = c; 
    if (t == 0) return b; 
    if ((t /= d) == 1) return b + c; 
    if (!p) p = d * .3; 
    if (a < Math.abs(c)) { 
      a = c; 
      var s = p / 4; 
    } else var s = p / (2 * Math.PI) * Math.asin(c / a); 
    return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b; 
  }, 
  easeOutElastic: function(x, t, b, c, d) { 
    var s = 1.70158; 
    var p = 0; 
    var a = c; 
    if (t == 0) return b; 
    if ((t /= d) == 1) return b + c; 
    if (!p) p = d * .3; 
    if (a < Math.abs(c)) { 
      a = c; 
      var s = p / 4; 
    } else var s = p / (2 * Math.PI) * Math.asin(c / a); 
    return a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b; 
  }, 
  easeInOutElastic: function(x, t, b, c, d) { 
    var s = 1.70158; 
    var p = 0; 
    var a = c; 
    if (t == 0) return b; 
    if ((t /= d / 2) == 2) return b + c; 
    if (!p) p = d * (.3 * 1.5); 
    if (a < Math.abs(c)) { 
      a = c; 
      var s = p / 4; 
    } else var s = p / (2 * Math.PI) * Math.asin(c / a); 
    if (t < 1) return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b; 
    return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b; 
  }, 
  easeInBack: function(x, t, b, c, d, s) { 
    if (s == undefined) s = 1.70158; 
    return c * (t /= d) * t * ((s + 1) * t - s) + b; 
  }, 
  easeOutBack: function(x, t, b, c, d, s) { 
    if (s == undefined) s = 1.70158; 
    return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b; 
  }, 
  easeInOutBack: function(x, t, b, c, d, s) { 
    if (s == undefined) s = 1.70158; 
    if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b; 
    return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b; 
  }, 
  easeInBounce: function(x, t, b, c, d) { 
    return c - jQuery.easing.easeOutBounce(x, d - t, 0, c, d) + b; 
  }, 
  easeOutBounce: function(x, t, b, c, d) { 
    if ((t /= d) < (1 / 2.75)) { 
      return c * (7.5625 * t * t) + b; 
    } else if (t < (2 / 2.75)) { 
      return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b; 
    } else if (t < (2.5 / 2.75)) { 
      return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b; 
    } else { 
      return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b; 
    } 
  }, 
  easeInOutBounce: function(x, t, b, c, d) { 
    if (t < d / 2) return jQuery.easing.easeInBounce(x, t * 2, 0, c, d) * .5 + b; 
    return jQuery.easing.easeOutBounce(x, t * 2 - d, 0, c, d) * .5 + c * .5 + b; 
  } 
}); 
 
 
function getRandomArbitary(min, max) { 
  return Math.random() * (max - min) + min; 
} 
 
function test() { 
  $("#content-wheel-canvas").css("rotation", ""); 
  $("#content-wheel-canvas").animate({ 
    rotation: 720 + getRandomArbitary(100, 300) 
  }, { 
    duration: 8000, 
    easing: "easeOutCubic", 
    step: function(now, fx) { 
 
      $(this).css({ 
        "transform": "rotate(" + now + "deg)" 
      }); 
    } 
  }); 
} 
 
 
$("#test").click(function() { 
  test(); 
});
#content-wheel { 
  padding-top: 60px; 
  padding-bottom: 70px; 
  position: relative 
} 
 
#content-wheel-pointer { 
  position: absolute; 
  width: 100%; 
  transform: rotate(180deg); 
  bottom: 85px 
} 
 
#content-wrapper { 
  background-color: #26262c; 
  overflow-y: scroll; 
  overflow-x: scroll; 
  padding-bottom: 30px 
} 
 
#wheel-pointer { 
  display: block; 
  margin: auto; 
  height: 24px; 
  width: 24px; 
  padding-top: 16px; 
  -webkit-transition: .1s ease; 
  -moz-transition: .1s ease; 
  -ms-transition: .1s ease; 
  -o-transition: .1s ease; 
  transition: .1s ease; 
  color: transparent; 
  opacity: 0 
} 
 
.duel-wheel-pointer { 
  position: absolute; 
  width: 100%; 
  transform: rotate(180deg); 
  text-align: center; 
  top: 145px; 
  color: #333 
} 
 
#content-wheel-canvas { 
  height: 100%; 
  width: 100% 
} 
 
#content-wheel-canvas-wrapper { 
  margin: auto; 
  height: 400px; 
  width: 400px; 
  transform: rotate(180deg) 
} 
 
#wheel { 
  display: block; 
  margin: auto; 
  margin-top: 0; 
  width: 100%; 
  height: 100%; 
  opacity: 0; 
  background: url(https://image.ibb.co/d7xykG/image_big_0014489.png) no-repeat center; 
  background-size: 100% 
} 
 
#content-wheel-balance { 
  position: absolute; 
  left: 40px; 
  right: 0; 
  bottom: 60px; 
  z-index: 1 
} 
 
#content-wheel-autobetter { 
  position: absolute; 
  left: 40px; 
  right: 0; 
  top: 250px; 
  z-index: 1; 
  display: none 
} 
 
#content-wheel-timer { 
  position: absolute; 
  bottom: 125px; 
  width: 100% 
} 
 
#wheel-timer { 
  display: none; 
  margin: auto; 
  width: 400px; 
  opacity: 1; 
  font-size: 30px; 
  color: #444; 
  text-align: center; 
  font-weight: 300 
} 
 
#content-wheel { 
  padding-top: 60px; 
  padding-bottom: 70px; 
  position: relative 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<button style="width: 200px; height: 30px;" id="test">ЖМИ</button> 
</button> 
<div id="content-wrapper"> 
  <div id="content-wheel"> 
    <div id="content-wheel-canvas-wrapper"> 
      <div id="content-wheel-canvas" class="noselect" style="transform: rotate(1060.07deg);"> 
        <div id="wheel" style="opacity: 1;"></div> 
      </div> 
    </div> 
  </div> 
</div>

Answer 2

Он продолжает с того же места где закончил, ведь jquery.animate меняет сами значения параметров при анимации, то есть после анимации новые значения уже записаны в объект. Если вы хотите крутить с начала вам просто необходимо перед новой анимацией вручную установить начальные значения.

То есть в самом начале вызова test() вы делаете SetStartValues(). Разумеется SetStartValues() вам самим придётся реализовать.

READ ALSO
Как управлять элементом на странице при перетаскивании его мышкой [drag&amp;drop]

Как управлять элементом на странице при перетаскивании его мышкой [drag&drop]

Привет всем! Ребята, есть задача создать конструктор украшенийЕсть лента элементов и рабочая область на которой расположен шнурок

136
Удаление дублей в select multiple

Удаление дублей в select multiple

Есть функция которая вызывает следующий код:

201
Вывод текста из БД сдвигает блок

Вывод текста из БД сдвигает блок

Мучаюсь с этой проблемой уже почти два дняПишу на Bootstrap 3, страница без вывода текста выглядит так:

286
Определение города посетителя js

Определение города посетителя js

Использую сервис для определения гео http://apisypexgeo

247