Is there any way that I can check if an element is visible in pure JS (no jQuery) ?
So, for example, in this page: Performance Bikes, if you hover over Deals (on the top menu), a window of deals appear, but at the beginning it was not shown. It is in the HTML but it is not visible.
So, given a DOM element, how can I check if it is visible or not? I tried:
window.getComputedStyle(my_element)['display']);
but it doesn't seem to be working. I wonder which attributes should I check. It comes to my mind:
display !== 'none'
visibility !== 'hidden'
Any others that I might be missing?
Here's the code I wrote to find the only visible among a few similar elements, and return the value of its "class" attribute without jQuery:
All the other solutions broke for some situation for me..
See the winning answer breaking at:
http://plnkr.co/edit/6CSCA2fe4Gqt4jCBP2wu?p=preview
Eventually, I decided that the best solution was
$(elem).is(':visible')
- however, this is not pure javascript. it is jquery..so I peeked at their source and found what I wanted
This is the source: https://github.com/jquery/jquery/blob/master/src/css/hiddenVisibleSelectors.js
This may help : Hide the element by positioning it on far most left position and then check the offsetLeft property. If you want to use jQuery you can simply check the :visible selector and get the visibility state of the element.
HTML :
CSS :
javaScript :
jQuery :
jsFiddle
This is what I did:
HTML & CSS: Made the element hidden by default
JavaScript: Added a code to check whether the visibility is hidden or not:
A little addition to ohad navon's answer.
If the center of the element belongs to the another element we won't find it.
So to make sure that one of the points of the element is found to be visible