开发者

JQuery: How do I determine if a slideToggle has been toggled?

开发者 https://www.devze.com 2022-12-27 17:29 出处:网络
How do I do the following pseudo code in JQuery? $(document).ready(function(){ $(\".btn-slide\").click(function(){

How do I do the following pseudo code in JQuery?

$(document).ready(function(){
    $(".btn-slide").click(function(){
        $("#divToSlide").slideToggle("fast")开发者_StackOverflow中文版;
        if ($(this).isToggled) {  // <---- DOESN'T WORK -------
            // do something when toggled
        } else {
            // do something different when not toggled
        }
    });  
});


Or you could just use the toggle function: http://api.jquery.com/toggle/

$('#target').toggle(function() {
    alert('First handler for .toggle() called.');
}, function() {
    alert('Second handler for .toggle() called.');
});

Note: You can have as many toggles as you want, just add more functions:

$('#target').toggle(function() {
    alert('1 handler for .toggle() called.');
}, function() {
    alert('2 handler for .toggle() called.');
}, function() {
    alert('3 handler for .toggle() called.');
}, function() {
    alert('4 handler for .toggle() called.');
}, function() {
    alert('5 handler for .toggle() called.');
});

[EDIT]

$('a.toggleButton').toggle(function() {
    $("#divToSlide").slideDown("fast");
    // do something when toggled
}, function() {
    $("#divToSlide").slideUp("fast");
    // do something when toggled    });
});


Try using a callback function for slideToggle

$(document).ready(function(){
    $(".btn-slide").click(function(){
        $("#divToSlide").slideToggle("fast", function(){
            if ($(this).is(":visible")) {  // <---- DOESN'T WORK -------
            // do something when toggled
            }
            else {
            // do something different when not toggled
            }
        });
    });            
 });  


$(document).ready(function(){
    $(".btn-slide").click(function(){
        if ('#divToSlide').is(':visible')) {
            // do something when toggled
        } else {
            // do something different when not toggled
        }
    });  
});


try:

$(document).ready(function(){
    $(".btn-slide").click(function(){
        $("#divToSlide").toggle(
        function() {  
            // do something when toggled
            $(this).slideUp();
        },
        function(){
            // do something different when not toggled
            $(this).slideDown();
        }
    );  
});
0

精彩评论

暂无评论...
验证码 换一张
取 消