2015-08-30 104 views
0

這是我第一次在這裏問一個問題,所以請告訴我我做錯了什麼。同時動畫和過渡?

所以我試圖做一個網站,我有按鈕來在頁面加載飛行,然後他們會彈出(縮放更大),當我把鼠標懸停。已經過了2天,我仍然無法找到如何去做的答案。當我使用「向前」填充模式使用動畫時,懸停在動畫之後不起作用。當我使用轉換時,它不會在頁面加載時觸發。同時每個按鍵都會有來自另外一個.06s延遲,當他們在飛

感謝

代碼:。(這是我走到這一步,我用java腳本生成的按鈕,並計算它們的初始位置。)

<!DOCTYPE html> 
 
<html> 
 

 
<head data-gwd-animation-mode="quickMode"> 
 
\t <title id="title">Jack's Desk</title> 
 
\t <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
 
\t <meta name="generator" content="Google Web Designer 1.3.2.0521"> 
 
\t <style type="text/css"> 
 
\t \t html, body { 
 
\t \t width: 100%; 
 
\t \t height: 100%; 
 
\t \t margin: 0px; 
 
\t \t } 
 
\t \t .backgroundImg { 
 
\t \t position: absolute; 
 
\t \t width: 100%; 
 
\t \t height: auto; 
 
\t \t } 
 
\t \t .mainDiv { 
 
\t \t position: absolute; 
 
\t \t width: 100%; 
 
\t \t height: 100%; 
 
\t \t } 
 
\t </style> 
 
</head> 
 

 
<body> 
 
\t 
 
\t <div id="mainDiv" class="mainDiv"> 
 
\t </div> 
 
\t 
 
\t <script style=""> 
 
\t \t const OPACITY = 0.8; 
 
\t \t const TRANSITION_TIME = .4; 
 
\t \t const ANIMATION_TIME = 1.1; 
 
\t \t //For the fly in function. 
 
\t \t var index = 0; 
 
\t \t var delay = 0; 
 
\t \t 
 
\t \t for (var i = 0; i < 60; i++) { 
 
\t \t \t var theButton = document.createElement("Button"); 
 
\t \t \t document.getElementById("mainDiv").appendChild(theButton); 
 
\t \t \t theButton.innerText = "Hola number " + i; 
 
\t \t \t theButton.style.position = "fixed"; 
 
\t \t \t theButton.style.left = Math.random() * window.innerWidth + "px"; 
 
\t \t \t theButton.style.top = Math.random() * window.innerHeight + "px"; 
 
\t \t \t flyIn(theButton, 0.06); 
 
\t \t } 
 
\t \t 
 
\t \t function flyIn(element, additionalDelay) { 
 
\t \t \t var elementTop = element.offsetTop; 
 
\t \t \t var elementLeft = element.offsetLeft; 
 
\t \t \t var centerX = window.innerWidth/2; 
 
\t \t \t var centerY = window.innerHeight/2; 
 
\t \t \t var angle = Math.atan2(centerY - elementTop, elementLeft - centerX); 
 
\t \t \t var newTop = - Math.sin(angle) * 390; 
 
\t \t \t var newLeft = Math.cos(angle) * 390; 
 
\t \t \t var newStyle = document.createElement("Style"); 
 
\t \t \t newStyle.type = "text/css"; 
 
\t \t \t newStyle.innerHTML = ".label" + index 
 
\t \t \t \t + "{opacity:0; -webkit-animation: flyin" + index + " 1.2s forwards; -webkit-animation-delay:" + delay + "s}" 
 
\t \t \t \t + "\n.label" + index + ":hover {-webkit-transform: scale(1.5, 1.5); opacity: 1.0;}" 
 
\t \t \t \t + "\[email protected] flyin" + index + "{\nfrom{\n-webkit-transform: translate(" + newLeft + "px, " + newTop + "px) scale(3, 3);\n opacity: 0}" 
 
\t \t \t \t + "\nto{\n-webkit-transform: translate(0px, 0px) scale(1, 1);\n opacity: " + OPACITY +"\n}" 
 
\t \t \t \t ; 
 
\t \t \t document.getElementsByTagName("head")[0].appendChild(newStyle); 
 
\t \t \t element.className = "label" + index; 
 
\t \t \t 
 
\t \t \t delay += additionalDelay; 
 
\t \t \t index++; 
 
\t \t } 
 
\t </script> 
 
</body> 
 

 
</html>

+0

你可以添加你有什麼代碼嗎?我們需要了解你有什麼動畫/過渡。 – ryachza

+0

添加您的代碼... –

+3

好吧,我添加了代碼。 –

回答

0

所以我還挺發現dirtily骯髒污穢的解決方法(抱歉的語言)是向後設置動畫。但這種方式不會讓我對動畫有任何延遲,因爲這些按鈕將在動畫開始之前顯示。我想我沒有別的選擇。不能太貪心。 :(

<!DOCTYPE html> 
 
<html> 
 

 
<head data-gwd-animation-mode="quickMode"> 
 
\t <title id="title">Jack's Desk</title> 
 
\t <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
 
\t <meta name="generator" content="Google Web Designer 1.3.2.0521"> 
 
\t <style type="text/css"> 
 
\t \t html, body { 
 
\t \t width: 100%; 
 
\t \t height: 100%; 
 
\t \t margin: 0px; 
 
\t \t } 
 
\t \t body { 
 
\t \t -webkit-transform: perspective(1400px) matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); 
 
\t \t -webkit-transform-style: preserve-3d; 
 
\t \t background-color: transparent; 
 
\t \t } 
 
