下拉菜单隐藏在图片后面
Posted
技术标签:
【中文标题】下拉菜单隐藏在图片后面【英文标题】:dropdown is hidden behind image 【发布时间】:2019-02-28 18:36:44 【问题描述】:当向下滚动导航栏然后悬停正常工作时,我的下拉菜单隐藏在图像后面(猫头鹰 carousal 之一)。
问题仅显示在图像所在的上部区域。
我尝试通过提供 z-index.通过为导航栏提供比图像滑块更高的值。但这不起作用。
我也尝试过溢出值。
我给了溢出:可见;导航栏,但仍然不可见。
错误下拉菜单的屏幕截图
jQuery(document).ready(function($)
$("#owl-demo").owlCarousel(
navigation : true,
slideSpeed : 300,
paginationSpeed : 500,
items: 1,
singleItem: true,
autoPlay : 4000
);
);
$('body').on('mouseenter mouseleave','.dropdown',function(e)
var _d=$(e.target).closest('.dropdown');
_d.addClass('show');
setTimeout(function()
_d[_d.is(':hover')?'addClass':'removeClass']('show');
$('[data-toggle="dropdown"]', _d).attr('aria-expanded',_d.is(':hover'));
,300);
);
/*sticky header position*/
.sticky.is-sticky
position: fixed;
left: 0;
right: 0;
top: 0;
z-index: 999;
width: 100%;
header
background: transparent !important;
/*body */
/*min-height: 1200px;*/
/**/
nav
background-color: #000000;
min-height: 85px;
color: white;
opacity: 0.7;
.dropdown:hover>.dropdown-menu
display: block;
overflow: visible;
z-index: 1000;
position: inherit;
margin-top: 10%;
nav ul li a
color: white !important;
.top-div
font-size: 14px;
background-color: black;
color: white;
padding-top: 10px;
.top-div p .fa-map-marker
margin-left: 4%;
.top-div p i
margin-right: 0.5%;
margin-left: 3%;
color: #c6b52d;
/* Ow; Slider CSS*/
#owl-demo
z-index: 30;
.owl-wrapper
positon: relative;
.owl-controls
position: absolute;
bottom: 10px;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
.owl-theme .owl-controls .owl-page span
background: #fff !important;
.owl-img
width: 100%;
.owl-text-overlay
position: absolute;
text-align: center;
width: 60%;
top: 50%;
transform: translateY(-50%);
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
color: #fff;
background-color: rgba(0, 0, 0, 0.4);
background: rgba(0, 0, 0, 0.4);
padding-bottom: 20px;
font-family: "Open Sans", sans-serif;
border-radius: 15px 50px 30px 5px;
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Imagine</title>
<link href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,800" rel="stylesheet">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.theme.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js"></script>
</head>
<body>
<!--top header-->
<header>
<!--most top info -->
<!--<div style="height: 40px; background: red"></div>-->
<!-- End most top info -->
<nav class="navbar navbar-expand-lg navbar-light top-navbar" data-toggle="sticky-onscroll">
<div class="container">
<a class="navbar-brand" href="#">
<img src="assets/images/Logo-white.png" >
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarSupportedContent">
<ul class="navbar-nav pull-right">
<li class="nav-item">
<a class="nav-link active" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about-us.html">Who We Are</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown link
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Support & Service</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact Us</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="owl-carousel owl-theme" id="owl-demo">
<div>
<div class="owl-text-overlay hidden-xs">
<h2 class="owl-title">Arrow</h2>
<p class="owl-caption hidden-sm">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent iaculis purus vel enim suscipit, vitae volutpat ante scelerisque. Pellentesque blandit malesuada dui, sed aliquet risus molestie non.</p>
</div><img class="owl-img" src="https://res.cloudinary.com/jbennett45/image/upload/v1474628443/arrow_dqnmmf.jpg"></div>
<div>
<div class="owl-text-overlay hidden-xs">
<h2 class="owl-title">Flash</h2>
<p class="owl-caption hidden-sm">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent iaculis purus vel enim suscipit, vitae volutpat ante scelerisque. Pellentesque blandit malesuada dui, sed aliquet risus molestie non.</p>
</div><img class="owl-img" src="https://res.cloudinary.com/jbennett45/image/upload/v1474628444/flash_pxythx.jpg"></div>
</div>
【问题讨论】:
您是否尝试在您的 CSS 属性末尾添加!important
?试试z-index
。您的 css 可能会被其他内容覆盖,这也取决于首先加载和最后加载的内容等。示例:z-index: 9999 !important;
@Martin 我试过了,但还是不行。我刚刚发布了下拉错误的图像,请看一下。谢谢
你能在 JSFiddle 中设置它吗?看看会容易很多
我通过 github 页面 mohsinbhaai.github.io/imagine 实现了它,它很快就会工作。否则你会看到完整的代码或者你可以从这里下载github.com/mohsinbhaai/imagine
【参考方案1】:
将z-index
添加到nav
。
z-index 属性指定元素的堆叠顺序。
Reference
jQuery(document).ready(function($)
$("#owl-demo").owlCarousel(
navigation : true,
slideSpeed : 300,
paginationSpeed : 500,
items: 1,
singleItem: true,
autoPlay : 4000
);
);
$('body').on('mouseenter mouseleave','.dropdown',function(e)
var _d=$(e.target).closest('.dropdown');
_d.addClass('show');
setTimeout(function()
_d[_d.is(':hover')?'addClass':'removeClass']('show');
$('[data-toggle="dropdown"]', _d).attr('aria-expanded',_d.is(':hover'));
,300);
);
/*sticky header position*/
.sticky.is-sticky
position: fixed;
left: 0;
right: 0;
top: 0;
z-index: 999;
width: 100%;
header
background: transparent !important;
/*body */
/*min-height: 1200px;*/
/**/
nav
background-color: #000000;
min-height: 85px;
color: white;
opacity: 0.7;
z-index: 999999; //Add this
.dropdown:hover>.dropdown-menu
display: block;
overflow: visible;
z-index: 1000;
position: inherit;
margin-top: 10%;
nav ul li a
color: white !important;
.top-div
font-size: 14px;
background-color: black;
color: white;
padding-top: 10px;
.top-div p .fa-map-marker
margin-left: 4%;
.top-div p i
margin-right: 0.5%;
margin-left: 3%;
color: #c6b52d;
/* Ow; Slider CSS*/
#owl-demo
z-index: 30;
.owl-wrapper
positon: relative;
.owl-controls
position: absolute;
bottom: 10px;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
.owl-theme .owl-controls .owl-page span
background: #fff !important;
.owl-img
width: 100%;
.owl-text-overlay
position: absolute;
text-align: center;
width: 60%;
top: 50%;
transform: translateY(-50%);
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
color: #fff;
background-color: rgba(0, 0, 0, 0.4);
background: rgba(0, 0, 0, 0.4);
padding-bottom: 20px;
font-family: "Open Sans", sans-serif;
border-radius: 15px 50px 30px 5px;
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Imagine</title>
<link href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,800" rel="stylesheet">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.theme.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js"></script>
</head>
<body>
<!--top header-->
<header>
<!--most top info -->
<!--<div style="height: 40px; background: red"></div>-->
<!-- End most top info -->
<nav class="navbar navbar-expand-lg navbar-light top-navbar" data-toggle="sticky-onscroll">
<div class="container">
<a class="navbar-brand" href="#">
<img src="assets/images/Logo-white.png" >
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarSupportedContent">
<ul class="navbar-nav pull-right">
<li class="nav-item">
<a class="nav-link active" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about-us.html">Who We Are</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown link
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Support & Service</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact Us</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="owl-carousel owl-theme" id="owl-demo">
<div>
<div class="owl-text-overlay hidden-xs">
<h2 class="owl-title">Arrow</h2>
<p class="owl-caption hidden-sm">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent iaculis purus vel enim suscipit, vitae volutpat ante scelerisque. Pellentesque blandit malesuada dui, sed aliquet risus molestie non.</p>
</div><img class="owl-img" src="https://res.cloudinary.com/jbennett45/image/upload/v1474628443/arrow_dqnmmf.jpg"></div>
<div>
<div class="owl-text-overlay hidden-xs">
<h2 class="owl-title">Flash</h2>
<p class="owl-caption hidden-sm">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent iaculis purus vel enim suscipit, vitae volutpat ante scelerisque. Pellentesque blandit malesuada dui, sed aliquet risus molestie non.</p>
</div><img class="owl-img" src="https://res.cloudinary.com/jbennett45/image/upload/v1474628444/flash_pxythx.jpg"></div>
</div>
【讨论】:
将 z-index 添加到导航类。 这不起作用我添加了我面临的问题的屏幕截图,请查看问题区域图像是否存在。谢谢 是的,这个问题已经解决了。你可以运行这段代码并全屏打开,看看这段代码运行良好。 我粘贴整个代码。现在图像滑块被隐藏,下拉菜单工作正常。但我也想要图像滑块。请查看代码github.com/mohsinbhaai/imagine 并在这里直播mohsinbhaai.github.io/imagine 谢谢你 我在您的代码导航中看不到z-index
。添加后它对我来说效果很好。甚至图像滑块也能正常工作。您已将z-index
添加到.dropdown:hover>.dropdown-menu
,这是导致此问题的原因。【参考方案2】:
您需要将z-index: 999
添加到.navbar
类。这是下拉菜单的整体父级:
jQuery(document).ready(function($)
$("#owl-demo").owlCarousel(
navigation : true,
slideSpeed : 300,
paginationSpeed : 500,
items: 1,
singleItem: true,
autoPlay : 4000
);
);
$('body').on('mouseenter mouseleave','.dropdown',function(e)
var _d=$(e.target).closest('.dropdown');
_d.addClass('show');
setTimeout(function()
_d[_d.is(':hover')?'addClass':'removeClass']('show');
$('[data-toggle="dropdown"]', _d).attr('aria-expanded',_d.is(':hover'));
,300);
);
/*sticky header position*/
.sticky.is-sticky
position: fixed;
left: 0;
right: 0;
top: 0;
z-index: 999;
width: 100%;
header
background: transparent !important;
/*body */
/*min-height: 1200px;*/
/**/
nav
background-color: #000000;
min-height: 85px;
color: white;
opacity: 0.7;
.dropdown:hover>.dropdown-menu
display: block;
overflow: visible;
z-index: 1000;
position: inherit;
margin-top: 10%;
.navbar
z-index: 999;
nav ul li a
color: grey !important;
.top-div
font-size: 14px;
background-color: black;
color: white;
padding-top: 10px;
.top-div p .fa-map-marker
margin-left: 4%;
.top-div p i
margin-right: 0.5%;
margin-left: 3%;
color: #c6b52d;
/* Ow; Slider CSS*/
#owl-demo
z-index: 30;
.owl-wrapper
positon: relative;
.owl-controls
position: absolute;
bottom: 10px;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
.owl-theme .owl-controls .owl-page span
background: #fff !important;
.owl-img
width: 100%;
.owl-text-overlay
position: absolute;
text-align: center;
width: 60%;
top: 50%;
transform: translateY(-50%);
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
color: #fff;
background-color: rgba(0, 0, 0, 0.4);
background: rgba(0, 0, 0, 0.4);
padding-bottom: 20px;
font-family: "Open Sans", sans-serif;
border-radius: 15px 50px 30px 5px;
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Imagine</title>
<link href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,800" rel="stylesheet">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.theme.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js"></script>
</head>
<body>
<!--top header-->
<header>
<!--most top info -->
<!--<div style="height: 40px; background: red"></div>-->
<!-- End most top info -->
<nav class="navbar navbar-expand-lg navbar-light top-navbar" data-toggle="sticky-onscroll">
<div class="container">
<a class="navbar-brand" href="#">
<img src="assets/images/Logo-white.png" >
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarSupportedContent">
<ul class="navbar-nav pull-right">
<li class="nav-item">
<a class="nav-link active" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about-us.html">Who We Are</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown link
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Support & Service</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact Us</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="owl-carousel owl-theme" id="owl-demo">
<div>
<div class="owl-text-overlay hidden-xs">
<h2 class="owl-title">Arrow</h2>
<p class="owl-caption hidden-sm">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent iaculis purus vel enim suscipit, vitae volutpat ante scelerisque. Pellentesque blandit malesuada dui, sed aliquet risus molestie non.</p>
</div><img class="owl-img" src="https://res.cloudinary.com/jbennett45/image/upload/v1474628443/arrow_dqnmmf.jpg"></div>
<div>
<div class="owl-text-overlay hidden-xs">
<h2 class="owl-title">Flash</h2>
<p class="owl-caption hidden-sm">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent iaculis purus vel enim suscipit, vitae volutpat ante scelerisque. Pellentesque blandit malesuada dui, sed aliquet risus molestie non.</p>
</div><img class="owl-img" src="https://res.cloudinary.com/jbennett45/image/upload/v1474628444/flash_pxythx.jpg"></div>
</div>
只是一个旁注:您可能想要更改子菜单项的颜色文本,因为它们当前是白色的,与背景图像匹配并且不可见。
【讨论】:
【参考方案3】:只需将 z-index 添加到下拉列表中,它应该大于图像及其父级的 z-index(如果有的话)。
【讨论】:
【参考方案4】:如果 Z-index 不起作用,另一种可能是溢出设置为隐藏。这就是我刚才发生的事情。
【讨论】:
以上是关于下拉菜单隐藏在图片后面的主要内容,如果未能解决你的问题,请参考以下文章