核心分析:
如上面的这个Tab选项卡效果,最核心的就是下方俩行高亮部分代码,第二行的margin-bottom:-1px;使下方的正文部分向上“提”了1px的距离,从而达到了鼠标上移后选项卡白色遮住下方黑色边框的效果(注:由于IE不是符合W3C标准,所以当鼠标移到选项卡上时需要添加一个额外属性position:relative;来修复IE不覆盖下方边框的这个Bug)。第四行的margin-left:-1px;的目的是让四个选项卡向左移动1px的距离,达到左右都只有一条分割线的效果。
CSS代码:
1
2
3
4
5
6
7
8
9
|
.demoTab{
width
:
400px
;
font
:
14px
/
1.5
Microsoft YaHei,
verdana
,
Helvetica
,
Arial
,
sans-serif
;}
.demoTab .demoTabHd{
margin-bottom
:
-1px
;
border
:
1px
solid
#6C92AD
;
border-bottom
:
none
;
background
:
#EAF0FD
;}
.demoTab .demoTabNav{
height
:
28px
;
overflow
:
hidden
; *zoom:
1
;}
.demoTab .demoTabList{
float
:
left
;
margin-left
:
-1px
;
padding
:
0
22px
;
line-height
:
28px
;
border-left
:
1px
solid
#6C92AD
;
border-right
:
1px
solid
#6C92AD
;
font-weight
:
bold
;
color
:
#005590
;
text-align
:
center
;
cursor
:
pointer
;}
.demoTab .demoTabList.current{
position
:
relative
;
background
:
#fff
;}
.demoTab .demoTabBd{
border
:
1px
solid
#6C92AD
;}
.demoTab .demoTabBd .roundBox{
padding
:
15px
;}
.demoTab .demoTabContent{
display
:
none
;}
.demoTab .demoTabContent.current{
display
:
block
;}
|
HTML代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<
div
id
=
"demoTab"
class
=
"demoTab"
>
<
div
class
=
"demoTabHd"
>
<
ul
class
=
"demoTabNav clearfix"
>
<
li
class
=
"demoTabList current"
>前端
li
>
<
li
class
=
"demoTabList"
>实战
li
>
<
li
class
=
"demoTabList"
>交互
li
>
<
li
class
=
"demoTabList"
>优化
li
>
ul
>
div
>
<
div
class
=
"demoTabBd"
>
<
div
class
=
"roundBox"
>
<
div
class
=
"demoTabContent current"
>这是第一个选项卡的内容。
div
>
<
div
class
=
"demoTabContent"
>这是第二个选项卡的内容。
div
>
<
div
class
=
"demoTabContent"
>这是第三个选项卡的内容。
div
>
<
div
class
=
"demoTabContent"
>这是第四个选项卡的内容。
div
>
div
>
div
>
div
>
|
负margin负位移技术应用:
效果预览:
alt
=
"toygers kittens"
/>
a
>
<
span
class
=
"demoIcon"
><
img
width
=
"54"
height
=
"54"
src
=
"buyInSoon.png"
alt
=
"icon"
/>
span
>
div
>
利用负margin制作鳞片式导航:
效果预览:
核心分析:
鳞片式导航可以说是使用负margin技术最佳场合之一。如上方的例子,试着想象不使用负margin,单纯的使用图片来制作这类鳞片式导航,想象下切图、定位、点击后图片切换、中间接壤部分的遮罩、维护开发的工作量,光想想这些就不寒而栗。而利用负margin这里仅仅添加一个margin-left即可达到前后遮盖的效果。
CSS代码:
1
2
3
4
5
6
|
.demoNav .demoList{
float
:
left
;
width
:
154px
;
height
:
53px
;
margin
:
0
0
0
-20px
;}
.demoNav .demoList.firstList{
margin
:
0
;}
.demoNav .demoList a{
display
:
block
;
line-height
:
53px
;
padding
:
0
0
0
25px
;
color
:
#005590
;
background
:
url
(
"flakeNavTab.png"
)
left
top
no-repeat
;
font-weight
:
bold
;
text-decoration
:
none
;}
.demoNav .demoList a:hover{
background
:
url
(
"flakeNavTabHover.png"
)
left
top
no-repeat
;}
.demoNav .demoList.current a{
background
:
url
(
"flakeNavTabCurrent.png"
)
left
top
no-repeat
;}
.demoNav .demoList.current a:hover{
background
:
url
(
"flakeNavTabCurrent.png"
)
left
top
no-repeat
;}
/* fix ie6 hover issue */
|
HTML代码:
1
2
3
4
5
6
7
8
|
<
div
id
=
"demoNav"
class
=
"demoNav"
>
<
ul
class
=
"clearfix"
>
<
li
class
=
"demoList firstList current"
><
a
href
=
"Javascript:void(0)"
>鳞片导航
a
>
li
>
<
li
class
=
"demoList"
><
a
href
=
"Javascript:void(0)"
>前端开发
a
>
li
>
<
li
class
=
"demoList"
><
a
href
=
"Javascript:void(0)"
>用户体验
a
>
li
>
<
li
class
=
"demoList"
><
a
href
=
"Javascript:void(0)"
>实战为主
a
>
li
>
ul
>
div
>
|
利用负margin制作自适应左右布局:
效果预览:
alt
=
"toygers kittens"
/>
div
>
<
div
class
=
"demoText"
>利用负margin制作自适应左右布局
div
>
div
>
div
>