【发布时间】:2015-01-28 17:34:50
【问题描述】:
编辑:这是一个 Youtube 视频,说明了我的问题: http://youtu.be/OguwjZR_GdU
在我的网站Black Star Opal 上,我一直在尝试实现一个粘性菜单,就像这个Dansk Kids。我查看了 Dansk Kids 网站的 javascript 和 CSS:他们的菜单中似乎没有涉及 javascript(除了在滚动时删除了粘性菜单下方的徽标)。如果可能的话,我希望我的粘性菜单与他们的一样平滑(即在弹出和弹出时与 url 栏保持齐平)。
这是我的 #carttrans 的 CSS,菜单 div:
position: fixed;
-webkit-backface-visibility: hidden;
height: 49px;
top: 0px;
left: 0px;
background-color: rgb(255, 255, 255);
width: 100% !important;
z-index: 10000;
text-align: center;
margin-left: 0px;
padding-left: 7px;
border: 1px solid rgb(221, 221, 221);
border-left: none;
border-right: none;
border-bottom-style: solid !important;
border-bottom-width: 1px !important;
border-bottom-color: rgb(221,221,221) !important;
-webkit-transform: translateZ(0);
我也使用这个 js 代码(只是因为没有它菜单不会在 iOS Safari 上显示,虽然我不确定为什么):
$(function() {
// grab the initial top offset of the navigation
var sticky_navigation_offset_top = $('#carttrans').offset().top;
// our function that decides weather the navigation bar should have "fixed" css position or not.
var sticky_navigation = function(){
var scroll_top = $(window).scrollTop(); // our current vertical position from the top
if ($(window).width() < 500)
{
// if we've scrolled more than the navigation, change its position to fixed to stick to top, otherwise change it back to relative
if (scroll_top > sticky_navigation_offset_top) {
$('#carttrans').css({ 'position': 'fixed', 'top':0, 'left':0 });
} else {
$('#carttrans').css({ 'position': 'fixed' });
}
}
};
// run our function on load
sticky_navigation();
// and run it again every time you scroll
$(window).scroll(function() {
sticky_navigation();
});
});
我什至删除了菜单中的所有元素,只留下空白的白条,看看它是否会做同样的事情。它像以前一样尴尬地进进出出。
对此的任何帮助都会令人惊叹。
编辑:正如我在下面所说,弹出和弹出的 URL 栏似乎扰乱了我的粘性菜单。这可能是重绘问题或速度变慢,因为在其他站点上,url 栏的消失和菜单的后续移动(例如,在粘性菜单演示中)非常顺利,我正在做/已经测试过它们弹出相同的网址栏。
干杯, 抢
【问题讨论】:
标签: javascript css menu sticky