Flexbox重叠绝对定位元素

时间:2018-02-13 16:33:55

标签: html css css3 flexbox css-position

我正在尝试使用flexbox创建一个多语言导航栏,使用一些JS来切换我不想要的元素的可见性,但是在使用绝对定位保留布局时遇到了问题。

理想情况下,我希望.language具有绝对定位,因此多语言元素可以无形地重叠,但应用它会覆盖flexbox布局。

.language {
  position: absolute;
}

jsFiddle

function switchLanguage(lang) {
  var languageElements = document.getElementsByClassName('language')
  for (var x in languageElements) {
    if (languageElements[x].getAttribute('lang') != lang) {
      languageElements[x].style.visibility = 'hidden';
    } else {
      languageElements[x].style.visibility = 'visible';
    }
  }
}

switchLanguage('bs')
#menu {
  width: 100vw;
  position: fixed;
  top: 0px;
  text-align: center;
}

ul {
  list-style: none;
}

.container {
  display: flex;
}

.container>li {
  flex: 1;
}

@media all and (max-width: 600px) {
  .container {
    flex-wrap: wrap;
  }
  .container>li {
    flex-basis: 50%;
  }
}

@media all and (max-width: 400px) {
  .container>li {
    flex-basis: 100%;
  }
}

.language {
  z-index: auto;
  position: absolute;
}
<nav id="menu">
  <div class="language" lang="bs">
    <ul class="container">
      <li><a href="#about">O Nama</a></li>
      <li><a href="#hours">Radno Vrijeme</a></li>
      <li><a href="#map">Karta</a></li>
      <li><a href="#">Meni</a></li>
      <li onclick="switchLanguage('en')">English</li>
    </ul>
  </div>
  <div class="language" lang="en">
    <ul class="container">
      <li><a href="#about ">About Us</a></li>
      <li><a href="#hours ">Opening Hours</a></li>
      <li><a href="#map ">Map</a></li>
      <li><a href="# ">Menu</a></li>
      <li onclick="switchLanguage('bs')">Bosanski</li>
    </ul>
  </div>
</nav>

2 个答案:

答案 0 :(得分:2)

您需要将宽度设置为绝对定位元素。如果没有它,它会折叠到内容大小,如果为空,则为零。还建议应用正确的偏移量。

<强> jsFiddle

.language {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
}

答案 1 :(得分:0)

使用css类并在我使用hide类的块之间切换,如下所示

HTML:

  <nav id="menu">
    <div class="language hide" lang="bs">
      <ul class="container">
        <li><a href="#about">O Nama</a></li>
        <li><a href="#hours">Radno Vrijeme</a></li>
        <li><a href="#map">Karta</a></li>
        <li><a href="#">Meni</a></li>
        <li onclick="switchLanguage('en')">English</li>
      </ul>
    </div>

    <div class="language" lang="en">
      <ul class="container">
        <li><a href="#about ">About Us</a></li>
        <li><a href="#hours ">Opening Hours</a></li>
        <li><a href="#map ">Map</a></li>
        <li><a href="# ">Menu</a></li>
        <li onclick="switchLanguage('bs')">Bosanski</li>
      </ul>
    </div>
  </nav>

CSS:

#menu {
  width: 100vw;
  position: fixed;
  top: 0px;
  text-align: center;
}

ul {
  list-style: none;
}

.container {
  display: flex;
}

.container>li {
  flex: 1;
}

@media all and (max-width: 600px) {
  .container {
    flex-wrap: wrap;
  }
  .container>li {
    flex-basis: 50%;
  }
}

@media all and (max-width: 400px) {
  .container>li {
    flex-basis: 100%;
  }
}

.language.hide {
  display:none;
}

JS:修改JSfiddle上的JS来切换类

function switchLanguage(lang) {
  var languageElements = document.getElementsByClassName('language')
  for (var x in languageElements) {
    if (languageElements[x].getAttribute('lang') != lang) {
      console.log(languageElements[x].classList);
      languageElements[x].classList.add("hide");
    } else {
      console.log(languageElements[x].classList);
      languageElements[x].classList.remove("hide");
    }
  }
}

switchLanguage('bs')

请在下面的codepen上找到工作代码: https://codepen.io/YasirKamdar/pen/YexWOX