开发者

Adding a Logo on a UL list on the left

   <!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 ]){
          开发者_Go百科      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> 

Screenshot after using the script above: http://img148.imageshack.us/img148/3513/beforek.gif

I am trying to add a logo on left of the home like this: http://img687.imageshack.us/img687/9005/afterg.gif

Where do I change the CSS to achieve that?


Quite simply, you need to add a new list element to the top of your unordered list, containing the image you want. Something like this:

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

Then you want to style that so it fits in with your other list elements. Something like this should do:

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

There are many ways in which you could style the image differently, I personally would change the the structure of the CSS, but a quick fix is just to style all list elements.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