重构 nav 垂直导航菜单展开收缩动画 (#1407)
* 更新 nav 用例 * 优化 nav 垂直导航的浅色背景样式 * 剔除 nav 多余选择器 * 优化 nav 代码书写规范 * 重构 nav 垂直导航菜单展开收缩动画
This commit is contained in:
commit
32ea564e42
@ -127,103 +127,94 @@
|
||||
|
||||
<div class="demo-nav-tree">
|
||||
<div class="layui-inline">
|
||||
|
||||
<ul class="layui-nav layui-nav-tree" lay-accordion lay-filter="test">
|
||||
<li class="layui-nav-item">
|
||||
<a class="" href="javascript:;">产品</a>
|
||||
<a class="" href="javascript:;">菜单 1</a>
|
||||
<dl class="layui-nav-child">
|
||||
<dd><a href="javascript:;">移动模块</a></dd>
|
||||
<dd>
|
||||
<a href="javascript:;">后台模版</a>
|
||||
<dl class="layui-nav-child">
|
||||
<dd><a href="javascript:;">组件一</a></dd>
|
||||
<dd>
|
||||
<a href="javascript:;">组件二</a>
|
||||
</dd>
|
||||
<dd><a href="javascript:;">组件三</a></dd>
|
||||
</dl>
|
||||
</dd>
|
||||
<dd><a href="javascript:;">电商平台</a></dd>
|
||||
<dd><a href="">跳转菜单</a></dd>
|
||||
<dd><a href="javascript:;">选项1</a></dd>
|
||||
<dd><a href="javascript:;">选项2</a></dd>
|
||||
<dd><a href="javascript:;">选项3</a></dd>
|
||||
</dl>
|
||||
</li>
|
||||
<li class="layui-nav-item">
|
||||
<a href="javascript:;">解决方案</a>
|
||||
<a href="javascript:;">菜单 2</a>
|
||||
<dl class="layui-nav-child">
|
||||
<dd><a href="javascript:;">移动模块</a></dd>
|
||||
<dd>
|
||||
<a href="javascript:;">后台模版</a>
|
||||
</dd>
|
||||
<dd><a href="">电商平台</a></dd>
|
||||
<dd><a href="javascript:;">选项1</a></dd>
|
||||
<dd><a href="javascript:;">选项2</a></dd>
|
||||
<dd><a href="javascript:;">选项3</a></dd>
|
||||
</dl>
|
||||
</li>
|
||||
<li class="layui-nav-item"><a href="javascript:;">云市场</a></li>
|
||||
<li class="layui-nav-item"><a href="">社区</a></li>
|
||||
<li class="layui-nav-item">
|
||||
<a href="javascript:;">菜单 3</a>
|
||||
<dl class="layui-nav-child">
|
||||
<dd><a href="javascript:;">选项1</a></dd>
|
||||
<dd><a href="javascript:;">选项2</a></dd>
|
||||
<dd><a href="javascript:;">选项3</a></dd>
|
||||
</dl>
|
||||
</li>
|
||||
<li class="layui-nav-item"><a href="javascript:;">菜单 4</a></li>
|
||||
<li class="layui-nav-item"><a href="javascript:;">菜单 5</a></li>
|
||||
</ul>
|
||||
|
||||
</div>
|
||||
<div class="layui-inline">
|
||||
|
||||
<ul class="layui-nav layui-nav-tree layui-bg-gray" lay-filter="test">
|
||||
<li class="layui-nav-item">
|
||||
<a class="" href="javascript:;">产品</a>
|
||||
<a class="" href="javascript:;">菜单 1</a>
|
||||
<dl class="layui-nav-child">
|
||||
<dd><a href="javascript:;">移动模块</a></dd>
|
||||
<dd>
|
||||
<a href="javascript:;">后台模版</a>
|
||||
<dl class="layui-nav-child">
|
||||
<dd><a href="javascript:;">组件一</a></dd>
|
||||
<dd>
|
||||
<a href="javascript:;">组件二</a>
|
||||
</dd>
|
||||
<dd><a href="javascript:;">组件三</a></dd>
|
||||
</dl>
|
||||
</dd>
|
||||
<dd><a href="javascript:;">电商平台</a></dd>
|
||||
<dd><a href="">跳转菜单</a></dd>
|
||||
<dd><a href="javascript:;">选项1</a></dd>
|
||||
<dd><a href="javascript:;">选项2</a></dd>
|
||||
<dd><a href="javascript:;">选项3</a></dd>
|
||||
</dl>
|
||||
</li>
|
||||
<li class="layui-nav-item">
|
||||
<a href="javascript:;">解决方案</a>
|
||||
<a href="javascript:;">菜单 2</a>
|
||||
<dl class="layui-nav-child">
|
||||
<dd><a href="javascript:;">移动模块</a></dd>
|
||||
<dd>
|
||||
<a href="javascript:;">后台模版</a>
|
||||
</dd>
|
||||
<dd><a href="">电商平台</a></dd>
|
||||
<dd><a href="javascript:;">选项1</a></dd>
|
||||
<dd><a href="javascript:;">选项2</a></dd>
|
||||
<dd><a href="javascript:;">选项3</a></dd>
|
||||
</dl>
|
||||
</li>
|
||||
<li class="layui-nav-item"><a href="javascript:;">云市场</a></li>
|
||||
<li class="layui-nav-item"><a href="">社区</a></li>
|
||||
<li class="layui-nav-item">
|
||||
<a href="javascript:;">菜单 3</a>
|
||||
<dl class="layui-nav-child">
|
||||
<dd><a href="javascript:;">选项1</a></dd>
|
||||
<dd><a href="javascript:;">选项2</a></dd>
|
||||
<dd><a href="javascript:;">选项3</a></dd>
|
||||
</dl>
|
||||
</li>
|
||||
<li class="layui-nav-item"><a href="javascript:;">菜单 4</a></li>
|
||||
<li class="layui-nav-item"><a href="javascript:;">菜单 5</a></li>
|
||||
</ul>
|
||||
|
||||
</div>
|
||||
<div class="layui-inline">
|
||||
|
||||
<ul class="layui-nav layui-bg-cyan layui-nav-tree" lay-filter="test">
|
||||
<ul class="layui-nav layui-nav-tree layui-bg-black" lay-accordion lay-filter="test">
|
||||
<li class="layui-nav-item layui-nav-itemed">
|
||||
<a class="" href="javascript:;">产品</a>
|
||||
<a class="" href="javascript:;">菜单 1</a>
|
||||
<dl class="layui-nav-child">
|
||||
<dd><a href="javascript:;">移动模块</a></dd>
|
||||
<dd>
|
||||
<a href="javascript:;">后台模版</a>
|
||||
</dd>
|
||||
<dd><a href="javascript:;">电商平台</a></dd>
|
||||
<dd><a href="">跳转菜单</a></dd>
|
||||
<dd><a href="javascript:;">选项1</a></dd>
|
||||
<dd><a href="javascript:;">选项2</a></dd>
|
||||
<dd><a href="javascript:;">选项3</a></dd>
|
||||
</dl>
|
||||
</li>
|
||||
<li class="layui-nav-item">
|
||||
<a href="javascript:;">解决方案</a>
|
||||
<a href="javascript:;">菜单 2</a>
|
||||
<dl class="layui-nav-child">
|
||||
<dd><a href="javascript:;">移动模块</a></dd>
|
||||
<dd><a href="javascript:;">后台模版</a></dd>
|
||||
<dd><a href="">电商平台</a></dd>
|
||||
<dd><a href="javascript:;">选项1</a></dd>
|
||||
<dd><a href="javascript:;">选项2</a></dd>
|
||||
<dd><a href="javascript:;">选项3</a></dd>
|
||||
</dl>
|
||||
</li>
|
||||
<li class="layui-nav-item"><a href="">云市场</a></li>
|
||||
<li class="layui-nav-item"><a href="">社区</a></li>
|
||||
<li class="layui-nav-item">
|
||||
<a href="javascript:;">菜单 3</a>
|
||||
<dl class="layui-nav-child">
|
||||
<dd><a href="javascript:;">选项1</a></dd>
|
||||
<dd><a href="javascript:;">选项2</a></dd>
|
||||
<dd><a href="javascript:;">选项3</a></dd>
|
||||
</dl>
|
||||
</li>
|
||||
<li class="layui-nav-item"><a href="javascript:;">菜单 4</a></li>
|
||||
<li class="layui-nav-item"><a href="javascript:;">菜单 5</a></li>
|
||||
</ul>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
@ -1285,7 +1285,6 @@ body .layui-table-tips .layui-layer-content{background: none; padding: 0; box-sh
|
||||
|
||||
.layui-nav .layui-nav-more{position: absolute; top: 0; right: 3px; left: auto !important; margin-top: 0; font-size: 12px; cursor: pointer; transition: all .2s; -webkit-transition: all .2s;}
|
||||
.layui-nav .layui-nav-mored,
|
||||
.layui-nav-expand > a .layui-nav-more,
|
||||
.layui-nav-itemed > a .layui-nav-more{transform: rotate(180deg);}
|
||||
|
||||
|
||||
@ -1333,12 +1332,9 @@ body .layui-table-tips .layui-layer-content{background: none; padding: 0; box-sh
|
||||
/* 导航浅色背景 */
|
||||
.layui-nav.layui-bg-gray .layui-nav-item a,
|
||||
.layui-nav-tree.layui-bg-gray a{color: #373737; color: rgba(0,0,0,.8);}
|
||||
.layui-nav-tree.layui-bg-gray{padding: 6px 0;}
|
||||
.layui-nav-tree.layui-bg-gray .layui-nav-itemed>a{color: #000 !important;}
|
||||
.layui-nav.layui-bg-gray .layui-this a{color: #16b777;}
|
||||
.layui-nav-tree.layui-bg-gray .layui-nav-child{padding-left: 11px; background: none;}
|
||||
.layui-nav-tree.layui-bg-gray .layui-nav-item>a{padding-top: 0; padding-bottom: 0;}
|
||||
.layui-nav-tree.layui-bg-gray .layui-nav-item>a .layui-nav-more{padding: 0;}
|
||||
.layui-nav-tree.layui-bg-gray .layui-this,
|
||||
.layui-nav-tree.layui-bg-gray .layui-this>a,
|
||||
.layui-nav-tree.layui-bg-gray .layui-nav-child dd.layui-this,
|
||||
|
@ -275,12 +275,13 @@ layui.define('jquery', function(exports){
|
||||
// 点击菜单 - a 标签触发
|
||||
,clickThis: function() {
|
||||
var othis = $(this);
|
||||
var parents = othis.parents(NAV_ELEM);
|
||||
var parents = othis.closest(NAV_ELEM);
|
||||
var filter = parents.attr('lay-filter');
|
||||
var parent = othis.parent() ;
|
||||
var child = othis.siblings('.'+ NAV_CHILD);
|
||||
var unselect = typeof parent.attr('lay-unselect') === 'string'; // 是否禁用选中
|
||||
|
||||
// 满足点击选中的条件
|
||||
if (!(othis.attr('href') !== 'javascript:;' && othis.attr('target') === '_blank') && !unselect) {
|
||||
if (!child[0]) {
|
||||
parents.find('.'+ THIS).removeClass(THIS);
|
||||
@ -290,28 +291,44 @@ layui.define('jquery', function(exports){
|
||||
|
||||
// 若为垂直菜单
|
||||
if (parents.hasClass(NAV_TREE)) {
|
||||
var NAV_ITEMED = NAV_ITEM + 'ed';
|
||||
var NAV_EXPAND = 'layui-nav-expand';
|
||||
var needExpand = !(parent.hasClass(NAV_EXPAND) || parent.hasClass(NAV_ITEMED));
|
||||
var NAV_ITEMED = NAV_ITEM + 'ed'; // 用于标注展开状态
|
||||
var needExpand = !parent.hasClass(NAV_ITEMED); // 是否执行展开
|
||||
var ANIM_MS = 200; // 动画过渡毫秒数
|
||||
|
||||
// 动画执行完成后的操作
|
||||
var complete = function() {
|
||||
$(this).css({
|
||||
"display": "" // 剔除动画生成的 style display,以适配外部样式的状态重置
|
||||
});
|
||||
// 避免导航滑块错位
|
||||
parents.children('.'+ NAV_BAR).css({
|
||||
opacity: 0
|
||||
})
|
||||
};
|
||||
|
||||
// 是否正处于动画中的状态
|
||||
if (child.is(':animated')) return;
|
||||
|
||||
// 剔除可能存在的 CSS3 动画类
|
||||
child.removeClass(NAV_ANIM);
|
||||
|
||||
// 若有子菜单,则展开
|
||||
// 若有子菜单,则对其执行展开或收缩
|
||||
if (child[0]) {
|
||||
child.stop().slideToggle(200, function() {
|
||||
needExpand || parent.removeClass(NAV_ITEMED);
|
||||
});
|
||||
parent[needExpand ? 'addClass': 'removeClass'](NAV_EXPAND);
|
||||
// 手风琴
|
||||
if (needExpand) {
|
||||
// 先执行 slideDown 动画,再标注展开状态样式,避免元素 `block` 状态导致动画无效
|
||||
child.slideDown(ANIM_MS, complete);
|
||||
parent.addClass(NAV_ITEMED);
|
||||
} else {
|
||||
// 先取消展开状态样式,再将元素临时显示,避免 `none` 状态导致 slideUp 动画无效
|
||||
parent.removeClass(NAV_ITEMED);
|
||||
child.show().slideUp(ANIM_MS, complete);
|
||||
}
|
||||
|
||||
// 手风琴 --- 收缩兄弟展开项
|
||||
if (typeof parents.attr('lay-accordion') === 'string' || parents.attr('lay-shrink') === 'all') {
|
||||
// 收缩兄弟项
|
||||
parent.siblings().removeClass([
|
||||
NAV_ITEMED,
|
||||
NAV_EXPAND
|
||||
].join(' ')).children('.'+NAV_CHILD).stop().slideUp(200);
|
||||
var parentSibs = parent.siblings('.'+ NAV_ITEMED);
|
||||
parentSibs.removeClass(NAV_ITEMED);
|
||||
parentSibs.children('.'+ NAV_CHILD).show().stop().slideUp(ANIM_MS, complete);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -359,48 +376,57 @@ layui.define('jquery', function(exports){
|
||||
|
||||
// 导航菜单
|
||||
,nav: function(){
|
||||
var TIME = 200, timer = {}, timerMore = {}, timeEnd = {}, NAV_TITLE = 'layui-nav-title'
|
||||
var TIME = 200;
|
||||
var timer = {};
|
||||
var timerMore = {};
|
||||
var timeEnd = {};
|
||||
var NAV_TITLE = 'layui-nav-title';
|
||||
|
||||
// 滑块跟随
|
||||
,follow = function(bar, nav, index){
|
||||
var othis = $(this), child = othis.find('.'+NAV_CHILD);
|
||||
var follow = function(bar, nav, index) {
|
||||
var othis = $(this);
|
||||
var child = othis.find('.'+NAV_CHILD);
|
||||
|
||||
// 是否垂直导航菜单
|
||||
if (nav.hasClass(NAV_TREE)) {
|
||||
// 无子菜单时跟随
|
||||
if (!child[0]) {
|
||||
var thisA = othis.children('.'+ NAV_TITLE);
|
||||
bar.css({
|
||||
top: othis.offset().top - nav.offset().top
|
||||
,height: (thisA[0] ? thisA : othis).outerHeight()
|
||||
,opacity: 1
|
||||
top: othis.offset().top - nav.offset().top,
|
||||
height: (thisA[0] ? thisA : othis).outerHeight(),
|
||||
opacity: 1
|
||||
});
|
||||
}
|
||||
} else {
|
||||
child.addClass(NAV_ANIM);
|
||||
|
||||
// 若居中对齐
|
||||
if(child.hasClass(NAV_CHILD_C)) child.css({
|
||||
if (child.hasClass(NAV_CHILD_C)) {
|
||||
child.css({
|
||||
left: -(child.outerWidth() - othis.width()) / 2
|
||||
});
|
||||
}
|
||||
|
||||
// 滑块定位
|
||||
if (child[0]) { // 若有子菜单,则滑块消失
|
||||
bar.css({
|
||||
left: bar.position().left + bar.width()/2
|
||||
,width: 0
|
||||
,opacity: 0
|
||||
left: bar.position().left + bar.width() / 2,
|
||||
width: 0,
|
||||
opacity: 0
|
||||
});
|
||||
} else { // bar 跟随
|
||||
bar.css({
|
||||
left: othis.position().left + parseFloat(othis.css('marginLeft'))
|
||||
,top: othis.position().top + othis.height() - bar.height()
|
||||
left: othis.position().left + parseFloat(othis.css('marginLeft')),
|
||||
top: othis.position().top + othis.height() - bar.height()
|
||||
});
|
||||
}
|
||||
|
||||
// 渐显滑块并适配宽度
|
||||
timer[index] = setTimeout(function() {
|
||||
bar.css({
|
||||
width: child[0] ? 0 : othis.width()
|
||||
,opacity: child[0] ? 0 : 1
|
||||
width: child[0] ? 0 : othis.width(),
|
||||
opacity: child[0] ? 0 : 1
|
||||
});
|
||||
}, device.ie && device.ie < 10 ? 0 : TIME);
|
||||
|
||||
@ -418,23 +444,24 @@ layui.define('jquery', function(exports){
|
||||
|
||||
// 遍历导航
|
||||
$(NAV_ELEM + elemFilter).each(function(index) {
|
||||
var othis = $(this)
|
||||
,bar = $('<span class="'+ NAV_BAR +'"></span>')
|
||||
,itemElem = othis.find('.'+NAV_ITEM);
|
||||
var othis = $(this);
|
||||
var bar = $('<span class="'+ NAV_BAR +'"></span>');
|
||||
var itemElem = othis.find('.'+NAV_ITEM);
|
||||
|
||||
// hover 滑动效果
|
||||
if (!othis.find('.'+NAV_BAR)[0]) {
|
||||
othis.append(bar);
|
||||
( othis.hasClass(NAV_TREE)
|
||||
? itemElem.find('dd,>.'+ NAV_TITLE)
|
||||
: itemElem).on('mouseenter', function(){
|
||||
: itemElem
|
||||
).on('mouseenter', function() {
|
||||
follow.call(this, bar, othis, index);
|
||||
}).on('mouseleave', function() { // 鼠标移出
|
||||
// 是否为垂直导航
|
||||
if (othis.hasClass(NAV_TREE)) {
|
||||
bar.css({
|
||||
height: 0
|
||||
,opacity: 0
|
||||
height: 0,
|
||||
opacity: 0
|
||||
});
|
||||
} else {
|
||||
// 隐藏子菜单
|
||||
@ -445,14 +472,16 @@ layui.define('jquery', function(exports){
|
||||
}, 300);
|
||||
}
|
||||
});
|
||||
|
||||
// 鼠标离开当前菜单时
|
||||
othis.on('mouseleave', function() {
|
||||
clearTimeout(timer[index])
|
||||
timeEnd[index] = setTimeout(function() {
|
||||
if (!othis.hasClass(NAV_TREE)) {
|
||||
bar.css({
|
||||
width: 0
|
||||
,left: bar.position().left + bar.width()/2
|
||||
,opacity: 0
|
||||
width: 0,
|
||||
left: bar.position().left + bar.width() / 2,
|
||||
opacity: 0
|
||||
});
|
||||
}
|
||||
}, TIME);
|
||||
@ -461,9 +490,9 @@ layui.define('jquery', function(exports){
|
||||
|
||||
// 展开子菜单
|
||||
itemElem.find('a').each(function() {
|
||||
var thisA = $(this)
|
||||
,parent = thisA.parent()
|
||||
,child = thisA.siblings('.'+NAV_CHILD);
|
||||
var thisA = $(this);
|
||||
var parent = thisA.parent();
|
||||
var child = thisA.siblings('.'+ NAV_CHILD);
|
||||
|
||||
// 输出小箭头
|
||||
if (child[0] && !thisA.children('.'+ NAV_MORE)[0]) {
|
||||
|
Loading…
Reference in New Issue
Block a user