【问题标题】:CSS: Wrong element width and position IE11 (flexbox, position, transform)CSS:错误的元素宽度和位置 IE11(flexbox、位置、变换)
【发布时间】:2020-06-28 17:01:23
【问题描述】:

我在 Internet Explorer 11 中的飞行数小时以来一直在苦苦挣扎。

我有一个带有两个子元素的header。我用flexbox space-between 显示它们。第二个元素(在标题的右侧)具有position relative 和一个具有position absolute 的子元素。单击时,我使用transform translateY 为元素的位置设置动画,并使用transform translateY,因此它将直接滑入标题中右侧元素的下方。这适用于除 IE11 之外的所有浏览器,其中 flyin 位于右侧(垂直溢出可见)并且宽度不包括最后一个元素。

当我在元素.header__selection 上使用padding-right: 100px; 时,浮出控件位于视口右侧,框宽度覆盖所有元素。此外,当我在父元素上删除 position relative 时,框宽度是正确的。我不知道这里有什么问题。我猜位置和弹性盒有一些问题。希望有一些意见。

使用 sn-p 在不同的浏览器上进行测试。这是它在 IE11 上的屏幕截图:

const selection = document.querySelector('.header__selection');
const flyin = document.querySelector('.header__selection-flyin');

selection.addEventListener('click', function() {
  flyin.classList.toggle('state-header__selection-flyin--open');
});
body {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.header {
  background-color: white;
  border-bottom: 1px solid gray;
  
  display: flex;
  justify-content: space-between;
  align-items: center;
  
  height: 60px;
  width: 100%;
}

.header__selection {
  position: relative;
}

.header__selection-flyin {
  display: flex;
  background-color: lightcoral;
  border: 1px solid black;
  position: absolute;
  transform: translateY(-100%);
  transition: transform 0.3s ease-in-out;
  right: 0;
  z-index: -1;
}

.header__selection-flyin-item {
  padding: 5px 10px;
}

.state-header__selection-flyin--open {
  transform: translateY(100%);
  z-index: 1;
  transition: transform 0.3s ease-in-out, z-index 0.3s 0.2s;
}
  
<header class="header">
  <div class="header__logo">Logo</div>
  <div class="header__selection">
    <div class="header__selection-active">
      Selected Item
    </div>
    <div class="header__selection-flyin">
      <div class="header__selection-flyin-item">Item</div>
      <div class="header__selection-flyin-item">Item</div>
      <div class="header__selection-flyin-item">Item</div>
    </div>
  </div>
</header>

【问题讨论】:

    标签: css flexbox position internet-explorer-11 css-transforms


    【解决方案1】:

    我尝试使用 IE 11 浏览器测试问题,我可以使用您的代码产生问题。

    我尝试检查 CSS 代码,发现如果在 .header__selection-flyin 中将 positionabsolute 更改为 fixed 类然后问题可以解决 IE 11 浏览器。

     position: relative;
     to
     position: fixed;
    

    修改代码:

    const selection = document.querySelector('.header__selection');
    const flyin = document.querySelector('.header__selection-flyin');
    
    selection.addEventListener('click', function() {
      flyin.classList.toggle('state-header__selection-flyin--open');
    });
    body {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }
    
    .header {
      background-color: white;
      border-bottom: 1px solid gray;
      
      display: flex;
      justify-content: space-between;
      align-items: center;
     
      height: 60px;
      width: 100%;
    
    }
    
    .header__selection {
      position: relative;
    
    }
    
    .header__selection-flyin {
      display: flex;
      background-color: lightcoral;
      border: 1px solid black;
      position: fixed;
      transform: translateY(-100%);
      transition: transform 0.3s ease-in-out;
      right: 0;
      z-index: -1;
    
    }
    
    .header__selection-flyin-item {
      padding: 5px 10px;
    
    }
    
    .state-header__selection-flyin--open {
      transform: translateY(100%);
      z-index: 1;
      transition: transform 0.3s ease-in-out, z-index 0.3s 0.2s;
    }
    <header class="header">
      <div class="header__logo">Logo</div>
      <div class="header__selection">
        <div class="header__selection-active">
          Selected Item
        </div>
        <div class="header__selection-flyin">
          <div class="header__selection-flyin-item">Item</div>
          <div class="header__selection-flyin-item">Item</div>
          <div class="header__selection-flyin-item">Item</div>
        </div>
      </div>
    </header>

    在 IE 11 浏览器中的输出:

    【讨论】:

    • 这意味着我的标题保持固定,如果我在页面较大时向下滚动?如果是这样,这不是一个可能的解决方案,因为我的标题不必固定在顶部。我有一个有效的解决方案,稍后会在这里发布。无论如何谢谢!
    • 感谢您告知我们问题的状态。由于您已经找到了解决方案,我建议您发布您的解决方案并尝试将其标记为该问题的答案。它可以在未来帮助其他社区成员解决类似的问题。感谢您的理解。
    猜你喜欢
    • 1970-01-01
    • 2021-07-23
    • 2018-10-22
    • 1970-01-01
    • 2020-12-22
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多