2017-03-06 50 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