我正在创建一个下拉菜单,当悬停在上面时,它会显示子链接。
但是,我遇到了一个问题,当我将鼠标悬停在下拉列表上时,它会显示一个空白框,并且只在悬停在它上面时显示文本。
我希望在任何时候都能显示文本。
// Sticky Navbar
var h = document.getElementById("navbar");
var stuck = false;
var stickPoint = getDistance();
function getDistance() {
var topDist = h.offsetTop;
return topDist;
}
window.onscroll = function(e) {
var distance = getDistance() - window.pageYOffset;
var offset = window.pageYOffset;
if ( (distance <= 0) && !stuck) {
h.style.position = 'fixed';
h.style.top = '0px';
stuck = true;
} else if (stuck && (offset <= stickPoint)){
h.style.position = 'static';
stuck = false;
}
}
// Preloader
const preloader = document.querySelector('.preloader');
const fadeEffect = setInterval(() => {
// if we don't set opacity 1 in CSS, then //it will be equaled to "", that's why we // check it
if (!preloader.style.opacity) {
preloader.style.opacity = 1;
}
if (preloader.style.opacity > 0) {
preloader.style.opacity -= 0.1;
} else {
clearInterval(fadeEffect);
}
}, 200);
window.addEventListener('load', fadeEffect);
/* Navbar */
#navbar {
height: 75px;
display: block;
background: #333;
width: 100%;
}
#navbar a{
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
padding-top: 10px;
text-align: center;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
#navbar a:hover, .dropdown:hover .dropbtn {
background-color: silver;
color: black;
}
.dropdown-content {
display: none;
position: absolute;
margin-left: 49%;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
display: inline-block;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
<!-- Navbar -->
<div id="navbar">
<div class="dropdown">
<button class="dropbtn">Projects
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">Drum Kit</a>
<a href="#">Coming Soon...</a>
</div>
</div>
</div>
除此之外,我还使用VanillaJS实现了Sticky Navbar和一个简单的预加载程序,如果有帮助的话,我将与大家分享。
帮我做下拉列表。任何帮助都将不胜感激。
作为@Y。S.指出你的颜色设置为白色。不确定你尝试了什么都不起作用,这对我来说很好:
// Sticky Navbar
var h = document.getElementById("navbar");
var stuck = false;
var stickPoint = getDistance();
function getDistance() {
var topDist = h.offsetTop;
return topDist;
}
window.onscroll = function(e) {
var distance = getDistance() - window.pageYOffset;
var offset = window.pageYOffset;
if ( (distance <= 0) && !stuck) {
h.style.position = 'fixed';
h.style.top = '0px';
stuck = true;
} else if (stuck && (offset <= stickPoint)){
h.style.position = 'static';
stuck = false;
}
}
// Preloader
const preloader = document.querySelector('.preloader');
const fadeEffect = () =>
{
setInterval(() => {
// if we don't set opacity 1 in CSS, then //it will be equaled to "", that's why we // check it
if (!preloader.style.opacity) {
preloader.style.opacity = 1;
}
if (preloader.style.opacity > 0) {
preloader.style.opacity -= 0.1;
} else {
clearInterval(fadeEffect);
}
}, 200);
}
window.addEventListener('load', fadeEffect);
/* Navbar */
#navbar {
height: 75px;
display: block;
background: #333;
width: 100%;
}
#navbar a{
font-size: 16px;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
padding-top: 10px;
text-align: center;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
#navbar a:hover, .dropdown:hover .dropbtn {
background-color: silver;
color: black;
}
.dropdown-content {
display: none;
position: absolute;
margin-left: 49%;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
display: inline-block;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
<!-- Navbar -->
<div id="navbar">
<div class="dropdown">
<button class="dropbtn">Projects
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">Drum Kit</a>
<a href="#">Coming Soon...</a>
</div>
</div>
</div>
<div class="preloader"></div>