fadeIn / fadeOut based on a boolean

后端 未结 2 1403
眼角桃花
眼角桃花 2021-02-19 23:02

I was wondering if I really have to write:

if (status) {
    $(\'#status-image-\' + id).fadeIn();
} else {
    $(\'#status-image-\' + id).fadeOut();
}

2条回答
  •  盖世英雄少女心
    2021-02-19 23:22

    No, there is not, but you can make one like that:

    jQuery.fn.fadeInOrOut = function(status){
        return status ? this.fadeIn() : this.fadeOut();
    }
    

    and then call it like that (see this jsfiddle for a proof):

    $('#status-image-' + id).fadeInOrOut(status);
    

    Is it what you wanted?

提交回复
热议问题