最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery is(:visible) for visibility : hidden - Stack Overflow

programmeradmin2浏览0评论

In jQuery:

e.is(':visible');

checks if an element is displayed or not.

Is there an function in jQuery to check if an element has the attribute visibility to hidden or visible?

Now I have to make that function myself. But i want to use the jQuery function instead if it exists.

The function I made:

$.fn.isVisible = function() {
    return ($(this).css('opacity') != '0' && $(this).css('visibility') !== 'hidden');
};

To extend my example: JsFiddle

The real question is: Is there a jQuery function or not?

In jQuery:

e.is(':visible');

checks if an element is displayed or not.

Is there an function in jQuery to check if an element has the attribute visibility to hidden or visible?

Now I have to make that function myself. But i want to use the jQuery function instead if it exists.

The function I made:

$.fn.isVisible = function() {
    return ($(this).css('opacity') != '0' && $(this).css('visibility') !== 'hidden');
};

To extend my example: JsFiddle

The real question is: Is there a jQuery function or not?

Share Improve this question edited Apr 28, 2017 at 10:55 Nebulosar asked Apr 28, 2017 at 10:35 NebulosarNebulosar 1,8553 gold badges22 silver badges47 bronze badges 8
  • use attribute selector – guradio Commented Apr 28, 2017 at 10:36
  • 1 btw you're only checking for opacity. Do you think opacity is enough? :) and also you have an extra ) in your code – Roko C. Buljan Commented Apr 28, 2017 at 10:37
  • 1 @RokoC.Buljan From documentation: "Elements with visibility: hidden or opacity: 0 are considered to be visible" – Esko Commented Apr 28, 2017 at 10:38
  • 1 @Roko C. Buljan it was a quick paste, sorted afterwards ;) – Morpheus Commented Apr 28, 2017 at 10:39
  • 1 So i also quick pasted my code.. sorry for the EXTRA ')' bothering you.. But thanks for the response :) – Nebulosar Commented Apr 28, 2017 at 10:42
 |  Show 3 more comments

2 Answers 2

Reset to default 17

You can check css property visibility is set to visible or hidden.

if ($("#element").css("visibility") === "visible") {
    //...
}

or in your case:

$.fn.isVisible = function() {
    return $(this).css('visibility') === 'visible';
};

You can use $('#test').css('visibility'); to get the value of visibility

发布评论

评论列表(0)

  1. 暂无评论