【问题标题】:How to place dropdown-content to the right side of the left-navigation on hover如何在悬停时将下拉内容放置在左侧导航的右侧
【发布时间】:2020-05-22 18:44:47
【问题描述】:

所以我有一个左侧导航栏,它在悬停时打开,在里面有一个按钮,在悬停时也有下拉内容。但现在下拉内容出现在按钮下方(左侧导航内)。我希望下拉内容出现在按钮旁边,左侧导航的右侧(所以在左侧导航之外)。

有什么想法吗?

<div id="mySidenav" class="sidenav" style="background-image: linear-gradient(#7C0D0D, #DC0404);">
    <div class="dropdown">
      <button class="dropbtn" [ngStyle]="{'color': 'white','font-style': 'bold', 'font-size': '20.01px', 'font-family':'sans-serif'}">  <img src="assets/icon-overview-white.png" style="padding-right:14px; ">Screening</button>
      <div class="dropdown-content">
        <a href="#">Link 1</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
      </div>
    </div>
  </div>
/* The side navigation menu */
.sidenav {
  height: 70%;
  width: 55px;
  position: fixed;
  z-index:0;
  top:200px;
  left:0px;
  right:50px;
  background-color: #ff0000;
  overflow-x:hidden;
  padding-top: 0px;
  transition: 0.5s;
}
  /* ON HOVER */
  .sidenav:hover {
    width: 250px;
  }
.sidenav normalb:hover {
  background-color: #4F0909;
}
.buttonGroup {
  align-content: baseline;
}

  /* Darker background on mouse-over */
  .btn:hover {
    background-color: #4F0909;
  }
  .sidenav h1 {
    background-color: #850101; /* Blue background */
    border: thin; /* Remove borders */
    color: white; /* White text */
    font-size: 16px; /* Set a font size */
    cursor: pointer; /* Mouse pointer on hover */
    overflow:hidden;
  }
    .sidenav h1:hover {
      background-color: #4F0909;
    }

  /* The navigation menu links */
  .sidenav a {
    padding-left:10px;
    text-decoration: none;
    font-size: 18px;
    color: #f1f1f1;
    display:block;
    transition: 0.3s
  }

  .sidenav a:hover, .offcanvas a:focus {
    color: #f1f1f1;
  }

#main {
  transition: margin-left .5s;
  padding: 20px;
}
.dropdown {

  position: relative;
  display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #640303;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index:1;  
}

  /* Links inside the dropdown */
  .dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
  }

    /* Change color of dropdown links on hover */
    .dropdown-content a:hover {
      background-color: #D90909;
    }

/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
  display: block;
}

/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
  background-color: #D60707;
}

【问题讨论】:

    标签: javascript html css angular


    【解决方案1】:

    如果您确定左侧导航栏的宽度,则可以设置.dropdown-contentleft 属性。由于dropdown-contentabsolute,因此设置left 值会将下拉菜单向右移动。

    .dropdown {
      position: relative;
      display: inline-block;
    }
    
    
    /* Dropdown Content (Hidden by Default) */
    
    .dropdown-content {
      display: none;
      position: absolute;
      background-color: #640303;
      min-width: 160px;
      box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
      z-index: 1;
      top:0;
      left: 144px;
      /* Here I have set the width (minus padding) of the dropdown-content */
    }
    
    
    /* Links inside the dropdown */
    
    .dropdown-content a {
      color: black;
      padding: 12px 16px;
      text-decoration: none;
      display: block;
    }
    
    
    /* Change color of dropdown links on hover */
    
    .dropdown-content a:hover {
      background-color: #D90909;
    }
    
    
    /* Show the dropdown menu on hover */
    
    .dropdown:hover .dropdown-content {
      display: block;
    }
    
    
    /* Change the background color of the dropdown button when the dropdown content is shown */
    
    .dropdown:hover .dropbtn {
      background-color: #D60707;
    }
    <div id="mySidenav" class="sidenav" style="background-image: linear-gradient(#7C0D0D, #DC0404);">
      <div class="dropdown">
        <button class="dropbtn" style="color: white;font-style: bold; font-size: 20.01px; font-family:sans-serif;">  <img src="assets/icon-overview-white.png" style="padding-right:14px; ">Screening</button>
        <div class="dropdown-content">
          <a href="#">Link 1</a>
          <a href="#">Link 2</a>
          <a href="#">Link 3</a>
        </div>
      </div>
    </div>

    【讨论】:

    • 一旦我编辑了 left: 属性,下拉内容的一部分就会消失在 sidenav 中。我需要它出现在侧面导航的右侧(外侧)。
    • 尝试将top:0也设置为.dropdown-content。检查更新的答案。
    • 我现在试过了,但它仍然出现在侧导航中。
    • 你检查了我附加的代码 sn-p 吗?这是您预期的行为吗?
    • 知道了,只需将溢出更改为导航可见。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 2014-09-13
    相关资源
    最近更新 更多