JavaScript 实现 hover 下拉菜单
on Front-End
const Hover = {
hover ($showBtn, $showBox, options) {
if (!$showBtn.length || !$showBox.length) {
return;
}
let timer = null,
top = parseInt($showBox.css('top'), 10),
opt = options || {},
activeClass = opt.activeClass || '';
function leaveHandler (e) {
timer = setTimeout(function () {
$showBtn.removeClass(activeClass);
$showBox.stop().animate({
'top': top,
'opacity': 0
}, 200, function () {
$(this).hide();
});
}, 200);
}
function enterHandler () {
clearTimeout(timer);
$showBtn.addClass(activeClass);
$showBox.show().stop().animate({
'top': opt.top,
'opacity': 1
}, 300);
}
$showBox.on('mouseenter', enterHandler);
$showBtn.on('mouseleave', leaveHandler);
$showBtn.on('mouseenter', enterHandler);
$showBox.on('mouseleave', leaveHandler);
}
};
module.exports = Hover;
使用时调用:
Hover.hover($showBtn, $showBox, {
'activeClass': 'btn-active',
'top': 30
});