javascript - 如何在悬停时将下拉内容放置在左侧导航的右侧

标签 javascript html css angular

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

有什么想法吗?

<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;
}

最佳答案

如果您确定左侧导航栏的宽度,则可以设置 .dropdown-contentleft 属性。由于 dropdown-contentabsolute,设置左侧值会将下拉列表移到右侧。

.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>

关于javascript - 如何在悬停时将下拉内容放置在左侧导航的右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60099741/

相关文章:

javascript - 获取占位符文本的父节点

javascript - 能够处理数组的 QueryString Javascript 集合对象

python - sqlalchemy.exc.OperationalError : (sqlite3. OperationalError) 没有这样的列

html - 为什么 <hr> 在 HTML 中的厚度与 CSS 不同?

javascript - 在自定义服务中通过 Promise Angular 获取值(value)

javascript - 组合单选按钮和文本字段

php - schema.org/Google Rich Snippet 微格式是否违反了 HTML 规则?

html - 如何修复 Mobile-View 的表格?

html - 为什么 <body> 元素上有默认边距?

javascript - 在电子邮件中收到未填写的联系表格