简体   繁体   English

如何修复这个使用绝对 position 的选项卡布局?

[英]How to fix this tab layout which is using absolute position?

I know how absolute position works but in this case, it is spoiling the layout.我知道绝对 position 的工作原理,但在这种情况下,它破坏了布局。 Sure, I can use the min-height for every screen size using media queries but that's not a full-proof solution as the client could upload any image size.当然,我可以使用媒体查询为每个屏幕尺寸使用min-height ,但这不是一个完整的解决方案,因为客户端可以上传任何图像尺寸。

The content from below overlaps the tab which is using absolute position.下面的内容与使用绝对 position 的选项卡重叠。 I'm using absolute-position so that the animation works.我正在使用绝对位置,以便 animation 工作。 is there a possibility to add a jQuery code to detect the image height and auto apply it or is there a better solution?是否有可能添加 jQuery 代码来检测图像高度并自动应用它,还是有更好的解决方案? How do I solve this problem?我该如何解决这个问题?

JSFiddle: https://jsfiddle.net/6yo5b198/ JSFiddle:https://jsfiddle.net/6yo5b198/

HTML: HTML:

<div class="modules">
    <div class="container">
        <div class="row">
            <div class="col-md-12 col-12">
        <div id="exTab1">
          <div class="row">
            <div class="col-md-3 col-12">
              <ul class="nav nav-pills">
                <li class="active">
                  <a class="nav-link active" href="#a1a" data-toggle="tab">Overview</a>
                </li>
                <li><a class="nav-link " href="#a2a" data-toggle="tab">About Us</a>
                </li>
                <li><a class="nav-link " href="#a3a" data-toggle="tab">Services</a>
                </li>
              </ul>
            </div>
            <div class="col-md-9 col-12">
              <div class="tab-content clearfix">
                <div class="tab-pane active" id="a1a">
                  <img src="https://i.postimg.cc/xjFVtP8v/1511048-810098669070876-8091817687576561426-n.jpg">
                </div>
                <div class="tab-pane" id="a2a">
                  <img src="https://i.postimg.cc/d3XM3JpH/198582c8c440a3a96817d79a6dc28731-640x469.jpg">
                </div>
                <div class="tab-pane" id="a3a">
                  <img src="https://i.postimg.cc/qMhVYC69/50445941-10213980102716788-1077889899119509504-n.jpg">
                </div>
              </div>
            </div>
          </div>
        </div>
            </div>
        </div>
    </div>
</div>
<div class="">
  <h1>Other web contents go here.</h1>
</div>

CSS: CSS:

.tab-content .tab-pane img {
  opacity: 0;
  -moz-transform: translateX(50px);
  -ms-transform: translateX(50px);
  -o-transform: translateX(50px);
  -webkit-transform: translateX(50px);
  transform: translateX(50px);
  transition: 1s all cubic-bezier(0.075, 0.82, 0.165, 1);
  max-width: 100%;
  height: auto;
}
.tab-content .active img {
  opacity: 1;
  transform: translate(0);
}
#exTab1 .tab-content {
  overflow: hidden;
}
#exTab1 .tab-content .tab-pane {
  display: block;
  position: absolute;
  overflow: hidden;
}

You don't want min-height , because you don't know the height at each responsive level (on mobile devices it might be lower).您不想要min-height ,因为您不知道每个响应级别的高度(在移动设备上可能会更低)。 What you want is for the .active tab to set the height of the tabs.您想要的是.active选项卡设置选项卡的高度。 To do that, you need to include it in document flow, by setting its position back to static .为此,您需要将其包含在文档流中,方法是将其position设置回static

#exTab1 .tab-content .tab-pane.active {
  position: static;
}

This also requires you to add a top: 0 to the other ones.这还需要您将top: 0添加到其他的。 Without it, the tabs after the currently active ones will not be positioned at the top of tabs container, but at the bottom of the active one.没有它,当前活动选项卡之后的选项卡将不会位于选项卡容器的顶部,而是位于活动选项卡的底部。

Your updated fiddle: https://jsfiddle.net/websiter/grkn493p/您更新的小提琴: https://jsfiddle.net/websiter/grkn493p/


Note: if you also want the change in container height to be smooth, you have to use height on the container and transition it each time a tab changes, by reading it from the height of the current tab.注意:如果您还希望容器高度的变化平滑,则必须在容器上使用height并在每次选项卡更改时转换它,方法是从当前选项卡的高度读取它。

Set the transition:设置过渡:

#exTab1 .tab-content {
  transition: height .3s cubic-bezier(.4,0,.2,1);
}

Get the active tab's height when the tab changes and set it on the container: Get the active tab's height when the tab changes and set it on the container:

$(function() {
// on document ready...

  $('.nav').on('shown.bs.tab', function(e){
  // on every `shown.bs.tab` triggered on a `.nav`, run this code:

    const $target = $($(e.target).attr('href'));
    //find target from `href` tab attribute

    $target.is('div') && $target.closest('.tab-content').css({
    // if target is div, set it's css to

      height: $target.height()
      // the height of target (defined above)

    })
  });
});

Updated fiddle: https://jsfiddle.net/websiter/uz0fkrj2/更新小提琴: https://jsfiddle.net/websiter/uz0fkrj2/

Do note the updated fiddle was taken a step further.请注意更新的小提琴更进一步。 I noticed the set height on container doesn't get updated when the page is resized.我注意到调整页面大小时容器上的设置高度没有更新。 Which poses two distinct problems:这带来了两个明显的问题:

  • a) the function to update the height needs to change into target agnostic (because we don't know the target in the resize event). a) function 更新高度需要更改为与目标无关(因为我们不知道resize事件中的目标)。 But that's easily fixable, as the target is the currently .active tab panel.但这很容易解决,因为目标是当前的.active选项卡面板。
  • b) window.resize is triggered way too often and setting the height of each tab-container in the page so often will result in performance issues (the page animations will stagger). b) window.resize的触发方式过于频繁,并且经常设置页面中每个选项卡容器的高度会导致性能问题(页面动画会错开)。 To avoid that, I throttle -ed the update function (not allow it to run more often than x milliseconds).为了避免这种情况,我对更新throttle进行了限制(不允许它运行的频率超过x毫秒)。 I imported lodash and used its _.throttle method - there is a jQuery equivalent but I'm not sure about its syntax - never used it.我导入了 lodash 并使用了它的_.throttle方法——有一个 jQuery 等价物,但我不确定它的语法——从未使用过。

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

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