導覽列是網站的骨幹,當使用者把視線移過去時,如果能讓其他區域稍微退後、目前的項目浮出來,操作焦點就非常清楚。遮罩層加滑動展開,正是達到這個效果的經典手法。
滑入整個選單區域時,一層半透明遮罩淡入,把背景壓暗;滑到某個項目時,該項目的子層以 slideDown 展開,同時把其他已展開的子層收起,形成「明亮的項目浮在略暗的背景上」的對比。
$(function () {
var $oe_menu = $('#oe_menu');
var $oe_menu_items = $oe_menu.children('li');
var $oe_overlay = $('#oe_overlay');
$oe_menu_items.bind('mouseenter', function () {
var $this = $(this);
$this.addClass('slided selected');
$this.children('div').css('z-index', '9999').stop(true, true).slideDown(200, function () {
$oe_menu_items.not('.slided').children('div').hide();
$this.removeClass('slided');
});
}).bind('mouseleave', function () {
var $this = $(this);
$this.removeClass('selected').children('div').css('z-index', '1');
});
$oe_menu.bind('mouseenter', function () {
$(this).addClass('hovered');
$oe_overlay.stop(true, true).fadeTo(200, 0.6);
}).bind('mouseleave', function () {
$(this).removeClass('hovered');
$oe_overlay.stop(true, true).fadeTo(200, 0);
$oe_menu_items.children('div').hide();
});
});
stop(true, true) 用來清掉尚未播完的動畫並跳到終點,避免滑鼠快速來回時動畫堆疊、畫面抖動。
展開的子層 z-index 拉到 9999,確保它蓋在遮罩之上;滑鼠移出後改回 1,讓層級關係復原。
遮罩的淡入淡出綁在整個 menu 上,而不是每個 li 上,這樣在項目間移動時遮罩不會反覆閃爍。
最後別忘了給 #oe_overlay 設 position: fixed 或 absolute 並填滿視窗,同時加上 pointer-events 考量,避免遮罩擋住不該擋的點擊。