jQuery 切换以更改图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1062731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:32:50  来源:igfitidea点击:

jQuery toggle to change image

jquerycssuser-interfacecss-sprites

提问by hejog

I have a list of divs with images in them:

我有一个包含图像的 div 列表:

<div class="wizard-img"><%= image_tag("sources/pix/nyt.png") %></div>
<div class="wizard-img"><%= image_tag("sources/pix/theguardian.png") %></div>

When a user clicks the div, I'd like it to change the image to x-on.png, when clicked again, it'll change to x-off.png, and when clicked a third time it should revert to x.png.

当用户单击 div 时,我希望它将图像更改为 x-on.png,再次单击时,它将更改为 x-off.png,并且当第三次单击时,它应该恢复为 x。 .png

Is it possible to do this without manually specifying jQuery for every image change? Can I traverse the div and find the image name, and simply append the -off/-on to the image?

是否可以在不为每个图像更改手动指定 jQuery 的情况下执行此操作?我可以遍历 div 并找到图像名称,然后简单地将 -off/-on 附加到图像吗?

Thanks!

谢谢!

回答by nikc.org

Perhaps CSS spriteswould work for you?

也许CSS 精灵对你有用

That would save you from loading a separate image every time you click the image (button?), and you could solve your problem by adding and removing a css-class, e.g.:

这样可以避免每次单击图像(按钮?)时加载单独的图像,并且您可以通过添加和删除 css 类来解决您的问题,例如:

$('.wizard-img').click(
    function() {
        if ($(this).hasClass('on')) {
            $(this).removeClass('on').addClass('off');
        } else if ($(this).hasClass('off')) {
            $(this).removeClass('off');
        } else {
            $(this).addClass('on');
        }
    }
);

回答by jeroen.verhoest

http://docs.jquery.com/Events/toggle

http://docs.jquery.com/Events/toggle

$(".wizard-img").toggle(
  function () {
    $(this).find("img").attr({src:"x-on.png"});
  },
  function () {
    $(this).find("img").attr({src:"x-off.png"});
  },
  function () {
    $(this).find("img").attr({src:"x.png"});
  }
);

回答by Dan F

CSS Sprites would indeed be theway to do this, but just for completeness, here's another option.

CSS精灵确实是做这种方式,只是为了完整性,这里的另一种选择。

Normally I'm the last to reach for a regexp, but I think they'll help here

通常我是最后一个接触正则表达式的人,但我认为他们会在这里提供帮助

$('.wizard-img').click(function() {
    var $img = $(this).find('img') ,
        src = $img.attr('src') ,
        onCheck = /\-on\.jpg$/ ,
        offCheck = /\-off\.jpg$/ ,
        normal = /\.jpg$/
    ;

    if(src.match(onCheck)) {
        $img.attr('src', src.replace(onCheck, '-off.jpg'));
    } else if (src.match(offCheck)) {
        $img.attr('src', src.replace(offCheck, '.jpg'));
    } else {
        $img.attr('src', src.replace(normal, '-on.jpg'));
    }
});

回答by Steve Ingram

I have found this plugin useful to do something similar to what you asked:

我发现这个插件很有用,可以做类似于你问的事情:

http://www.malsup.com/jquery/cycle/

http://www.malsup.com/jquery/cycle/

回答by Deniz Dogan

FWIW, I suggest you do this using plain CSS and background-image. It sounds to me that this is not really normal images, but rather "buttons".

FWIW,我建议您使用纯 CSS 和background-image. 在我看来,这不是真正的普通图像,而是“按钮”。

回答by karim79

$('.wizard-img').click(function() {
    var img = $(this).find('img');
    var src = img.attr('src');

    //image is neither on nor off, so change src to on and return
    if(src != 'x-on.png' && src != 'x-off.png') {
        img.attr('src','x-on.png');
        return false;
    }

    //image is on, so change src to off and return
    if(src == 'x-on.png') {
        img.attr('src','x-off.png');
        return false;    
    }

    //image is off, so change src to x and return
    if(src == 'x-off.png') {
        img.attr('src','x.png');
        return false;    
    }
});

回答by Syed

$("img").hover(
    function() { this.src = this.src.replace("_Off", "_On");},
    function() { this.src = this.src.replace("_On", "_Off");
});

http://kaidez.com/tutorial-simple-effective-jquery-image-rollover/

http://kaidez.com/tutorial-simple-effective-jquery-image-rollover/

回答by xuannd

I created plugin swapimg: http://xuannd.wordpress.com/2010/12/03/jquery-swap-images-easy/
use: $('.swapimg').swapimg();
opt: $('.swapimg').swapimg({imgOn:"_on.png", imgOff:".png"});

我创建了插件 swapimg:http
: //xuannd.wordpress.com/2010/12/03/jquery-swap-images-easy/使用:$('.swapimg').swapimg();
选择:$('.swapimg').swapimg({imgOn:"_on.png", imgOff:".png"});

(function($){
    $.fn.swapimg=function(options){
        var defaults={imgOn:"_on.gif",imgOff:".gif"};
        var options=$.extend(defaults,options);
        var $isOn=false;
        var $obj_index=-1;
        return this.each(
            function(){
                if($(this).is('img')){
                    obj=$(this)
                }
                else{
                    obj=$(this).find('img')
                }
                obj.hover(
                    function(){
                        if(this.src.indexOf('_on')==-1){
                            $isOn=false
                        }else{
                            $isOn=true
                        }

                        if($isOn==false){
                            this.src=this.src.replace(options.imgOff,options.imgOn)
                        }
                    },
                    function(){
                        if($isOn==false){
                            this.src=this.src.replace(options.imgOn,options.imgOff)
                        }
                    }
                )
            }
        )
    }
})(jQuery);