我想要一個容器下來,當我點擊我的菜單中的項目。動畫向下工作正常。但是當我點擊菜單中的其他項目時,它不會向上播放動畫。類刪除不工作的過渡
的CSS:
.card{
width: 100%;
background: blue;
transform: translateY(-100px);
opacity: 0;
height:0;
min-height: 0;
transition-timing-function: cubic-bezier(.175,.885,.32,1.275);
transition-property: opacity,transform;
transition-duration: 1s;
}
.card-appeared{
margin-top: 0;
opacity: 1;
transform: translateY(0);
min-height: 300px;
transition-delay: 1s;
height:auto;
width: 100%;
}
HTML:
<div id="aboutme" class="container card ">
About me
</div>
<div id="gallery" class="container card card-appeared">
Gallery
</div>
添加和移除類
function appear(child){
parent.classList.remove("card-appeared");
let others = document.getElementsByClassName("card-appeared");
for(var i = 0; i < others.length;i++){
others[i].classList.remove("card-appeared");
}
child.classList.add("card-appeared");
}
function dissapear(child) {
child.classList.remove("card-appeared");
parent.classList.add("card-appeared");
}
others
基本的JavaScript是其他卡在頁面列表和父第一個容器。
如果您需要任何其他代碼,請讓我知道。我似乎無法獲得向上的動畫效果,但動畫效果不起作用。
謝謝。
謝謝你的簡要解釋。在代碼中給出的解決方案並不真正按照它應該的方式工作(在第一個容器上出現半屏)。絕對位置技巧確實奏效。可悲的是,我不是一個由於文檔流而絕對定位的粉絲,但是在這種情況下不好使用它! – Ellisan
@Ellisan絕對定位非常適合您需要在動態環境下顯示出現場外內容的情況。所以,不要猶豫,在必要時使用絕對定位 - 如果你餵它,照顧好它,並記住要散步 - 它會做你的權利;) – UncaughtTypeError
@Ellisan關於最初的解決方案,您可能會發現需要調整'transform:translateY()'屬性值以適合您特定環境的要求 - 上述演示已使用問題中提供的代碼進行了測試,並且工作得當 - 儘管我明白可能會有更多在測試此解決方案時,我無法解釋您的產品中的代碼。 – UncaughtTypeError