How can I toggle between 2 images

前端 未结 4 487
醉梦人生
醉梦人生 2020-12-13 16:29

Im trying to use this code to toggle between a play and pause button but it doesn\'t seem to be working. How can I get it toggle between the 2 images when the are clicked

相关标签:
4条回答
  • 2020-12-13 17:01

    You can use the .toggle() function. I've updated your fiddle. Also, you hadn't escaped the quotes properly in your image tags.

    $("#infoToggler").toggle(function() {
        $(this).html('<img src="http://maraa.in/wp-content/uploads/2011/09/pause-in-times-of-conflict.png" width="60px" height="60px"/>');
    }, function() {
        $(this).html('<img src="http://tympanus.net/PausePlay/images/play.png" width="60px" height="60px"/>');
    });​
    
    0 讨论(0)
  • 2020-12-13 17:06
    <div id="infoToggler"><img src="http://tympanus.net/PausePlay/images/play.png" width="60px" height="60px"/></div>
    $(document).ready(function(){
    var src1 = "http://tympanus.net/PausePlay/images/play.png";
    var src2 = "http://maraa.in/wp-content/uploads/2011/09/pause-in-times-of-conflict.png";
    $("#infoToggler").click(function(){
       var src = $('#infoToggler img').attr('src'); 
       if(src == src1){$('#infoToggler img').attr('src',src2);}
       else{$('#infoToggler img').attr('src',src1);}
    });
    

    })​

    it's working, i had checked..

    0 讨论(0)
  • 2020-12-13 17:19

    A different maybe easier way to handle it:

    http://jsfiddle.net/M9QBb/1/

    $("#infoToggler").click(function() {
        $(this).find('img').toggle();
    });​
    
    <div id="infoToggler">
      <img src="image1.png" width="60px" height="60px"/>
      <img src="image2.png" width="60px" height="60px" style="display:none"/>
    </div>
    
    0 讨论(0)
  • 2020-12-13 17:22

    Pure HTML/CSS

    label.tog > input {
      display: none; /* Hide the checkbox */
    }
    
    label.tog > input + span {
      text-indent: -9000px; /* Make text Accessible but not visible */
      display: inline-block;
      width: 24px;
      height: 24px;
      background: center / contain no-repeat url("//i.stack.imgur.com/gmP6V.png"); /*Play*/
    }
    
    label.tog > input:checked + span {
      background-image: url("//i.stack.imgur.com/ciXLl.png"); /*Pause*/
    }
    <label class="tog">
    	  <input type="checkbox" checked>
    	  <span>Button Play Pause</span>
    </label>


    Toggle inner span's images using jQuery

    Useful cause there's no new request to the server to load images:

    <span class="tog">
       <img src="play.png">
       <img src="pause.png" style="display:none;">
    </span>
    

    $(".tog").click(function(){
      $('img',this).toggle();
    });
    

    Or, let's say we have this HTML and the .tog image selector:

    <img class="tog" src="play.png"/>
    

    Using Array.prototype.reverse()

    var togSrc = [ "play.png", "pause.png" ];
    
    $(".tog").click(function() {
       this.src =  togSrc.reverse()[0];
    });
    

    Using the current src value and String.prototype.match()

    Useful if you don't know the initial state (play? pause?)

    var togSrc = [ "play.png", "pause.png" ];
    
    $(".tog").click(function() {
      this.src = togSrc[ this.src.match('play') ? 1 : 0 ];
    });
    

    NOTE: for the last two examples you need to pre-load your images, to prevent the time-gap the browsers makes to request and load a new image from the server.

    0 讨论(0)
提交回复
热议问题