css 如何在向上的方向上改变元素的高度,并使上面的邻居做出相应的React?

u3r8eeie  于 2023-04-08  发布在  React
关注(0)|答案(1)|浏览(157)

这里有一个可折叠部分的列表。当一个部分打开时,它需要向上移动(改变高度),这样它上面的元素会移动,而下面的元素不会移动。注意,上面的元素溢出是可以接受的。
不能使用absolute,因为它不会将元素推到它上面。还要注意,打开时部分内容的高度是动态的,并且是未知的。
默认情况下,当一个部分的内容被设置为关闭时不显示,打开时不显示,但它会移动它下面的部分。
有没有其他方法可以改变方向?
尝试在显示要打开的部分的内容时将位置设置为absolute,但这不是一个解决方案。
下面的代码片段仅供参考。真实的的应用程序是在react中,但功能是相同的。

打开中间部分应移动第一部分,而不是最后一部分

  1. const allCollapsibleSections = document.querySelectorAll('[data-js="collapsible-section"]')
  2. const setIsOpenFunctions = [];
  3. allCollapsibleSections.forEach((collapsibleSection, index) => {
  4. const openCloseButton = collapsibleSection.querySelector('button');
  5. const section = collapsibleSection.querySelector('section');
  6. let open = section.getAttribute('open') || false;
  7. const renderSection = ()=>{
  8. if(open){
  9. section.style.display = 'unset';
  10. return;
  11. }
  12. section.style.display = 'none';
  13. }
  14. const toggleIsOpen = () => {
  15. open = !open;
  16. renderSection();
  17. }
  18. renderSection();
  19. setIsOpenFunctions[index] = toggleIsOpen;
  20. openCloseButton.addEventListener('click', ()=>{
  21. setIsOpenFunctions[index]();
  22. });
  23. })
  1. <div>
  2. <div style="display: flex; flex-direction: row;" data-js="collapsible-section">
  3. <button>Open/Close</button>
  4. <section style="background: yellow; height: 50px">
  5. This is a section
  6. </section>
  7. </div>
  8. <div style="display: flex; flex-direction: row;" data-js="collapsible-section">
  9. <button>Open/Close</button>
  10. <section style="background: yellow; height: 50px">
  11. This is a section
  12. </section>
  13. </div>
  14. <div style="display: flex; flex-direction: row;" data-js="collapsible-section">
  15. <button>Open/Close</button>
  16. <section style="background: yellow; height: 50px">
  17. This is a section
  18. </section>
  19. </div>
  20. </div>
42fyovps

42fyovps1#

看起来这是一个XY问题,尝试的解决方案并不是问题的唯一解决方案。
所以一个解决方案是,而不是旨在移动上面的元素,父元素应该有一个最大高度,并可滚动,当一个部分被打开,只是滚动到该部分。

  1. const allCollapsibleSections = document.querySelectorAll('[data-js="collapsible-section"]')
  2. const setIsOpenFunctions = [];
  3. allCollapsibleSections.forEach((collapsibleSection, index) => {
  4. const openCloseButton = collapsibleSection.querySelector('button');
  5. const section = collapsibleSection.querySelector('section');
  6. let open = section.getAttribute('open') || false;
  7. const renderSection = ()=>{
  8. if(open){
  9. section.style.display = 'unset';
  10. section.scrollIntoView({
  11. behavior: "smooth",
  12. block: "end",
  13. inline: "nearest",
  14. });
  15. return;
  16. }
  17. section.style.display = 'none';
  18. }
  19. const toggleIsOpen = () => {
  20. open = !open;
  21. renderSection();
  22. }
  23. renderSection();
  24. setIsOpenFunctions[index] = toggleIsOpen;
  25. openCloseButton.addEventListener('click', ()=>{
  26. setIsOpenFunctions[index]();
  27. });
  28. })
  1. <div style="max-height: 80px; overflow-y: scroll;">
  2. <div style="display: flex; flex-direction: row;" data-js="collapsible-section">
  3. <button>Open/Close</button>
  4. <section style="background: yellow; height: 50px">
  5. This is a section
  6. </section>
  7. </div>
  8. <div style="display: flex; flex-direction: row;" data-js="collapsible-section">
  9. <button>Open/Close</button>
  10. <section style="background: yellow; height: 50px">
  11. This is a section
  12. </section>
  13. </div>
  14. <div style="display: flex; flex-direction: row;" data-js="collapsible-section">
  15. <button>Open/Close</button>
  16. <section style="background: yellow; height: 50px">
  17. This is a section
  18. </section>
  19. </div>
  20. <div style="display: flex; flex-direction: row;" data-js="collapsible-section">
  21. <button>Open/Close</button>
  22. <section style="background: yellow; height: 50px">
  23. This is a section
  24. </section>
  25. </div>
  26. <div style="display: flex; flex-direction: row;" data-js="collapsible-section">
  27. <button>Open/Close</button>
  28. <section style="background: yellow; height: 50px">
  29. This is a section
  30. </section>
  31. </div>
  32. <div style="display: flex; flex-direction: row;" data-js="collapsible-section">
  33. <button>Open/Close</button>
  34. <section style="background: yellow; height: 50px">
  35. This is a section
  36. </section>
  37. </div>
  38. </div>
展开查看全部

相关问题