Toggling an image src with jQuery

后端 未结 7 617
孤街浪徒
孤街浪徒 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:24

    This is another approach:

    
    
    

    And for the jQuery

    /* jQuery */
    $('#magic-toggle').click(function() {
    if($('.fun-img').attr('src') === plus) {
      $('.fun-img').attr('src', minus);
    } else {
      $('.fun-img').attr('src', plus)
    }
    })
    

    Needs some fun animation, probably, but gets the work done.

    Here's a snippet:

    var plus = 'https://d30y9cdsu7xlg0.cloudfront.net/png/5805-200.png';
    var minus = 'https://d30y9cdsu7xlg0.cloudfront.net/png/5802-200.png';
    
    $('#magic-toggle').click(function() {
      if ($('.fun-img').attr('src') === plus) {
        $('.fun-img').attr('src', minus);
      } else {
        $('.fun-img').attr('src', plus)
      }
    })
    .fun-img {
      width: 80px;
    }
    
    

提交回复
热议问题