Slidetoggle with a close button - jQuery

2019-08-05 23:36发布

I'm using the slidetoggle function to open & close a box when clicking on a link. I want to be able to add a "close" button (link) inside that box aswell as being able to close it via the slidetoggle trigger. How would i do this?

This is my jQuery:

$(".popup-b").hide(); 
$(".popup-l").click(function(){
    $(this).toggleClass("active").next().slideToggle(0);
    return false;
});

My HTML

<a class="popup-l" href="#">Areas</a>
<div id="area-p" class="popup-box">
    <div class="close-this"><a href="#">Close</a></div>
    <p>This is the popup box</p>
</div>

2条回答
beautiful°
2楼-- · 2019-08-06 00:04

jQuery:

$(".popup-b").hide(); 
$(".popup-l").click(function(){
    $(this).toggleClass("active").next().slideToggle(0);
    return false;
});
$(".close-popup").click(function() {
  $(this).parent().prev().toggleClass("active").next().slideToggle(0);
);

HTML

<a class="popup-l" href="#">Areas</a>
<div id="area-p" class="popup-box">
    <div class="close-this"><a href="#" class="close-popup">Close</a></div>
    <p>This is the popup box</p>
</div>
查看更多
Evening l夕情丶
3楼-- · 2019-08-06 00:25

Check out this Fiddle to see it working.

$(".popup-content").hide(); 
$(".popup-title").click(function(){
    $(this).toggleClass("active").next().slideToggle(0);
    return false;
});
$(".popup-content .close-this").click(function(){
    $(".popup-title", $(this).parents(".popup-box")).click();
    return false;
});

This way the active class will be setted when the close link is used too.

You can also have many .popup-box on the same page.

查看更多
登录 后发表回答