>  기사  >  웹 프론트엔드  >  CSS3로 멋진 내비게이션 바 효과 만들기

CSS3로 멋진 내비게이션 바 효과 만들기

WBOY
WBOY원래의
2016-08-27 08:53:571966검색

오늘은 호버 선택기를 주로 사용합니다. 효과를 보려면 마우스를 위로 올리십시오.

1. 일반 네비게이션 바

  • 콘텐츠
  • 서비스
  • 연락처

이 평범한 네비게이션 바의 경우, 마우스가 위로 슬라이드될 때만 색상이 바뀌므로 아이디어가 매우 간단해집니다.

(1) ul 태그 레이아웃 사용

(2) 마우스 통과 이벤트

<span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="demo1"</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><</span><span style="color: #800000;">ul</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>Home<span style="color: #0000ff;"></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>Content<span style="color: #0000ff;"></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>Service<span style="color: #0000ff;"></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>Team<span style="color: #0000ff;"></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>Contact<span style="color: #0000ff;"></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"></</span><span style="color: #800000;">ul</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #800000;">*</span>{<span style="color: #ff0000;">
    padding</span>:<span style="color: #0000ff;">0</span>;<span style="color: #ff0000;">
    margin</span>:<span style="color: #0000ff;">0</span>;<span style="color: #ff0000;">
    list-style</span>:<span style="color: #0000ff;">none</span>;<span style="color: #ff0000;">
    text-decoration</span>:<span style="color: #0000ff;">none</span>;
}<span style="color: #800000;">
a</span>{<span style="color: #ff0000;">
    color</span>:<span style="color: #0000ff;">#fff</span>;
}<span style="color: #800000;">
#demo1</span>{<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;">600px</span>;
}<span style="color: #800000;">
#demo1 ul li</span>{<span style="color: #ff0000;">
    float</span>:<span style="color: #0000ff;">left</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;">100px</span>;<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;">50px</span>;<span style="color: #ff0000;">
    text-align</span>:<span style="color: #0000ff;">center</span>;<span style="color: #ff0000;">
    background</span>:<span style="color: #0000ff;">#ccc</span>;<span style="color: #ff0000;">
    line-height</span>:<span style="color: #0000ff;">50px</span>;<span style="color: #ff0000;">
    color</span>:<span style="color: #0000ff;">#FFF</span>;
}<span style="color: #800000;">
#demo1 ul li:hover</span>{<span style="color: #ff0000;">
    background</span>:<span style="color: #0000ff;">#999</span>;
}

(2) 브라켓 네비게이션 바

  • 콘텐츠
  • 서비스
  • 연락처

여기에서는 탐색 표시줄의 레이아웃에 대해 많이 언급하지 않겠습니다. 마우스를 위에 올리면 텍스트에 괄호가 표시됩니다. 아이디어:

(1) 마우스 통과 이벤트가 없을 때 괄호는 실제로는 있지만 텍스트 중간에 보이지 않습니다

(2) 마우스가 넘어가면 괄호가 가운데에서 양쪽으로 이동하면서 보이지 않는 상태에서 보이는 상태로 변경됩니다.

 <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="demo2"</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><</span><span style="color: #800000;">ul</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><</span><span style="color: #800000;">li</span><span style="color: #0000ff;">><</span><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="#"</span><span style="color: #0000ff;">></span>Home<span style="color: #0000ff;"></</span><span style="color: #800000;">a</span><span style="color: #0000ff;">></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><</span><span style="color: #800000;">li</span><span style="color: #0000ff;">><</span><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="#"</span><span style="color: #0000ff;">></span>Content<span style="color: #0000ff;"></</span><span style="color: #800000;">a</span><span style="color: #0000ff;">></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><</span><span style="color: #800000;">li</span><span style="color: #0000ff;">><</span><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="#"</span><span style="color: #0000ff;">></span>Service<span style="color: #0000ff;"></</span><span style="color: #800000;">a</span><span style="color: #0000ff;">></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><</span><span style="color: #800000;">li</span><span style="color: #0000ff;">><</span><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="#"</span><span style="color: #0000ff;">></span>Team<span style="color: #0000ff;"></</span><span style="color: #800000;">a</span><span style="color: #0000ff;">></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><</span><span style="color: #800000;">li</span><span style="color: #0000ff;">><</span><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="#"</span><span style="color: #0000ff;">></span>Contact<span style="color: #0000ff;"></</span><span style="color: #800000;">a</span><span style="color: #0000ff;">></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"></</span><span style="color: #800000;">ul</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #800000;">#demo2</span>{<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;">600px</span>;<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;">50px</span>;<span style="color: #ff0000;">
    margin</span>:<span style="color: #0000ff;">20px auto</span>;
}<span style="color: #800000;">
#demo2 ul li</span>{<span style="color: #ff0000;">
    position</span>:<span style="color: #0000ff;">relative</span>;<span style="color: #ff0000;">
    float</span>:<span style="color: #0000ff;">left</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;">100px</span>;<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;">50px</span>;<span style="color: #ff0000;">
    text-align</span>:<span style="color: #0000ff;">center</span>;<span style="color: #ff0000;">
    line-height</span>:<span style="color: #0000ff;">50px</span>;<span style="color: #ff0000;">
    background</span>:<span style="color: #0000ff;">#000</span>;
}<span style="color: #800000;">
#demo2 ul li a:before</span>{<span style="color: #ff0000;">
    content</span>:<span style="color: #0000ff;">"["</span>;<span style="color: #ff0000;">
    margin-right</span>:<span style="color: #0000ff;">10px</span>;<span style="color: #ff0000;">
    transform</span>:<span style="color: #0000ff;">translateX(20px)</span>;<span style="color: #ff0000;">
    -webkit-transform</span>:<span style="color: #0000ff;">translateX(20px)</span>;<span style="color: #ff0000;">
    -moz-transform</span>:<span style="color: #0000ff;">translateX(20px)</span>;<span style="color: #ff0000;">
    -ms-transform</span>:<span style="color: #0000ff;">translateX(20px)</span>;
    
}<span style="color: #800000;">
#demo2 ul li a:after</span>{<span style="color: #ff0000;">
    content</span>:<span style="color: #0000ff;">"]"</span>;<span style="color: #ff0000;">
    margin-left</span>:<span style="color: #0000ff;">10px</span>;<span style="color: #ff0000;">
    transform</span>:<span style="color: #0000ff;">translateX(-20px)</span>;<span style="color: #ff0000;">
    -webkit-transform</span>:<span style="color: #0000ff;">translateX(-20px)</span>;<span style="color: #ff0000;">
    -moz-transform</span>:<span style="color: #0000ff;">translateX(-20px)</span>;<span style="color: #ff0000;">
    -ms-transform</span>:<span style="color: #0000ff;">translateX(-20px)</span>;
}<span style="color: #800000;">
#demo2 ul li a:before,#demo2 ul li a:after</span>{<span style="color: #ff0000;">
    display</span>:<span style="color: #0000ff;">inline-block</span>;<span style="color: #ff0000;">
    opacity</span>:<span style="color: #0000ff;">0</span>;<span style="color: #ff0000;">
    transition</span>:<span style="color: #0000ff;">transform 0.3s, opacity 0.2s</span>;<span style="color: #ff0000;">
    -moz-transition</span>:<span style="color: #0000ff;">transform 0.3s, opacity 0.2s</span>;<span style="color: #ff0000;">
    -webkit-transition</span>:<span style="color: #0000ff;">transform 0.3s, opacity 0.2s</span>;<span style="color: #ff0000;">
    -ms-transition</span>:<span style="color: #0000ff;">transform 0.3s, opacity 0.2s</span>;
}<span style="color: #800000;">
#demo2 ul li a:hover::before,#demo2 ul li a:hover::after,#demo2 ul li a:focus::before,#demo2 ul li a:focus::after </span>{<span style="color: #ff0000;">
    opacity</span>:<span style="color: #0000ff;"> 1</span>;<span style="color: #ff0000;">
    -webkit-transform</span>:<span style="color: #0000ff;"> translateX(0px)</span>;<span style="color: #ff0000;">
    -moz-transform</span>:<span style="color: #0000ff;"> translateX(0px)</span>;<span style="color: #ff0000;">
    transform</span>:<span style="color: #0000ff;"> translateX(0px)</span>;
}

셋. 슬라이딩 네비게이션 바

  • 콘텐츠
  • 서비스
  • 연락처

 

 

从演示效果来看要注意两点

(1)鼠标经过时有横向从上到下

(2)鼠标经过时文字从上到下并且变换颜色

这就和上一个例子很像了

(1)横线其实是存在的,只不过鼠标没有经过时是在文字上方且透明的,鼠标经过时横线由上到下。

(2)文字这里就要定义一个动画了,因为在鼠标经过时它体现了三种状态:

  ①文字从现位置划下

  ②文字从出现在上方

  ③文字从上方滑到现位置

 <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="demo3"</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><</span><span style="color: #800000;">ul</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><</span><span style="color: #800000;">li</span><span style="color: #0000ff;">><</span><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="#"</span><span style="color: #0000ff;">><</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>Home<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></</span><span style="color: #800000;">a</span><span style="color: #0000ff;">></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><</span><span style="color: #800000;">li</span><span style="color: #0000ff;">><</span><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="#"</span><span style="color: #0000ff;">><</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>Content<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></</span><span style="color: #800000;">a</span><span style="color: #0000ff;">></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><</span><span style="color: #800000;">li</span><span style="color: #0000ff;">><</span><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="#"</span><span style="color: #0000ff;">><</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>Service<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></</span><span style="color: #800000;">a</span><span style="color: #0000ff;">></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><</span><span style="color: #800000;">li</span><span style="color: #0000ff;">><</span><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="#"</span><span style="color: #0000ff;">><</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>Team<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></</span><span style="color: #800000;">a</span><span style="color: #0000ff;">></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><</span><span style="color: #800000;">li</span><span style="color: #0000ff;">><</span><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="#"</span><span style="color: #0000ff;">><</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>Contact<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></</span><span style="color: #800000;">a</span><span style="color: #0000ff;">></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"></</span><span style="color: #800000;">ul</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #800000;">#demo3 ul li</span>{<span style="color: #ff0000;">
    float</span>:<span style="color: #0000ff;">left</span>;<span style="color: #ff0000;">
    margin</span>:<span style="color: #0000ff;">0 25px</span>;<span style="color: #ff0000;">
    position</span>:<span style="color: #0000ff;">relative</span>;
}<span style="color: #800000;">
#demo3 ul li a</span>{<span style="color: #ff0000;">
    color</span>:<span style="color: #0000ff;">#D8761E</span>;<span style="color: #ff0000;">
    font-family</span>:<span style="color: #0000ff;">'Righteous', cursive</span>;<span style="color: #ff0000;">
    display</span>:<span style="color: #0000ff;">block</span>;<span style="color: #ff0000;">
    padding</span>:<span style="color: #0000ff;">10px 0</span>;
}<span style="color: #800000;">
#demo3 ul li span</span>{<span style="color: #ff0000;">
    display</span>:<span style="color: #0000ff;">block</span>;
}<span style="color: #800000;">
#demo3 ul li a:before</span>{<span style="color: #ff0000;">
    content</span>:<span style="color: #0000ff;">""</span>;<span style="color: #ff0000;">
    position</span>:<span style="color: #0000ff;">absolute</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;">100%</span>;<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;">3px</span>;<span style="color: #ff0000;">
    background</span>:<span style="color: #0000ff;">#D8761E</span>;<span style="color: #ff0000;">
    bottom</span>:<span style="color: #0000ff;">0</span>;<span style="color: #ff0000;">
    opacity</span>:<span style="color: #0000ff;">0</span>;<span style="color: #ff0000;">
    -webkit-transform</span>:<span style="color: #0000ff;"> translate3d(0, -40px, 0)</span>;<span style="color: #ff0000;">
    transform</span>:<span style="color: #0000ff;"> translate3d(0, -40px, 0)</span>;<span style="color: #ff0000;">
    -webkit-transition</span>:<span style="color: #0000ff;"> -webkit-transform 0s 0.3s, opacity 0.2s</span>;<span style="color: #ff0000;">
    transition</span>:<span style="color: #0000ff;"> transform 0s 0.3s, opacity 0.2s</span>;
}<span style="color: #800000;">
#demo3 ul li a:hover::before</span>{<span style="color: #ff0000;">
    opacity</span>:<span style="color: #0000ff;">1</span>;<span style="color: #ff0000;">
    -webkit-transform</span>:<span style="color: #0000ff;"> translate3d(0, 0, 0)</span>;<span style="color: #ff0000;">
    transform</span>:<span style="color: #0000ff;"> translate3d(0, 0, 0)</span>;<span style="color: #ff0000;">
    -webkit-transition</span>:<span style="color: #0000ff;"> -webkit-transform 0.5s, opacity 0.1s</span>;<span style="color: #ff0000;">
    transition</span>:<span style="color: #0000ff;"> transform 0.5s, opacity 0.1s</span>;<span style="color: #ff0000;">
    -webkit-transition-timing-function</span>:<span style="color: #0000ff;"> cubic-bezier(0.4, 0, 0.2, 1)</span>;<span style="color: #ff0000;">
    transition-timing-function</span>:<span style="color: #0000ff;"> cubic-bezier(0.4, 0, 0.2, 1)</span>; 
}<span style="color: #800000;">
#demo3 ul li a:hover span</span>{<span style="color: #ff0000;">
    color</span>:<span style="color: #0000ff;">#510301</span>;<span style="color: #ff0000;">
    -webkit-animation</span>:<span style="color: #0000ff;"> anim-francisco 0.3s forwards</span>;<span style="color: #ff0000;">
    animation</span>:<span style="color: #0000ff;"> anim-francisco 0.3s forwards</span>;
}<span style="color: #800000;">
@-webkit-keyframes anim-francisco </span>{<span style="color: #ff0000;">
    50% {
        opacity</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;">
        -webkit-transform</span>:<span style="color: #0000ff;"> translate3d(0, 100%, 0)</span>;<span style="color: #ff0000;">
        transform</span>:<span style="color: #0000ff;"> translate3d(0, 100%, 0)</span>;
    }<span style="color: #800000;">

    51% </span>{<span style="color: #ff0000;">
        opacity</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;">
        -webkit-transform</span>:<span style="color: #0000ff;"> translate3d(0, -100%, 0)</span>;<span style="color: #ff0000;">
        transform</span>:<span style="color: #0000ff;"> translate3d(0, -100%, 0)</span>;
    }<span style="color: #800000;">

    100% </span>{<span style="color: #ff0000;">
        opacity</span>:<span style="color: #0000ff;"> 1</span>;<span style="color: #ff0000;">
        -webkit-transform</span>:<span style="color: #0000ff;"> translate3d(0, 0, 0)</span>;<span style="color: #ff0000;">
        transform</span>:<span style="color: #0000ff;"> translate3d(0, 0, 0)</span>;
    }<span style="color: #800000;">
}

@keyframes anim-francisco </span>{<span style="color: #ff0000;">
    50% {
        opacity</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;">
        -webkit-transform</span>:<span style="color: #0000ff;"> translate3d(0, 100%, 0)</span>;<span style="color: #ff0000;">
        transform</span>:<span style="color: #0000ff;"> translate3d(0, 100%, 0)</span>;
    }<span style="color: #800000;">

    51% </span>{<span style="color: #ff0000;">
        opacity</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;">
        -webkit-transform</span>:<span style="color: #0000ff;"> translate3d(0, -100%, 0)</span>;<span style="color: #ff0000;">
        transform</span>:<span style="color: #0000ff;"> translate3d(0, -100%, 0)</span>;
    }<span style="color: #800000;">

    100% </span>{<span style="color: #ff0000;">
        opacity</span>:<span style="color: #0000ff;"> 1</span>;<span style="color: #ff0000;">
        -webkit-transform</span>:<span style="color: #0000ff;"> translate3d(0, 0, 0)</span>;<span style="color: #ff0000;">
        transform</span>:<span style="color: #0000ff;"> translate3d(0, 0, 0)</span>;
    }<span style="color: #800000;">
}</span>

这里注意的是不能给标签设置宽高,因为一旦设置宽高会影响文字滑动路线,变得很夸张。这里加span也是这个原因,鼠标经过a时让span以a为参照进行滑动。

里面的标签就不解释了,自己查效果会更好哦。

我好无奈呀,用这个显示效果a标签和li的默认样式竟然不能取消,谁能告诉我为什么/(ㄒoㄒ)/~~  不过自己编译的话在浏览器里是正常的

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.