在左侧的UL列表中添加徽标

时间:2011-09-08 22:26:33

标签: jquery html css

   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>N4N</title>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"></script> 
<script>
/**
 * @author Alexander Farkas
 * v. 1.21
 */


(function($) {
    if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
        var oldCurCSS = jQuery.curCSS;
        jQuery.curCSS = function(elem, name, force){
            if(name === 'background-position'){
                name = 'backgroundPosition';
            }
            if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
                return oldCurCSS.apply(this, arguments);
            }
            var style = elem.style;
            if ( !force && style && style[ name ] ){
                return style[ name ];
            }
            return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
        };
    }

    var oldAnim = $.fn.animate;
    $.fn.animate = function(prop){
        if('background-position' in prop){
            prop.backgroundPosition = prop['background-position'];
            delete prop['background-position'];
        }
        if('backgroundPosition' in prop){
            prop.backgroundPosition = '('+ prop.backgroundPosition;
        }
        return oldAnim.apply(this, arguments);
    };

    function toArray(strg){
        strg = strg.replace(/left|top/g,'0px');
        strg = strg.replace(/right|bottom/g,'100%');
        strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
        var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
        return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
    }

    $.fx.step. backgroundPosition = function(fx) {
        if (!fx.bgPosReady) {
            var start = $.curCSS(fx.elem,'backgroundPosition');

            if(!start){//FF2 no inline-style fallback
                start = '0px 0px';
            }

            start = toArray(start);

            fx.start = [start[0],start[2]];

            var end = toArray(fx.options.curAnim.backgroundPosition);
            fx.end = [end[0],end[2]];

            fx.unit = [end[1],end[3]];
            fx.bgPosReady = true;
        }
        //return;
        var nowPosX = [];
        nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
        nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
        fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

    };
})(jQuery);
</script>
<script>
$(document).ready(function() {
    $("ul#nav li a").addClass("js");
    $("ul#nav li a").hover(
      function () {
        $(this).stop(true,true).animate({backgroundPosition:"(0 0)"}, 200);
        $(this).animate({backgroundPosition:"(0 -5px)"}, 150);
      }, 
      function () {
        $(this).animate({backgroundPosition:"(0 -149px)"}, 200);

      }
    );

});
</script>
<style>
html, body, div {
    margin: 0; padding: 0; border: 0;
}

body {
    background: #f5f0e0 url(noise.png);
}

ul#nav {
    width: 900px; margin: 0 auto; text-align: center; overflow: hidden;
    background: url(bg.jpg) center no-repeat;
}
    ul#nav li {
        float: left; list-style: none; 
    }
        ul#nav li a {
            display: block; width: 97px; height: 77px; 
            padding: 72px 0 0 0; margin: 0 32px 0 32px;
            font: bold 16px Helvetica, Arial, Sans-Serif; text-transform: uppercase;
            color: #9c5959; text-shadow: 0 1px 3px #c4bda6; text-decoration: none;

            background: url(label.png) 0 -149px no-repeat; 
        }
            ul#nav li a:hover {
                background: url(label.png) 0 0 no-repeat;
                color: #eee9d9; text-shadow: 0 2px 3px #4c2222;
            }

            ul#nav li a.js:hover {
                background: url(label.png) 0 -149px no-repeat;
            }

#topHdr {
}
</style>
</head>

<body>
<div id=topHdr>
    <div>
    <ul id="nav">
        <li><a href="#" title="Home">Home</a></li>
        <li><a href="#" title="Events">Events</a></li>
        <li><a href="#" title="About">About</a></li>
        <li><a href="#" title="Extras">Extras</a></li>
        <li><a href="#" title="Share Links">Share It</a></li>

    </ul>
    </div>
</div>
</body>
</html> 

使用上述脚本后的屏幕截图:http://img148.imageshack.us/img148/3513/beforek.gif

我正试图在家的左边添加一个徽标,如下所示:http://img687.imageshack.us/img687/9005/afterg.gif

我在哪里更改CSS以实现这一目标?

1 个答案:

答案 0 :(得分:0)

很简单,您需要在无序列表的顶部添加一个新的列表元素,其中包含您想要的图像。像这样:

<ul id="nav">
   <li><img src="some_image.png" /></li>
   <li><a href="#" title="Home">Home</a></li>
   ......
</ul>

然后你想要设置样式,以便它适合你的其他列表元素。这样的事情应该做:

ul#nav li  {
    width: 97px; height: 77px; 
}

您可以通过多种方式对图像进行不同的设置,我个人会更改CSS的结构,但快速修复只是为了设置所有列表元素的样式。