我正在使用"Son of Suckerfish "创建一个下拉菜单(实际上它会弹出到右边)。
有些子菜单相当长,位于折叠栏下方。是否有一种方法(使用JavaScript/jQuery),以确保菜单总是在折叠(我很高兴,如果那些太大,不适合在视窗只是屁股到顶部)?
最后,我找不到答案,所以我自己想了一个解决办法。
以下是我使用的代码(需要jQuery):var winHeight = $(window).height();
$navULs = $('#nav ul');
$navULs.find('ul').each(function(i,v) {
$this = $(v);
var ulHeight = $this.height();
var parentHeight = $this.closest('li').height();
var parentTop = $this.offset().top;
var parentBottom = parentTop + parentHeight;
if ((winHeight - parentTop) < ulHeight) {
if (parentBottom < ulHeight) {
$this.css({
'margin-top': '0px', // remove margin-top (added by suckerfish css) as it screws up the calculations
'top': '-' + (parentTop) + 'px' // move the menu up by the amount of px available above the parent's top
});
} else {
$this.css('bottom', '0px'); // v basic, if sub menu will drop too much shove it up (css does the heavy lifting here)
}
}
});
我已经在这篇博文中解释过了。