如何在悬停时使下拉菜单悬停并将背景窗口设置为透明?

时间:2016-10-13 04:19:41

标签: html css html5 css3

如何在悬停时使下拉菜单悬停并将背景窗口设置为透明?

像这样:

enter image description here

和下拉菜单悬停如下:

enter image description here

因此,我想在下拉菜单悬停时将背景窗口设置为透明,如背景模式。

$(document).ready(function(){
    $(".dropdown").hover(function(){
        $(".backdrop").show();
        }, function(){
        $(".backdrop").hide();
    });
});
.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropdown {
    position: relative;
    display: inline-block;
    z-index: 101;
}

.dropdown-content {
    display: none;
    position: absolute;
    right: 0;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

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

.dropdown-content a:hover {background-color: #f1f1f1}

.dropdown:hover .dropdown-content {
    display: block;
}

.dropdown:hover .dropbtn {
    background-color: #3e8e41;
}
.backdrop {
  background-color:rgba(0,0,0,0.5);
   opacity: 0.5;
   height: 100%;
   left: 0;
   position: fixed;
   top: 0;
   width: 100%;
   z-index:100;
   display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="dropdown" style="float:left;">
  <button class="dropbtn">Left</button>
  <div class="dropdown-content" style="left:0;">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

<div class="backdrop"></div>

2 个答案:

答案 0 :(得分:5)

$(document).ready(function(){
    $(".dropdown").hover(function(){
        $(".backdrop").show();
        }, function(){
        $(".backdrop").hide();
    });
});
.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropdown {
    position: relative;
    display: inline-block;
    z-index: 101;
}

.dropdown-content {
    display: none;
    position: absolute;
    right: 0;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

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

.dropdown-content a:hover {background-color: #f1f1f1}

.dropdown:hover .dropdown-content {
    display: block;
}

.dropdown:hover .dropbtn {
    background-color: #3e8e41;
}
.backdrop {
  background-color:rgba(0,0,0,0.5);
   opacity: 0.5;
   height: 100%;
   left: 0;
   position: fixed;
   top: 0;
   width: 100%;
   z-index:100;
   display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="dropdown" style="float:left;">
  <button class="dropbtn">Left</button>
  <div class="dropdown-content" style="left:0;">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

<div class="backdrop"></div>

答案 1 :(得分:0)

或者只是简单地使用css - 感谢代码@ariefbruce

.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropdown {
    position: relative;
    display: inline-block;
    z-index: 101;
}

.dropdown-content {
    display: none;
    position: absolute;
    right: 0;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

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

.dropdown-content a:hover {background-color: #f1f1f1}

.dropdown:hover .dropdown-content {
    display: block;
}

.dropdown:hover .dropbtn {
    background-color: #3e8e41;
}
.backdrop {
  background-color:rgba(0,0,0,0.5);
   opacity: 0.5;
   height: 100%;
   left: 0;
   position: fixed;
   top: 0;
   width: 100%;
   z-index:-1;
   display: none;
}
.dropdown:hover > .backdrop{display: block;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="dropdown" style="float:left;">
  <button class="dropbtn">Left</button>
  <div class="dropdown-content" style="left:0;">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
  <div class="backdrop"></div>
</div>