\t \t .backgroundImg { 
 
\t \t position: absolute; 
 
\t \t width: 100%; 
 
\t \t height: auto; 
 
\t \t } 
 
\t \t .mainDiv { 
 
\t \t position: absolute; 
 
\t \t width: 100%; 
 
\t \t height: 100%; 
 
\t \t } 
 
\t </style> 
 
</head> 
 

 
<body> 
 
\t 
 
\t <div id="mainDiv" class="mainDiv"> 
 
\t </div> 
 
\t 
 
\t <script style=""> 
 
\t \t const OPACITY = 0.8; 
 
\t \t const TRANSITION_TIME = .4; 
 
\t \t const ANIMATION_TIME = 1.1; 
 
\t \t //For the fly in function. 
 
\t \t var index = 0; 
 
\t \t var delay = 0; 
 

 
\t \t 
 
\t \t for (var i = 0; i < 60; i++) { 
 
\t \t \t var theLabel = document.createElement("Button"); 
 
\t \t \t document.getElementById("mainDiv").appendChild(theLabel); 
 
\t \t \t theLabel.innerText = "Hola number " + i; 
 
\t \t \t theLabel.style.position = "fixed"; 
 
\t \t \t theLabel.style.left = Math.random() * window.innerWidth + "px"; 
 
\t \t \t theLabel.style.top = Math.random() * window.innerHeight + "px"; 
 
\t \t \t flyIn(theLabel, 0.06); 
 
\t \t } 
 
\t \t 
 
\t \t function flyIn(element, additionalDelay) { 
 
\t \t \t var elementTop = element.offsetTop; 
 
\t \t \t var elementLeft = element.offsetLeft; 
 
\t \t \t var centerX = window.innerWidth/2; 
 
\t \t \t var centerY = window.innerHeight/2; 
 
\t \t \t var angle = Math.atan2(centerY - elementTop, elementLeft - centerX); 
 
\t \t \t var newTop = - Math.sin(angle) * 390; 
 
\t \t \t var newLeft = Math.cos(angle) * 390; 
 
\t \t \t var newStyle = document.createElement("Style"); 
 
\t \t \t newStyle.type = "text/css"; 
 
\t \t \t newStyle.innerHTML = ".label" + index 
 
\t \t \t \t + "{opacity: " + OPACITY + ";-webkit-transition: all " + TRANSITION_TIME + "s;\n" 
 
\t \t \t \t + "-webkit-animation: flyin" + index + " " + ANIMATION_TIME + "2s reverse;}" 
 
\t \t \t \t + "\n.label" + index + ":hover {-webkit-transform: scale(1.5, 1.5); opacity: 1.0;}" 
 
\t \t \t \t + "\[email protected] flyin" + index + "{\n100%{\n-webkit-transform: translate(" + newLeft + "px, " + newTop + "px) scale(3, 3);\n opacity: 0}" 
 
\t \t \t \t + "\n0%{\n-webkit-transform: translate(0px, 0px) scale(1, 1);\n opacity: " + OPACITY + "!important;}" 
 
\t \t \t \t ; 
 
\t \t \t document.getElementsByTagName("head")[0].appendChild(newStyle); 
 
\t \t \t element.className = "label" + index; 
 
\t \t \t //element.innerText = parseInt(angle * 180/Math.PI) + ", (" + newLeft + ", " + newTop +")"; 
 
\t \t \t delay += additionalDelay; 
 
\t \t \t index++; 
 
\t \t } 
 
\t </script> 
 
</body> 
 

 
</html>

0

讓我知道?但我覺得這是你找什麼....

// HTML

<!DOCTYPE html> 
<html> 
<head> 
    <script type="text/javascript" src="index.js"></script> 
    <link rel="stylesheet" href="index.css"> 
</head> 
<body> 
    <section id="main"></section> 
</body> 
</html> 

// JS( index.js)

document.addEventListener("readystatechange", initFly); 

function initFly() { 
    var main = document.getElementById("main"); 
    for (var i=0; i < 60; i++){ 
     // 
     var btn = document.createElement("button"); 
     main.appendChild(btn); 
     btn.innerHTML = "Hola number " + i; 
     btn.style.left = Math.random() * window.innerWidth + "px"; 
     btn.style.top = Math.random() * window.innerHeight + "px"; 
     // 
     var eleTop = btn.offsetTop; 
     var eleLeft = btn.offsetLeft; 
     var centerX = window.innerWidth/2; 
     var centerY = window.innerHeight/2; 
     var angle = Math.atan2(centerY - eleTop, eleLeft - centerX); 
     var newTop = Math.sin(angle) * 390; 
     var newLeft = - Math.cos(angle) * 390; 
     // 
     btn.style.transform = "translate("+newLeft+"px,"+newTop+"px)"; 
     btn.style.opacity = "0.8"; 
    } 
} 

// CSS(index.css)

body { 
    width: 100vw; 
    height: 100vh; 
    margin: 0 !important; 
} 
section{ 
    position: absolute; 
    width: 100vw; 
    height: 100vh; 
}  
button{ 
    opacity: 0; 
    cursor: pointer; 
    position: fixed; 
    transition-duration: 3s; 
    transition-timing-function: ease; 
} 
button:hover{ 
    height: 100px; 
    width: 100px; 
} 
+0

我認爲我的代碼很混亂,因爲newLeft和newTop應該是它們源自的位置,並最終轉化爲(0,0)。另外每個人都會有延遲。 –