2014-07-11 173 views
0

我的轉換不起作用,我不知道爲什麼。CSS3轉換不起作用

HTML代碼

<!-- The user can select here the language --> 
<select name="language" id="language" onChange="ChangeLanguage()"> 
    <option value=" " selected/> 
    <option value="Basic"/>Basic 
    <option value="C/C++" />C/C++ 
</select> 

<div id="PartieCPP"> <!-- This <div> disappear when the user choose "Basic" --> 
    ... 
</div> 

<div id="PartieBAS"> <!-- This <div> disappear when the user choose "C/C++" --> 
    ... 
</div> 

JavaScript代碼

function ChangeLanguage() //this function is called when the user change the selected language 
{ 
    var choise = document.getElementById("language").options[document.getElementById("language").selectedIndex].value; 

    if (choise == "C/C++") 
    { 
     document.getElementById("PartieCPP").style.height = "auto"; //"PartieCPP" appear 
     document.getElementById("PartieBAS").style.height = "0px"; //"PartieBAS" disappear 
    } 
    else if (choise == "Basic") 
    { 
     document.getElementById("PartieCPP").style.height = "0px"; //"PartieCPP" disappear 
     document.getElementById("PartieBAS").style.height = "auto"; //"PartieBAS" appear 
    } 
    else 
    { 
     document.getElementById("PartieBAS").style.height = "0px"; //"PartieBAS" disappear 
     document.getElementById("PartieCPP").style.height = "0px"; //"PartieCPP" disappear 
    } 
} 

CSS代碼

#PartieCPP, #PartieBAS 
{ 
    height : 0px; 
    overflow : hidden; 

    -webkit-transition: height 2s; 
    -moz-transition: height 2s; 
    transition: height 2s; 
} 

此代碼工作正常,但沒有過渡,可有人向我解釋我在做什麼錯誤?

(我在Chrome和Firefox上試過)

+0

使2班...... 1,隱藏... 1,顯示...然後切換隱藏的類並關閉。注意:如果您想要兩種效果,請將轉換置於兩者中。 choise = choice :) – zgr024

+1

而不是改變元素上的樣式,只需交換CSS類名稱並讓樣式表完成工作即可。 –

+0

您的HTML有問題... –

回答

1

您需要輸入一個像素值才能進行轉換。你不能從'0px'轉換到'auto'。

這裏有一個link to a demo

這個JavaScript似乎這樣的伎倆:

function ChangeLanguage() //this function is called when the user change the selected language 
{ 
    var choise = document.getElementById("language").options[document.getElementById("language").selectedIndex].value; 

    if (choise == "C/C++") 
    { 
     document.getElementById("PartieCPP").style.height = "50px"; //"PartieCPP" appear 
     document.getElementById("PartieBAS").style.height = "0px"; //"PartieBAS" disappear 
    } 
    else if (choise == "Basic") 
    { 
     document.getElementById("PartieCPP").style.height = "0px"; //"PartieCPP" disappear 
     document.getElementById("PartieBAS").style.height = "50px"; //"PartieBAS" appear 
    } 
    else 
    { 
     document.getElementById("PartieBAS").style.height = "0px"; //"PartieBAS" disappear 
     document.getElementById("PartieCPP").style.height = "0px"; //"PartieCPP" disappear 
    } 
} 
+0

謝謝,它工作的很棒! :) –