问题描述
我正在建立一个响应迅速的网站,我需要在屏幕中间居中放置一个固定的页脚导航,将鼠标悬停在该页面上可以激活一个下拉菜单.我对所有内容都只使用百分比(从长远来看,这很重要,可以使标题在宽度方向上对齐),这使所有内容对我来说都有些混乱.
I am building a website that will be responsive and I need a fixed footer nav centered in the middle of the screen that when hovered over activates a drop-up menu. I am using only percentages for everything (important for lining things up width-wise with my header in the long run) which is making everything a little confusing for me.
每当我将鼠标悬停在INFO
上时(请参阅我的 JSFiddle )并尝试移动鼠标朝子菜单向上,我离开INFO
后,子菜单立即下降.当我从#footer-nav ul li:hover > ul
中删除css属性:position
,width
,bottom
和margin-bottom
(本质上将下拉菜单变为下拉菜单)时,即使在以下情况下,子菜单仍保持打开状态:我将鼠标移到子菜单上.我什至可以将margin-bottom
更改为20%
,子菜单仍将保持打开状态,并移至该空白区域.当我将其设置为下拉菜单而不是下拉菜单时,是什么使它关闭?
Whenever I hover over INFO
(see my JSFiddle) and attempt to move my mouse upwards toward the submenu, the submenu drops as soon as I leave INFO
. When I remove the css attributes: position
, width
, bottom
, and margin-bottom
from my #footer-nav ul li:hover > ul
(essentially turning the drop-up menu into a drop-down menu), the submenu stays open even when I move the mouse onto the submenu. I can even change the margin-bottom
to 20%
and the submenu will still stay open going over that empty space. What is making it close when I make it a drop-up rather than a drop-down?
在悬停触发的地方我也有一个问题.我可以将鼠标移到INFO
的左侧/右侧,我的子菜单仍然会弹出.如何使用当前的div
并设置百分比和居中设置来解决此问题?
I also have an issue with where the hovering is being triggered. I can move the mouse far to the left/right of INFO
and my submenu still pops up. How can I fix this using my current div
, set up with the percentages and centering?
以下是相关代码:
<div id="background">
<footer id="footer">
<div id="footer-nav">
<ul>
<li id="info">INFO
<ul>
<li id="twitter"><a href="https://twitter.com"
target="_blank">TWITTER</a></li>
<li id="instagram"><a href="https://www.instagram.com"
target="_blank">INSTAGRAM</a></li>
<li id="email"><a href="mailto:[email protected]">EMAIL</a></li>
</ul>
</li>
</ul>
</div>
</footer>
</div>
#background {
background-color: #FFFFFF;
margin: 0px;
padding: 0px;
top: 0;
left: 0;
width: 100%;
height: 100%;
position: fixed;
z-index: -1;
}
#footer {
position: fixed;
bottom: 0;
width: 25%;
text-align: center;
margin-bottom: 1%;
margin-left: -15%;
left: 50%;
margin-left: -12.5%;
}
a {
text-decoration: none;
color: inherit;
}
#footer-nav ul li:hover > ul {
display: block;
position: absolute;
width: 100%;
bottom: 100%;
margin-bottom: 2%;
}
#footer-nav {
width: 100%;
}
#footer-nav ul {
font-family: Arial;
font-size: 100%;
font-weight: bold;
color: #000000;
list-style-type: none;
text-align: center;
margin: auto;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
}
#footer-nav ul ul {
font-family: Arial;
font-size: 100%;
color: #000000;
list-style-type: none;
font-weight: normal;
display: none;
}
#email {
padding-top: 2%;
padding-bottom: 2%;
border: 1px solid black;
color: #000000;
}
#instagram {
padding-top: 2%;
padding-bottom: 2%;
border: 1px solid black;
color: #000000;
}
#twitter {
padding-top: 2%;
padding-bottom: 2%;
border: 1px solid black;
color: #000000;
}
推荐答案
问题来自#footer-nav ul li:hover > ul
上的margin-bottom: 2%
.当您从Info
按钮移至此边距时,您不再需要将鼠标悬停在有问题的元素上.要更正此问题,只需将其替换为padding-bottom: 2%
:
The problem comes from margin-bottom: 2%
on #footer-nav ul li:hover > ul
. As you move off of your Info
button into this margin, you're no longer hovering over the element in question. To correct this, simply replace it with padding-bottom: 2%
:
$(document).ready(function() {
$("#email").hover(function() {
$("#email").css("background-color", "black");
}, function() {
$("#email").css("background-color", "white");
});
});
$(document).ready(function() {
$("#email").hover(function() {
$("#email").css("color", "white");
}, function() {
$("#email").css("color", "black");
});
});
$(document).ready(function() {
$("#twitter").hover(function() {
$("#twitter").css("background-color", "black");
}, function() {
$("#twitter").css("background-color", "white");
});
});
$(document).ready(function() {
$("#twitter").hover(function() {
$("#twitter").css("color", "white");
}, function() {
$("#twitter").css("color", "black");
});
});
$(document).ready(function() {
$("#instagram").hover(function() {
$("#instagram").css("background-color", "black");
}, function() {
$("#instagram").css("background-color", "white");
});
});
$(document).ready(function() {
$("#instagram").hover(function() {
$("#instagram").css("color", "white");
}, function() {
$("#instagram").css("color", "black");
});
});
#background {
background-color: #FFFFFF;
margin: 0px;
padding: 0px;
top: 0;
left: 0;
width: 100%;
height: 100%;
position: fixed;
z-index: -1;
}
#footer {
position: fixed;
bottom: 0;
width: 25%;
text-align: center;
margin-bottom: 1%;
margin-left: -15%;
left: 50%;
margin-left: -12.5%;
}
a {
text-decoration: none;
color: inherit;
}
#footer-nav ul li:hover>ul {
display: block;
position: absolute;
width: 100%;
bottom: 100%;
padding-bottom: 2%;
}
#footer-nav {
width: 100%;
}
#footer-nav ul {
font-family: Arial;
font-size: 100%;
font-weight: bold;
color: #000000;
list-style-type: none;
text-align: center;
margin: auto;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
}
#footer-nav ul ul {
font-family: Arial;
font-size: 100%;
color: #000000;
list-style-type: none;
font-weight: normal;
display: none;
}
#email {
padding-top: 2%;
padding-bottom: 2%;
border: 1px solid black;
color: #000000;
}
#instagram {
padding-top: 2%;
padding-bottom: 2%;
border: 1px solid black;
color: #000000;
}
#twitter {
padding-top: 2%;
padding-bottom: 2%;
border: 1px solid black;
color: #000000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="background">
<footer id="footer">
<div id="footer-nav">
<ul>
<li id="info">INFO
<ul>
<li id="twitter"><a href="https://twitter.com" target="_blank">TWITTER</a></li>
<li id="instagram"><a href="https://www.instagram.com" target="_blank">INSTAGRAM</a></li>
<li id="email"><a href="mailto:[email protected]">EMAIL</a></li>
</ul>
</li>
</ul>
</div>
</footer>
</div>
还请注意,您可以将所有功能组合到一个$(document).ready
中,也可以将.css()
规则组合在一起.这将大大减少行数,提高可读性.我在以下示例中完成了此操作:
Also note that you can combine all of your functions into one $(document).ready
, along with combining the .css()
rules as well. This will significantly cut down on lines, improving readability. I've done this in the following example:
$(document).ready(function() {
$("#email").hover(function() {
$("#email").css("background-color", "black");
$("#email").css("color", "white");
}, function() {
$("#email").css("background-color", "white");
$("#email").css("color", "black");
});
$("#twitter").hover(function() {
$("#twitter").css("background-color", "black");
$("#twitter").css("color", "white");
}, function() {
$("#twitter").css("background-color", "white");
$("#twitter").css("color", "black");
});
$("#instagram").hover(function() {
$("#instagram").css("background-color", "black");
$("#instagram").css("color", "white");
}, function() {
$("#instagram").css("background-color", "white");
$("#instagram").css("color", "black");
});
});
#background {
background-color: #FFFFFF;
margin: 0px;
padding: 0px;
top: 0;
left: 0;
width: 100%;
height: 100%;
position: fixed;
z-index: -1;
}
#footer {
position: fixed;
bottom: 0;
width: 25%;
text-align: center;
margin-bottom: 1%;
margin-left: -15%;
left: 50%;
margin-left: -12.5%;
}
a {
text-decoration: none;
color: inherit;
}
#footer-nav ul li:hover>ul {
display: block;
position: absolute;
width: 100%;
bottom: 100%;
padding-bottom: 2%;
}
#footer-nav {
width: 100%;
}
#footer-nav ul {
font-family: Arial;
font-size: 100%;
font-weight: bold;
color: #000000;
list-style-type: none;
text-align: center;
margin: auto;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
}
#footer-nav ul ul {
font-family: Arial;
font-size: 100%;
color: #000000;
list-style-type: none;
font-weight: normal;
display: none;
}
#email {
padding-top: 2%;
padding-bottom: 2%;
border: 1px solid black;
color: #000000;
}
#instagram {
padding-top: 2%;
padding-bottom: 2%;
border: 1px solid black;
color: #000000;
}
#twitter {
padding-top: 2%;
padding-bottom: 2%;
border: 1px solid black;
color: #000000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="background">
<footer id="footer">
<div id="footer-nav">
<ul>
<li id="info">INFO
<ul>
<li id="twitter"><a href="https://twitter.com" target="_blank">TWITTER</a></li>
<li id="instagram"><a href="https://www.instagram.com" target="_blank">INSTAGRAM</a></li>
<li id="email"><a href="mailto:[email protected]">EMAIL</a></li>
</ul>
</li>
</ul>
</div>
</footer>
</div>
默认情况下,#info
元素将扩展为适合其容器的大小,因为它是一个块级元素.为了防止将其悬停在文本边缘之外,您需要将#info
设置为display: inline-block
.
By default, the #info
element will expand to fit the size of its container, as it is a block-level element. In order to prevent it from being able to be hovered over past the edge of the text, you'll want to set #info
to display: inline-block
.
这会缩小元素的宽度,并使弹出窗口进一步向右移动.为了解决这个问题,您需要在calc(-50% + 18.67px)
的#info > ul
上设置margin-left
. -50%
是继承的左对齐,而18.67px
是文本INFO
的默认宽度.如果在#info
上设置了width
,则calc()
中的值应更新为匹配.
This shrinks the width of the element, and causes the popup to come in further to the right. To counteract this, you'll want to set margin-left
on #info > ul
of calc(-50% + 18.67px)
. The -50%
is the inherit left-alignment, and the 18.67px
is the default width of the text INFO
. If you set a width
on #info
, the value in calc()
should be updated to match.
这可以在下面看到:
$(document).ready(function() {
$("#email").hover(function() {
$("#email").css("background-color", "black");
$("#email").css("color", "white");
}, function() {
$("#email").css("background-color", "white");
$("#email").css("color", "black");
});
$("#twitter").hover(function() {
$("#twitter").css("background-color", "black");
$("#twitter").css("color", "white");
}, function() {
$("#twitter").css("background-color", "white");
$("#twitter").css("color", "black");
});
$("#instagram").hover(function() {
$("#instagram").css("background-color", "black");
$("#instagram").css("color", "white");
}, function() {
$("#instagram").css("background-color", "white");
$("#instagram").css("color", "black");
});
});
#background {
background-color: #FFFFFF;
margin: 0px;
padding: 0px;
top: 0;
left: 0;
width: 100%;
height: 100%;
position: fixed;
z-index: -1;
}
#footer {
position: fixed;
bottom: 0;
width: 25%;
text-align: center;
margin-bottom: 1%;
margin-left: -15%;
left: 50%;
margin-left: -12.5%;
}
a {
text-decoration: none;
color: inherit;
}
#footer-nav ul li:hover>ul {
display: block;
position: absolute;
width: 100%;
bottom: 100%;
padding-bottom: 2%;
}
#footer-nav {
width: 100%;
}
#footer-nav ul {
font-family: Arial;
font-size: 100%;
font-weight: bold;
color: #000000;
list-style-type: none;
text-align: center;
margin: auto;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
}
#footer-nav ul ul {
font-family: Arial;
font-size: 100%;
color: #000000;
list-style-type: none;
font-weight: normal;
display: none;
}
#email {
padding-top: 2%;
padding-bottom: 2%;
border: 1px solid black;
color: #000000;
}
#instagram {
padding-top: 2%;
padding-bottom: 2%;
border: 1px solid black;
color: #000000;
}
#twitter {
padding-top: 2%;
padding-bottom: 2%;
border: 1px solid black;
color: #000000;
}
#info {
display: inline-block;
}
#info > ul {
margin-left: calc(-50% + 18.67px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="background">
<footer id="footer">
<div id="footer-nav">
<ul>
<li id="info">INFO
<ul>
<li id="twitter"><a href="https://twitter.com" target="_blank">TWITTER</a></li>
<li id="instagram"><a href="https://www.instagram.com" target="_blank">INSTAGRAM</a></li>
<li id="email"><a href="mailto:[email protected]">EMAIL</a></li>
</ul>
</li>
</ul>
</div>
</footer>
</div>
这篇关于鼠标移出后,Footer的导航子菜单将不会保持打开状态,除非我快速将鼠标悬停在ul打开子菜单的左/右上方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!