代码之家  ›  专栏  ›  技术社区  ›  Ron Harlev

jqueryui中是否支持CSS精灵

  •  5
  • Ron Harlev  · 技术社区  · 14 年前

    jQuery或jqueryui中是否存在支持?或者,一个经过良好调试的jQuery插件

    4 回复  |  直到 14 年前
        1
  •  4
  •   Will    14 年前

    使用sprite取决于到所需位置部分的偏移量——javascript无法访问图像数据,那么怎么会有这种情况呢?

    http://csssprites.com/

        2
  •  2
  •   Brian Maltzan    14 年前

    有一些好的 jquery-tool demos tab anchor 演示,他们的 stylesheet 写得很好。

    @马克:演示使用的标签 one image

        3
  •  1
  •   Peter Ajtai    14 年前

    .attr() :

      // Change the sprite state of an element
    $(elementSelector).attr("class", spriteClassOfChoie);
    

    例如,这里有一个使用sprite和jQuery的超级简单的图像库:

    jsFiddle example

    $(function() {
    
          // The sprite classes
        var sprites = ["home", "next", "prev"];
    
          // Which image is showing
        var showing = 0;
    
          // Show the first image
        $("#gallery").addClass(sprites[showing]);
    
          // Add a click handler to change sprite states
        $("input").click(function() { 
    
              // Cycle through images by making use of sprites
            $("#gallery").attr("class", sprites[(++showing % sprites.length)]);
        });
    });​
    

    HTML格式:

    <input type="button" value="Show Next Image" />
    <br/><br/>
    <div id="gallery"></div>
    

    CSS格式:

    .home {
    width:46px;
    height:44px;
    background:url('http://i.stack.imgur.com/vPDBk.gif') 0 0; }
    
    .next {
    width:43px;
    height:44px;
    background:url('http://i.stack.imgur.com/vPDBk.gif') -47px 0; }
    
    .prev {
    width:43px;
    height:44px;
    background:url('http://i.stack.imgur.com/vPDBk.gif') -91px 0; }
    
        4
  •  1
  •   Peter Ajtai    14 年前

    为什么不全部用CSS呢?洛杉矶:

    btn
    {
    width:50px;
    height:50px;
    background:url(images/btnspite.png) -30px 100px;
    }
    btn:hover
    {
    background-position:-30px -150px;
    }
    btn:active
    {
    background-position:-30px -200px;
    }
    

    这将使您开始实际实施它: http://css-tricks.com/css-sprites/