Toggling an image src with jQuery

后端 未结 7 602
孤街浪徒
孤街浪徒 2020-12-06 03:30

I wrote some code to toggle the src of an image and also to pause/resume the jQuery cycle2 plugin.

I\'m not sure why it isn\'t working and would appreciate some help

7条回答
  •  失恋的感觉
    2020-12-06 04:07

    For UI elements, such as pause/play buttons, you should be using CSS backgrounds, not inline images. That was you can simply swap class names to change the image.

    You can use inline images, but you should simplify using class names, once again.

    $('.play-pause').click(function(){
        if (!$(this).hasClass('play')) {
            $(this).attr('src', '/images/template/play.png');
            $(this).addClass('play')
            $('.cycle-slideshow').cycle('pause');   
        } else  {
            $(this).attr('src', '/images/template/pause.png');
            $(this).removeClass('play')
            $('.cycle-slideshow').cycle('resume');
        }
    });
    

提交回复
热议问题