Skip to content

Instantly share code, notes, and snippets.

@foo9
Last active December 16, 2015 02:09
Show Gist options
  • Save foo9/5360249 to your computer and use it in GitHub Desktop.
Save foo9/5360249 to your computer and use it in GitHub Desktop.
// how to use $("#hoge").visible(function () { console.log("(<●>д<●>)", this); }, function () { console.log("( ― д ― )", this); });
// uncompress
;(function ($) {
$.fn.visible = function (visible, invisible) {
if ((typeof visible !== "function") && (typeof invisible !== "function")) {
return this;
}
return this.each(function () {
if ($(this).css('display') === 'none') {
invisible.apply(this);
} else {
visible.apply(this);
}
});
};
})(jQuery || Zepto);
// compress
// ;(function(a){a.fn.visible=function(b,c){return"function"!==typeof b&&"function"!==typeof c?this:this.each(function(){"none"===a(this).css("display")?c.apply(this):b.apply(this)})}})(jQuery||Zepto);
@foo9
Copy link
Author

foo9 commented Apr 11, 2013

こっちのほうが、わかりやすいかも

;(function ($) {
    $.fn.visible = function (callback) {
        if ((typeof callback !== 'function')) {
            return this;
        }

        return this.each(function () {
            if ($(this).css('display') !== 'none') {
                callback.apply(this);
            }
        });
    };

    $.fn.invisible = function (callback) {
        if ((typeof callback !== 'function')) {
            return this;
        }

        return this.each(function () {
            if ($(this).css('display') === 'none') {
                callback.apply(this);
            }
        });
    };
})(jQuery || Zepto);

@foo9
Copy link
Author

foo9 commented Apr 11, 2013

$("#hoge").visible(function(){

}).invisible(function(){

});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment