jQuery hide and show toggle div with plus and minus icon

前端 未结 7 1985
说谎
说谎 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条回答
  • 2020-12-05 03:52

    Here is a quick edit of Enve's answer. I do like roXor's solution, but background images are not necessary. And everbody seems to forgot a preventDefault as well.

    $(document).ready(function() {
      $(".slidingDiv").hide();
    
      $('.show_hide').click(function(e) {
        $(".slidingDiv").slideToggle("fast");
        var val = $(this).text() == "-" ? "+" : "-";
        $(this).hide().text(val).fadeIn("fast");
        e.preventDefault();
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <a href="#" class="show_hide">+</a>
    
    <div class="slidingDiv">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta.
        Mauris massa. Vestibulum lacinia arcu eget nulla. </p>
    
      <p>Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis.
        Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. </p>
    
    </div>

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