2017-03-06 49 views
0

后不能正常工作有一个函数调用我有我的变换图像旋转步骤中的第一时间,它被称为在最初的函数调用时不工作后的问题我的页面加载。每次在我构建的图表上更改任何值时,都会调用此函数。为了画一幅画,有两枚硬币从屏幕的顶部落下,然后旋转两次,然后落在一叠硬币上。取而代之的是,每次调用函数时,硬币只会从屏幕的顶部放下/添加动画。与图像旋转jQuery的动画第一个函数调用

<div id="secondCoin"><img class="coin" src="./images/one_coin.png" alt="Second Coin Drops" /></div> 
<div id="firstCoin"><img class="coin" src="./images/one_coin.png" alt="First Coin Drops" /></div> 
<div id="showCoins"><img class="coin-stack" src="./images/fullstack_coins.png" alt="Stack of Coins" /></div> 

function coinStack(height){ 
var coinSound = document.getElementById('coin-sound'); 
var rotateDeg = 720; 
// prevents sound play on multiple clicks 
coinSound.pause(); 
coinSound.currentTime = 0; 
// causes coin stack to slide upward 
$("#showCoins").css("display", "inline-block"); 
$("#showCoins").css("top", height + "px"); 
screenWidth <= 400 ? $("#showCoins").stop().animate({top: '3'},1000) : $("#showCoins").stop().animate({top: '0'},1000); 
// first coin drops 
$("#firstCoin").css("display", "inline-block"); 
$("#firstCoin").css("top", "-324px"); 
$("#firstCoin").clearQueue().delay(1000).animate({top: '10', deg: rotateDeg},{ 
    duration: 350, 
    step: function(now){ 
     $("#firstCoin").css({ transform: "rotate(" + now + "deg)" }); 
    } 
}); 
// second coin drops 
$("#secondCoin").css("display", "inline-block"); 
$("#secondCoin").css("top", "-324px"); 
$("#secondCoin").clearQueue().delay(1400).animate({top: '0', deg: rotateDeg},{ 
    duration: 350, 
    step: function(now){ 
     $("#secondCoin").css({ transform: "rotate(" + now + "deg)" }); 
    } 
}); 
// coin sound effect 
coinSound.volume = 1.0; 
coinSound.play(); 

}

我使用.stop(尝试),.clearQueue(),设定/删除不同的变换属性,并检查了计算器的各种方法。似乎没有任何工作,所以我希望另一组眼睛会发现我的问题。提前致谢!

+0

难道你在控制台的任何错误,你可以给我们的HTML代码,所以我们可以尝试一下? – Zorken17

+0

控制台上没有出现任何错误,并且我会在稍后提供html。 – Jester

+0

在这里一切都很好......你能再详细描述一下这个问题吗?我没有得到这个部分“而是像这样工作,每次调用函数时,硬币只会从屏幕的顶部放下/添加动画。”这就是所谓的行为...... – Salketer

回答

1

这可能是一个有点晚了,但你需要做的是另外两个旋转增加每个迭代的动画。这将解决你的问题,你可以一次又一次地调用相同的功能。

var rotateDeg = 720; //Declare this outside the function 
 

 
$('button').click(function() { 
 
    coinStack(400); 
 
}) 
 

 
function coinStack(height) { 
 

 
    $("#showCoins").css("display", "inline-block"); 
 
    $("#showCoins").css("top", height + "px"); 
 
    $("#firstCoin").css("display", "inline-block"); 
 
    $("#firstCoin").css("top", "-324px"); 
 
    
 
    $("#firstCoin").clearQueue().delay(1000).animate({ 
 
    top: '10', 
 
    deg: rotateDeg 
 
    }, { 
 
    duration: 3500, 
 
    step: function(now) { 
 
     $("div").css({ 
 
     transform: "rotate(" + now + "deg)" 
 
     }); 
 
    } 
 
    }); 
 
    
 
    rotateDeg += 720; //For every animation you add another 720 degrees 
 
}; 
 

 
$("#secondCoin").css("display", "inline-block"); 
 
$("#secondCoin").css("top", "-324px"); 
 
$("#secondCoin").clearQueue().delay(1400).animate({top: '0', deg: rotateDeg},{ 
 
    duration: 350, 
 
    step: function(now){ 
 
     $("#secondCoin").css({ transform: "rotate(" + now + "deg)" }); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div id="secondCoin"><img class="coin" src="#" alt="Second Coin Drops" /></div> 
 
<div id="firstCoin"><img class="coin" src="#" alt="First Coin Drops" /></div> 
 
<div id="showCoins"><img class="coin-stack" src="#" alt="Stack of Coins" /></div> 
 

 
<button>Push to rotate</button>

+0

聪明的人,Zorken。每次调用函数时增加旋转仍然令人难以置信的是,它允许它旋转。特别是因为我使用了至少4种不同的变化/复位的变换属性,该属性值程度的手段,但我会接受它,如果它的工作原理。谢谢! – Jester

+0

因为你正在做的两个整圈,你可以要么只是增加两个(720度)或'$去除元素梃(你的元素).removeAttr(“风格”)'重置现在变量的函数。 – Zorken17

+0

这是很不幸的我曾经试图解决我的问题,开始用的方法之一。我完全没有尝试改变甚至完全消除风格。我只是满意你的想法最终成功,在所有其他尝试失败了我。再次感谢! – Jester

0

无论页面状态如何,它看起来像你在执行你的代码。

尝试插入这里面代码:

$(document).ready(function(){ 
//your code here 
}) 

欲了解更多信息,请参阅: https://learn.jquery.com/using-jquery-core/document-ready/

+0

这不是问题。我有一个在$(document).ready中调用的函数,另一个调用另一个调用它的函数。 initFutureValue(); - > getFutureValue(); - > coinStack(barHeight); – Jester