本文转自:http://www.f-z.cn/id/292
<div class="wrap" id="menu">
<ul>
<li data-id="a">
<span>家用电器
</span>
</li>
<li data-id="b">
<span>手机/运营商/数码
</span>
</li>
<li data-id="c">
<span>电脑办公
</span>
</li>
<li data-id="d">
<span>家居/家具/家装/厨具
</span>
</li>
<li data-id="e">
<span>男装/女装/童装/内衣
</span>
</li>
<li data-id="e">
<span>个户化妆/清洁用品/宠物
</span>
</li>
<li data-id="f">
<span>鞋靴/箱包/珠宝/奢侈品
</span>
</li>
<li data-id="g">
<span>运动/户外/钟表
</span>
</li>
<li data-id="h">
<span>汽车/汽车用品
</span>
</li>
<li data-id="i">
<span>女婴/玩具乐器
</span>
</li>
<li data-id="j">
<span>食品/酒类/生鲜/特产
</span>
</li>
<li data-id="k">
<span>医疗保健
</span>
</li>
</ul>
</div>
左边栏是一个列表,我们就用ul将这个列表所有信息都列出来,得到下面的效果
.wrap
{
position
: relative
;
width
: 200px
;
top
: 50px
;
left
: 50px
;
}
ul
{
padding
: 15px 0
;
margin
: 9px 0
;
list-style
: none
;
background-color
: #6c6669
;
color
: #fff
;
border-right-width
: 0
;
}
li
{
display
: block
;
height
: 30px
;
line-height
: 30px
;
padding-left
: 12px
;
cursor
: pointer
;
font-size
: 14px
;
position
: relative
;
}
li.active
{
background
: #999395
;
}
li span:hover
{
color
: #c81623
;
}
这里我们最外层的div设置div是相对定位,因为等会我们的右边的详细信息会通过设置绝对定位,外层div是相对div,内层的div相对外层的div进行绝对定位,这样实现布局的效果
<div id="sub" class="">
<div id="a" class="sub_content ">
<dl>
<dt>
<a href="#">电视
<i>>
</i>
</a>
</dt>
<dd>
<a href="#">合资品牌
</a>
<a href="#">国产品牌
</a>
<a href="#">互联网品牌
</a>
</dd>
</dl>
<dl>
<dt>
<a href="#">空调
<i>>
</i>
</a>
</dt>
<dd>
<a href="#">壁挂式空调
</a>
<a href="#">柜式空调
</a>
<a href="#">中央空调
</a>
<a href="#">空调配件
</a>
</dd>
</dl>
<dl>
<dt>
<a href="#">洗衣机
<i>>
</i>
</a>
</dt>
<dd>
<a href="#">滚筒洗衣机
</a>
<a href="#">洗烘一体机
</a>
<a href="#">波轮洗衣机
</a>
<a href="#">迷你洗衣机
</a>
<a href="#">洗衣机配件
</a>
</dd>
</dl>
<dl>
<dt>
<a href="#">冰箱
<i>>
</i>
</a>
</dt>
<dd>
<a href="#">对门
</a>
<a href="#">对开门
</a>
<a href="#">三门
</a>
<a href="#">双门
</a>
<a href="#">冷柜/冰吧
</a>
<a href="#">酒柜
</a>
<a href="#">冰箱配件
</a>
</dd>
</dl>
</div>
添加上右边详细菜单的div,这里因为是一个大类后面多个小类,使用dt和dd的语义化标签,更好,在添加上布局样式
.none
{
display
: none
;
}
#sub
{
width
: 600px
;
position
: absolute
;
border
: 1px solid #f7f7f7
;
background
: #f7f7f7
;
box-shadow
: 2px 0 5px
rgba
(0, 0, 0, .3
)
;
left
: 200px
;
top
: 0
;
box-sizing
: border-box
;
margin
: 0
;
padding
: 10px
;
}
.sub_content a
{
font-size
: 12px
;
color
: #666
;
text-decoration
: none
}
.sub_content dd a
{
border-left
: 1px solid #e0e0e0
;
padding
: 0 10px
;
margin
: 4px 0
;
}
.sub_content dl
{
overflow
: hidden
;
}
.sub_content dt
{
float
: left
;
width
: 70px
;
font-weight
: bold
;
clear
: left
;
position
: relative
;
}
.sub_content dd
{
float
: left
;
margin-left
: 5px
;
border-top
: 1px solid #eee
;
margin-bottom
: 5px
;
}
.sub_content dt i
{
width
: 4px
;
height
: 14px
;
font
: 400 9px/14px consolas
;
position
: absolute
;
right
: 5px
;
top
: 5px
;
}
这里的sub定位就是绝对定位,这样实现贴合左边布局显示成功,这里的小括号使用了一个i标签,同时这个i标签,i相对于外层的dt进行了定位,同时这里设置的none是为了隐藏右边,会通过js动态控制添加的class,同时li下的active也是通过js动态添加。
这里的js代码已经写了详细的注释,大家可以下源码,在本地自己打开看看
$
(document
)
.
ready
(
function
(
)
{
var sub
=
$
(
'#sub'
)
;
var activeRow
;
var activeMenu
;
var timer
;
var mouseInsub
=
false
;
sub
.
on
(
'mouseenter'
,
function
(e
)
{
mouseInsub
=
true
;
}
)
.
on
(
'mouseleave'
,
function
(e
)
{
mouseInsub
=
false
;
}
)
$
(
'#menu'
)
.
on
(
'mouseenter'
,
function
(e
)
{
sub
.
removeClass
(
'none'
)
;
}
)
.
on
(
'mouseleave'
,
function
(e
)
{
sub
.
addClass
(
'none'
)
;
if
(activeRow
)
{
activeRow
.
removeClass
(
'active'
)
;
activeRow
=
null
;
}
if
(activeMenu
)
{
activeMenu
.
addClass
(
'none'
)
;
activeMenu
=
null
;
}
}
)
.
on
(
'mouseenter'
,
'li'
,
function
(e
)
{
if
(
!activeRow
)
{
activeRow
=
$
(e
.target
)
.
addClass
(
'active'
)
;
activeMenu
=
$
(
'#'
+ activeRow
.
data
(
'id'
)
)
;
activeMenu
.
removeClass
(
'none'
)
;
return
}
if
(timer
)
{
clearTimeout
(timer
)
;
}
timer
=
setTimeout
(
function
(
)
{
if
(mouseInsub
)
{
return
}
if
(activeRow
)
{
activeRow
.
removeClass
(
'active'
)
;
activeMenu
.
addClass
(
'none'
)
;
}
activeRow
=
$
(e
.target
)
;
activeRow
.
addClass
(
'active'
)
;
activeMenu
=
$
(
'#'
+ activeRow
.
data
(
'id'
)
)
;
activeMenu
.
removeClass
(
'none'
)
;
timer
=
null
;
}
,
300
)
}
)
}
)