`每當我展開巢狀的可折疊項目時,其父級的可見性都不會正確計算。螢幕上遺漏了一些巢狀的內容
這是處理滾動高度的 html 程式碼和 js 程式碼
// Get all collapsible buttons const collapsibles = document.getElementsByClassName('collapsible'); // Add click event listener for each collapsible for (let i = 0; i < collapsibles.length; i++) { collapsibles[i].addEventListener('click', function() { this.classList.toggle('active'); const content = this.nextElementSibling; if (content.style.maxHeight) { content.style.maxHeight = null; } else { content.style.maxHeight = content.scrollHeight + 'px'; } // Get all nested collapsible buttons within this collapsible const nestedCollapsibles = content.getElementsByClassName('nested-collapsible'); // Loop through nested collapsibles and expand or collapse them accordingly for (let j = 0; j < nestedCollapsibles.length; j++) { const nestedContent = nestedCollapsibles[j].nextElementSibling; if (this.classList.contains('active')) { if (!nestedContent.style.maxHeight) { nestedContent.style.maxHeight = nestedContent.scrollHeight + 'px'; content.style.maxHeight = (parseInt(content.style.maxHeight) + nestedContent.scrollHeight) + 'px'; // Add nested collapsible's scroll height to parent collapsible's scroll height } else { nestedContent.style.maxHeight = null; content.style.maxHeight = (parseInt(content.style.maxHeight) - nestedContent.scrollHeight) + 'px'; // Subtract nested collapsible's scroll height from parent collapsible's scroll height } } } }); }
.active { color:blue; }
<div id="HotLikeMiami"> <div class="HLM"> <div class="HLMRow"> <div class="HLMTitle"> <h1 class="sub-title">Fantastic League</h1> </div> <div class="HLMImg"> <img src="fantasticLeague.png"> </div> <div class="HLMDetails"> <p>A 2D Top Down Shooter game which heavily focuses on Enemy AI. I have made an attempt to build this project from ground up. I have used SFML for rendering shapes and textures, the rest is made from scratch. The project involves the integration of complex enemy AI. A simple Data-Oriented Design approach has been followed in this project for handling different events and messages by the enemies. </p> </div> <div class="MyContibution"> <button class="collapsible">My Contribution</button> <div class="ContributionContent"> <p>Got a lot of contribution</p> <button class="collapsible nested-collapsible">My Contribution</button> <div class="ContributionContent"> <p>1. This process involves the development and integration of states like Game Menu, Splash Screen, InGame, Game Over etc states. These states are fetched with delta time which is calculated from the Game Loop. 2. This stage also includes the development of Asset Manager, Input Manager etc</p> </div> <button class="collapsible nested-collapsible">My Contribution</button> <div class="ContributionContent"> <p>1. This process involves the development and integration of states like Game Menu, Splash Screen, InGame, Game Over etc states. These states are fetched with delta time which is calculated from the Game Loop. 2. This stage also includes the development of Asset Manager, Input Manager etc</p> </div> <button class="collapsible nested-collapsible">My Contribution</button> <div class="ContributionContent"> <p>1. This process involves the development and integration of states like Game Menu, Splash Screen, InGame, Game Over etc states. These states are fetched with delta time which is calculated from the Game Loop. 2. This stage also includes the development of Asset Manager, Input Manager etc</p> </div> </div> </div> </div> </div> </div>``` </kbd>
我需要計算方面的幫助以及影響可見性的原因
我希望所有巢狀都是獨立的,並且每當展開時它們都會增加父級可見性的高度 這裡最後一個嵌套被剪掉:
我已經修改了您的程式碼,以便能夠在動態高度和回應能力下正常工作。
先檢查一下您是否正在展開或折疊。我們可以透過取得
classList.toggle
的結果來弄清楚這一點,如果新增了類,則為true
,如果刪除,則為false
。 p>我在每個
ContributionContent
內部新增了一個包裝器。此包裝器將負責計算內部內容的總高度。這是必要的,因為ContributionContent
的高度會波動。監聽
transitionend
事件,將最大高度設為none
。僅當手風琴展開時才設置高度。關閉0px
時應予以尊重。由於展開時最大高度設定為
none
,因此我們必須使用雙重請求動畫影格(Double RAF)才能先設定高度,然後再轉換到0px關閉時。這是一種在渲染發生後執行任務的(hacky)技術。請參閱有關 Double RAF 的此帖子。