如何动态定位绝对div?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (43)

假设我的页面宽度为1000像素,我的标题包含10个水平导航元素justify-content: space-between。如果我悬停nav 1链接,绝对菜单div将显示宽度特定宽度left: 0,如果我悬停nav 2,另一个菜单div将仍然显示left: 0。但是当我悬停nav 7或者nav 8,我怎样才能自动使菜单对齐(right: 0)?

这是我的html&css的代码片段:

<nav>
  <ul>
    <li>
      <a href="#">Nav menu1</a>
      <div class="menu">Content menu 1</div>
    </li>
    <li>
      <a href="#">Nav menu2</a>
      <div class="menu">Content menu 2</div>
    </li>
    <li>
      <a href="#">Nav menu3</a>
      <div class="menu">Content menu 3</div>
    </li>
    <li>
      <a href="#">Nav menu4</a>
      <div class="menu">Content menu 4</div>
    </li>
    <li>
      <a href="#">Nav menu5</a>
      <div class="menu">Content menu 5</div>
    </li>
    <li>
      <a href="#">Nav menu6</a>
      <div class="menu">Content menu 6</div>
    </li>
    <li>
      <a href="#">Nav menu7</a>
      <div class="menu">Content menu 7</div>
    </li>
    <li>
      <a href="#">Nav menu8</a>
      <div class="menu">Content menu 8</div>
    </li>
    <li>
      <a href="#">Nav menu9</a>
      <div class="menu">Content menu 9</div>
    </li>
    <li>
      <a href="#">Nav menu10</a>
      <div class="menu">Content menu 10</div>
    </li>
  </ul>
</nav>


ul {
    display: flex;
    font-size: 15px;
    height: 100%;
    border: solid 1px #ccc;
    max-width: 1200px;
    margin: 0 auto;
    padding: 24px;
    align-items: center;
    justify-content: space-between;
}

ul > li {
  display: flex;
  position: relative;
  cursor: pointer;
  padding-left: 6px;
  padding-right: 6px;
  height: 40px;
  align-items: center;
  white-space: nowrap;
  list-style: none;
}

.menu {
  display: block;
  position: absolute;
  visibility: hidden;
  color: #333;
    width: 500px;
  background: #fff;
  padding: 18px;
  min-height: 80px;
  box-shadow: 0px 2px 1px -1px rgba(0,0,0,0.2), 0px 1px 1px 0px rgba(0,0,0,0.14), 0px 1px 3px 0px rgba(0,0,0,0.12);
  opacity: 0;
  transition: all .4s ease;
  transition-delay: .3s;
  left: 0;
  top: 140%;
  z-index: 99999;
}

ul > li:hover .menu {
    display: block;
    visibility: visible;
    opacity: 1;
    top: 100%;
}
提问于
用户回答回答于

您的问题缺少很多信息,包括您正在使用的代码。看来你可能正在寻找第n个子伪类来定义一个范围。

以下是通用的,因为您没有提供HTML示例,并且会影响第6到第10个孩子。

CSS:

 li:nth-child(n+6):nth-child(-n+10){
 right:0;
 }

编辑:现在更新的答案问题更清楚......

通过添加(vanilla)Javascript查看以下示例。

<!doctype html>
<html>
<head>

<style>
ul{
display:flex;
  font-size:15px;
  height:100%;
  border:solid 1px #ccc;
  max-width:1200px;
  margin:0 auto;
  padding:24px;
  align-items:center;
  justify-content:space-between;
}
ul > li{
  display:flex;
  position:relative;
  cursor:pointer;
  padding-left:6px;
  padding-right:6px;
  height:40px;
  align-items:center;
  white-space:nowrap;
  list-style:none;
}
.menu{
  display:block;
  position:absolute;
  visibility:hidden;
  color:#333;
  width:500px;
  background:#fff;
  padding:18px;
  min-height:80px;
  box-shadow:0px 2px 1px -1px rgba(0,0,0,0.2), 0px 1px 1px 0px rgba(0,0,0,0.14), 0px 1px 3px 0px rgba(0,0,0,0.12);
  opacity:0;
  transition:all .4s ease;
  transition-delay:.3s;
  top:140%;
}
ul > li:hover .menu{
  display:block;
  visibility:visible;
  opacity:1;
  top:100%;
}
</style>

</head>
<body>

<nav>
 <ul>
  <li>
    <a href="#">Nav menu1</a>
    <div class="menu">Content menu 1</div>
  </li><li>
    <a href="#">Nav menu2</a>
    <div class="menu">Content menu 2</div>
  </li><li>
    <a href="#">Nav menu3</a>
    <div class="menu">Content menu 3</div>
  </li><li>
    <a href="#">Nav menu4</a>
    <div class="menu">Content menu 4</div>
  </li><li>
    <a href="#">Nav menu5</a>
    <div class="menu">Content menu 5</div>
  </li><li>
    <a href="#">Nav menu6</a>
    <div class="menu">Content menu 6</div>
  </li><li>
    <a href="#">Nav menu7</a>
    <div class="menu">Content menu 7</div>
  </li><li>
    <a href="#">Nav menu8</a>
    <div class="menu">Content menu 8</div>
  </li><li>
    <a href="#">Nav menu9</a>
    <div class="menu">Content menu 9</div>
  </li><li>
    <a href="#">Nav menu10</a>
    <div class="menu">Content menu 10</div>
  </li>
 </ul>
</nav>

<script>
var elems=document.querySelectorAll('div.menu');  //Get NodeList of elements
elems.forEach(function(elem){                     //Iterate
var pos=elem.getBoundingClientRect();             //Get element info
let stat=document.elementFromPoint(pos.right,0);  //Check position
if(stat===null){                                  //Will be null if out of viewport
elem.style.right='0';                             //Add the right:0; style
}else{
elem.style.left='0';                              //Add the left:0; style
}});
</script>

</body>
</html>

编辑2:根据要求,只需更改JavaScript以检查容器。

现在,这将与<ul>文档中找到的第一个匹配。你可能想要<ul id="cont">和id匹配:

<script>
//var cont=document.getElementById('cont');       //Match by ID
var cont=document.querySelector('ul');            //Match first <ul>

var posc=cont.getBoundingClientRect();            //Get container info
var elems=document.querySelectorAll('div.menu');  //Get NodeList of elements
elems.forEach(function(elem){                     //Iterate
var posv=elem.getBoundingClientRect();            //Get element info
var offset=posc.right-posv.right;                 //Calculate difference
if(offset<0){                                     //Will be <0 if out of container
elem.style.right='0';                             //Add the right:0; style
}else{
elem.style.left='0';                              //Add the left:0; style
}});
</script>

扫码关注云+社区

领取腾讯云代金券