简体   繁体   English

改变高度 (CSS/JS)

[英]Change Height (CSS/JS)

Whats wrong with my code?我的代码有什么问题? See Tabs 1-2, and then Tab 3.请参阅选项卡 1-2,然后是选项卡 3。

I recently posted this question: 'How can i change the height of active tabs dynamically?'我最近发布了这个问题:“如何动态更改活动选项卡的高度?” and received the following code.并收到以下代码。

As you can see, tab 1 and 2 are working fine.如您所见,选项卡 1 和 2 工作正常。 (Posted original question with 2 tabs. But after adding more tabs however, the dynamical change of height is not working anymore. Any suggestions? (发布带有 2 个选项卡的原始问题。但是在添加更多选项卡之后,高度的动态变化不再起作用。有什么建议吗?

Much appreciated非常感激

 document.getElementById('tab').style.height = document.querySelector(`#tab div`).getBoundingClientRect().height + 'px' const btn = [].slice.call(document.getElementsByTagName('button')) btn.forEach((item, index) => { item.addEventListener('click',function(){ btn.forEach((item) => {item.classList.remove('active')}) item.classList.add('active') document.getElementById('tab').setAttribute('data-tab', index) const currentTab = index === 0? document.querySelector(`#tab div`): document.querySelector(`#tab div + div`) document.getElementById('tab').style.height = currentTab.getBoundingClientRect().height + 'px' }) } )
 .wrapper { overflow: hidden; }.tabs { position: relative; -webkit-transition: all.9s ease-in-out; -moz-transition: all.9s ease-in-out; -ms-transition: all.9s ease-in-out; -o-transition: all.9s ease-in-out; transition: all.9s ease-in-out; }.active { color:blue; }.tabs> * { width: 100%; }.tabs[data-tab='1'] { transform: translateX(-100%); }.tabs[data-tab='2'] { transform: translateX(-200%); }.tabs[data-tab='3'] { transform: translateX(-300%); }.tabs[data-tab='4'] { transform: translateX(-400%); }.inliner { white-space: nowrap; }.inliner > * { display: inline-block; *display: inline; *zoom: 1; font-size: 1rem; letter-spacing: normal; vertical-align: top; word-spacing: normal; white-space: normal; }
 <div class="wrapper"> <button> Tab 1</button> <button> Tab 2</button> <button> Tab3</button> <div id="tab" class="tabs inliner"> <div> <h2>Content 1 </h2> </div> <div> <h2>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam </h2> </div> <div> <h3>TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT</h3> </div> </div> </div> <p>The Height of this text does not keep changing dynamically when clicking Tab no3</p>

I think you should make the variable currentTab point to the correct current tab.just like this我认为你应该让变量currentTab指向正确的当前选项卡。就像这样

const currentTab = document.querySelector(`#tab div:nth-of-type(${index + 1})`)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM