background-position另类用法

  • 来源: 互联网 作者: 若水   2008-03-22/11:59
  • <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <HTML XMLns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/HTML; charset=gb2312" />
    <title>让我们共同推广WEB标准在中国的发展</title>

    <style type="text/CSS">
    <!--
    ul#menu { width:300px; height:300px; margin:0px auto; padding:0px;  overflow:hidden; background:transparent url(/articleimg/2006/04/3366/01.jpg)}
    ul#menu  li { list-style-type:none; float:left; width:100px; height:100px;}
    ul#menu li a { display:block; width:100px; height:100px;  text-decoration:none; background:transparent url(/articleimg/2006/04/3366/02.jpg) no-repeat 500px 500px;}
    a#item1:hover {background-position: 0 0; }
    a#item2:hover {background-position: -100px 0;}
    a#item3:hover {background-position: -200px 0;}
    a#item4:hover {background-position: 0 -100px;}
    a#item5:hover {background-position: -100px -100px;}
    a#item6:hover {background-position: -200px -100px;}
    a#item7:hover {background-position: 0 -200px;}
    a#item8:hover {background-position: -100px -200px;}
    a#item9:hover {background-position: -200px -200px;}

    -->
    </style>
    </head>
    <body>
    <ul id="menu">
    <li><a id="item1" href="#" title="Item 1">&nbsp;</a></li>
    <li><a id="item2" href="#" title="Item 2">&nbsp;</a></li>
    <li><a id="item3" href="#" title="Item 3">&nbsp;</a></li>
    <li><a id="item4" href="#" title="Item 4">&nbsp;</a></li>
    <li><a id="item5" href="#" title="Item 5">&nbsp;</a></li>
    <li><a id="item6" href="#" title="Item 6">&nbsp;</a></li>
    <li><a id="item7" href="#" title="Item 7">&nbsp;</a></li>
    <li><a id="item8" href="#" title="Item 8">&nbsp;</a></li>
    <li><a id="item9" href="#" title="Item 9">&nbsp;</a></li>
    </ul>
    </body>
    </HTML>

    代码:

    <style>
    ul { width:300px; height:300px; margin:0px auto; padding:0px; overflow:hidden; background:transparent url(image/menu_gray.jpg)}
    li {list-style-type:none; width:100px; height:100px; float:left}
    li a { display:block; width:100px; height:100px; text-decoration:none;
    background:transparent url(image/menu_color.jpg) no-repeat 500px 500px; }
    a#item1:hover {background-position: 0 0; }
    a#item2:hover {background-position: -100px 0;}
    a#item3:hover {background-position: -200px 0;}
    a#item4:hover {background-position: 0 -100px;}
    a#item5:hover {background-position: -100px -100px;}
    a#item6:hover {background-position: -200px -100px;}
    a#item7:hover {background-position: 0 -200px;}
    a#item8:hover {background-position: -100px -200px;}
    a#item9:hover {background-position: -200px -200px;}
    </style>

    <ul>
    <li><a id="item1" href="#" title="Item 1">&nbsp;</a></li>
    <li><a id="item2" href="#" title="Item 2">&nbsp;</a></li>
    <li><a id="item3" href="#" title="Item 3">&nbsp;</a></li>
    <li><a id="item4" href="#" title="Item 4">&nbsp;</a></li>
    <li><a id="item5" href="#" title="Item 5">&nbsp;</a></li>
    <li><a id="item6" href="#" title="Item 6">&nbsp;</a></li>
    <li><a id="item7" href="#" title="Item 7">&nbsp;</a></li>
    <li><a id="item8" href="#" title="Item 8">&nbsp;</a></li>
    <li><a id="item9" href="#" title="Item 9">&nbsp;</a></li>
    </ul>

    是不是非常简单,而我们所用到的图片也仅仅是下面的2张而已:

    现在我们来分析 background-position 的用法:

    A元素一开始的时候背景位置设置成 background-position:500px 500px,而它的大小仅只有100px*100px而已,所以A标签下的所有背景都超过了可视范围无法显示,我们一开始看见的黑白照片就是UL的背景图象。接着当鼠标移动到A元素上的时候,再根据每个A元素所处的位置来分别为他们设置背景的移动大小,这样就形成上面的黑白图片交替效果。#p#分页标题#e#




    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多