Check if element is visible in DOM

后端 未结 18 1719

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

18条回答
  •  旧时难觅i
    2020-11-22 08:10

    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:

      // Build a NodeList:
      var nl = document.querySelectorAll('.myCssSelector');
    
      // convert it to array:
      var myArray = [];for(var i = nl.length; i--; myArray.unshift(nl[i]));
    
      // now find the visible (= with offsetWidth more than 0) item:
      for (i =0; i < myArray.length; i++){
        var curEl = myArray[i];
        if (curEl.offsetWidth !== 0){
          return curEl.getAttribute("class");
        }
      }
    

提交回复
热议问题