看看这个jsFiddle。
当您向下滚动一点时,导航栏应该出现在左侧。这可以正常工作。当您滚动回最顶部时会出现问题。它应该立即隐藏,但事实并非如此。有时它会在几秒钟的延迟后隐藏起来,有时它什么也不做。当您按下它应该的“到顶部”按钮时,它也不会隐藏。
有什么想法吗?
HTML
<nav id="menu-float">
<ul>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ul>
<div id="toTop">to top</div>
</nav>
CSS
nav#menu-float {
position: fixed;
left: 50%;
margin-left: -300px;
width: 120px;
background: white;
padding: 0.2em 0.4em;
padding-bottom: 10px;
border-radius: 8px;
box-shadow: 0 3px 0px rgba(0, 0, 0, 0.1);
top: 0;
opacity: 0;
}
nav#menu-float div#toTop {
background: #ffffff;
background: -moz-linear-gradient(top, #ffffff 0%, #ededed 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff),
color-stop(100%,#ededed));
background: -webkit-linear-gradient(top, #ffffff 0%,#ededed 100%);
background: -o-linear-gradient(top, #ffffff 0%,#ededed 100%);
background: -ms-linear-gradient(top, #ffffff 0%,#ededed 100%);
background: linear-gradient(top, #ffffff 0%,#ededed 100%);
border: 1px #DDD solid;
border-radius: 8px;
box-shadow: 0 2px 2px rgba(0, 0, 0, 0.08);
width: 50px;
height: 20px;
right: 5px;
position: absolute;
bottom: 5px;
cursor: pointer;
text-align: center;
}
JAVASCRIPT
// Only show side menu when x pixels from top and when window size allows it
function checkSize() {
var floatMenu = $("nav#menu-float");
if ($(window).width() > 560) {
$(window).scroll(function() {
if ($(document).scrollTop() > 100) {
floatMenu .animate({
opacity: 1,
top: "45px"
}, 800);
}
else {
floatMenu .animate({
opacity: 0,
top: "0px"
}, 800);
}
});
}
else {
floatMenu .hide();
}
}
checkSize();
$(window).resize(function() {
checkSize();
});
// Back to top
$("div#toTop").click(function(e) {
$("body,html").animate({
scrollTop: 0
}, 800);
});