简体   繁体   English

CSS精灵加载

[英]CSS Sprites loading

I want to change the src attribute of img inside of my span with id="slider_arrow", 我想使用id =“ slider_arrow”更改跨度内img的src属性,

<span id="slider_arrow">
      <img />
</span>

each time my jquery function runs: 每次我的jquery函数运行时:

    $("#0").click(function () {
           /*Rest of the function*/
        var arrow = document.getElementById('slider_arrow');
        /*I want to add different slider_arrow's sprite each time i call this                           function*/            
    });

CSS: CSS:

.red_arrow_sprite{
    width:25px;
    height:12px;
    background:url("/Images/arrows.png") 0 0px;
}

.yellow_arrow_sprite{
    width:25px;
    height:12px;
    background:url("/Images/arrows.png") -26px 0px;
}

.black_arrow_sprite{
    width:25px;
    height:12px;
    background:url("/Images/arrows.png") -51px 0px;
}

My CSS Sprite file has 75px width and 12px of height. 我的CSS Sprite文件的宽度为75px,高度为12px。 Each picture has 25px/12px. 每张图片有25px / 12px。 The questions are: 1) Do I have correctly written CSS rules? 问题是:1)我是否正确编写了CSS规则?
2) What I need to use to change src of img inside of span? 2)我需要使用什么来更改跨度内img的src?

Thanks! 谢谢!

Fiddle: http://jsfiddle.net/vtkppwuc/3/ 小提琴: http : //jsfiddle.net/vtkppwuc/3/

$("#0").click(function () {
    var classes = ['red_arrow_sprite','yellow_arrow_sprite','black_arrow_sprite'];
    var $span = $('#slider_arrow');
    $span.attr('class', (classes[($.inArray($span.attr('class'), classes)+1)%classes.length])); 
});

DEMO: http://jsfiddle.net/vtkppwuc/6/ 演示: http : //jsfiddle.net/vtkppwuc/6/

Remove the image inside span and use jQuery for setting the css class to your Slider, try this code 删除span内的图像,并使用jQuery将CSS类设置为Slider,请尝试以下代码

<span id="slider_arrow" class="red_arrow_sprite">         
</span>

$("#0").click(function () {
    var item = $('#slider_arrow');
    if (item.hasClass('red_arrow_sprite')){
       item.removeClass('red_arrow_sprite').addClass('yellow_arrow_sprite');
    } else if (item.hasClass('yellow_arrow_sprite')){
       item.removeClass('yellow_arrow_sprite').addClass('black_arrow_sprite');
    } else {
       item.removeClass('black_arrow_sprite').addClass('red_arrow_sprite');           
    }
});

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

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