简体   繁体   English

HTML + CSS悬停精灵,不同大小的图标,一个背景位置CSS

[英]HTML+CSS Hover sprites, different size icons, one background-position CSS

Bear with me on this one, it's hard to explain: 忍受这一点,这很难解释:

I have 4 -DIFFERENT SIZE- icons: 我有4个-DIFFERENT SIZE-图标: and another 4 for the hover effect: 还有4个用于悬停效果:

After countless failed attempts, I decided to ask: 经过无数次失败的尝试之后,我决定问:

How do I create 4 seperate sprite files (1 file = normal + hover version of the icon) in such a way/format that I could use the same CSS class (with the same background-position ) and just change the background-image using inline-css on the element (See HTML code below ) while keeping the positioning? 如何以这样的方式/格式创建4个单独的精灵文件(1个文件=普通+图标的悬停版本),可以使用相同的CSS类(具有相同的background-position ),而只需使用以下方法更改background-image元素上的inline-css(请参见下面的HTML代码),同时保持位置?

For example: background-position: 0 0; 例如: background-position: 0 0; for the normal look, and then on the a:hover - background-position: 0 -45; 正常外观,然后在a:hover - background-position: 0 -45; for every single icon pair. 对于每个单个图标对。

I tried using various generators but what I get is like 0 -45 for one sprite and 0 -37 for another one etc. 我尝试使用各种生成器,但得到的像是一个精灵的0 -45和另一个精灵的0 -37

Maybe someone could actually generate it the 'right way'? 也许有人真的可以以“正确的方式”生成它?

HTML: HTML:

<div class="menuPicHeader"> 
     <a href="#" style="background-image: url(sprite1.png);" class="">Text1</a>
     <a href="#" style="background-image: url(sprite2.png);" class="">Text2</a>
</div>

CSS: CSS:

.menuPicHeader a{   
        background-repeat: no-repeat;
        padding: 0 0 0 45px;
        height: 50px;
        background-position: 0 0;
    }

.menuPicHeader a:hover{
        background-position: ? ?;
    }

Figured it out, after a LOT of trial and error. 经过大量的反复试验后,找到了答案。 Basically, I put the top icon 2 vertical pixels off of the top of png, and use background-position: 0 -2px on the normal state. 基本上,我将顶部图标从png顶部background-position: 0 -2px了2个垂直像素,并在正常状态下使用background-position: 0 -2px Then, put the hover version for example 50 pixels below, with 2 pixels already inside the second picture (so basically 48px), and then use background-position: 0 -50px on the a:hover 然后,将悬停版本放置在例如50像素以下的位置,第二张图片中已经有2像素(因此基本上为48px),然后在a:hover上使用background-position: 0 -50px

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM