English 中文(简体)
Preloading images
原标题:
  • 时间:2009-11-25 09:48:42
  •  标签:
  • css
  • joomla

i ve a navigation bar like the one below..for each

  • it changes the background color on mouseover and mouseout and there is a lag to load those images which looks awful :( i wanna know how to preload those images so that i can save that lag time and make it work smoothly..
    <ul class="nav">
     <li class="Today"><a href="/" class="Active"/></li>
     <li class="Hot"><a href="/hot" class=""/></li>
     <li class="New"><a href="/new" class=""/></li>
     <li class="Categoies"><a href="/cat" class=""/></li>
     <li class="Terms"><a href="/terms" class=""></a></li>
    </ul>
    

    the css to display on mouseout event :

    #nav .Today a {
    -moz-background-clip:border;
    -moz-background-inline-policy:continuous;
    -moz-background-origin:padding;
    background:transparent url(../images/today.png) no-repeat scroll left top;
    border:0 none;
    height:25px;
    text-decoration:none;
    width:98px;
    }
    

    the css to display on mouseover event

    #nav .Today .Active {
    -moz-background-clip:border;
    -moz-background-inline-policy:continuous;
    -moz-background-origin:padding;
    background:transparent url(../images/today-over.png) no-repeat scroll left top;
    border:0 none;
    height:25px;
    text-decoration:none;
    width:98px;
    }
    

    BTW, this is done in joomla.. thankx

  • 最佳回答

    thanks for ur replies guys.. i found a way to reload all the images using jquery here: http://www.filamentgroup.com/lab/update_automatically_preload_images_from_css_with_jquery/

    问题回答

    I would do some research into CSS sprites, it will negate the need to preload your images and it will make your page load times go much faster. A List Apart has a good article on CSS sprites.

    If you really wanted to preload that one image you can create a really tiny pixel with a background of the image you wanted to hover over, it s not a very elegant solution but it would do the job.

    #preload
    {
        background-image:url(../images/today-over.png);
        width:1px;
        height:1px;
        position:absolute;
    }
    

    And then on your page:

    <div id="preload"></div>
    

    As Sam152 said, you should use CSS sprites. This will solve your problem as well as cutting down on HTTP requests.

    The simple way is instead of today.png and today-over.png put them into one image, one above the other, that is 98px by 50px. Then use this CSS:

    #nav .Today a {
        background: transparent url(../images/today.png) no-repeat scroll left top;
    }
    #nav .Today .Active {
        background-position: left bottom;
    }
    

    Note: I removed the other styles to keep this looking clean, but keep those in your code!

    The more advanced, and generally better, way is to merge all the menu images into one, so you only have one image to load. You d have a 490x50 image, and CSS like this:

    #nav .Today a {
        background: transparent url(../images/today.png) no-repeat scroll 0 0;
    }
    #nav .Today a.Active {
        background-position: 0 -25px;
    }
    #nav .Hot a {
        background: transparent url(../images/hot.png) no-repeat scroll -98px 0;
    }
    #nav .Hot a.Active {
        background-position: -98px -25px;
    }
    

    ...and so on.

    I suppose you could make your browser preload those images, if you would place them on a hidden div on your page, like this:

    <div style="display:none;"> ... the images ... </div>





    相关问题
    CSS working only in Firefox

    I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

    jquery ui dialog opens only once

    I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

    Drop down background url in Safari Issue

    selectBox.selectCSS { background: url(/Images/replacementSelectBackground.png) top left no-repeat height:auto; } I have an issue in Safari only where the image is not rendering on top ...

    CSS specific for Safari

    How do you target specifically safari in css with styles?

    Aligning textarea in a form

    Ive used the following css code to align my form elements: form { position:relative; } form input { position:absolute; left:11em; } However, the textarea element is not aligned correctly with the ...

    Firefox background image horizontal centering oddity

    I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

    CSS problem with page footer

    I have defined my page footer in the css file as: #footer { position: absolute; height: 50px; text-align: center; background: #66CCCC; bottom: 0px; left: 0px; width: 100%; height: ...

    热门标签