jQuery hide and show toggle div with plus and minus icon

前端 未结 7 2005
说谎
说谎 2020-12-05 02:59

I have the code working for the show and hide the div. How would I add two different icons as a sprite image for when the show and hide are active?

For

7条回答
  •  萌比男神i
    2020-12-05 03:51

    I would say the most elegant way is this:

    ...

    then css:

    .toggle{
     display:inline-block;
    height:48px;
    width:48px;  background:url("http://icons.iconarchive.com/icons/pixelmixer/basic/48/plus-icon.png");
    }
    .toggle.expanded{
      background:url("http://cdn2.iconfinder.com/data/icons/onebit/PNG/onebit_32.png");
    }
    

    and js:

    $(document).ready(function(){
      var $content = $(".content").hide();
      $(".toggle").on("click", function(e){
        $(this).toggleClass("expanded");
        $content.slideToggle();
      });
    });
    

    FIDDLE

提交回复
热议问题